What is the difference between is_convertible is_assignable

前端 未结 2 1022
盖世英雄少女心
盖世英雄少女心 2020-12-16 10:42

What is the difference between is_convertible and is_assignable?

Why,

in vs2012

is_convertible         


        
2条回答
  •  我在风中等你
    2020-12-16 11:27

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

    is_convertible 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(); where create() returns a reference to From. So is_convertible is false, since you can't bind an rvalue int to a non-const lvalue reference int&.

    is_assignable 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 can only be true if T is a non-const lvalue reference type; hence being false for .

    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.

提交回复
热议问题