Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c++ - What is the difference between is_convertible is_assignable

What is the difference between is_convertible and is_assignable?

Why,

in vs2012

is_convertible<int, int&> is false

is_assignable<int, int&> is true

in gcc4.7.2

is_convertible<int, int&> is false

is_assignable<int, int&> is false
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One difference is that the arguments are the other way round.

is_convertible<From,To> means that an expression of type From can be converted to type To. The standard defines this in terms of a function returning To containing a line return create<From>(); where create<From>() returns a reference to From. So is_convertible<int,int&> is false, since you can't bind an rvalue int to a non-const lvalue reference int&.

is_assignable<T,U> means that an rvalue expression of type U can be assigned to an expression of type T; that is, that t = u; is well-formed. The expression to be assigned to is specified as a function call returning an lvalue reference if T is an lvalue reference, and an rvalue reference otherwise. This means that is_assignable<T,U> can only be true if T is a non-const lvalue reference type; hence being false for <int,int&>.

I would guess that VS2012 either allows assignment to xvalues, or uses a non-standard check for is_assignable, giving what I think is an incorrect result.

Note that in general convertible doesn't imply assignable, since there may be no accessible assignment operator; and assignable doesn't imply convertible, since there may be an assignment operator but no accessible conversion constructor or operator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...