Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static_cast Conversion constructor vs Conversion operator [duplicate]

After reading this I tried making such conversion with static_cast:

class A;

class B { 
      public: 
         B(){} 

         B(const A&) //conversion constructor
         { 
              cout << "called B's conversion constructor" << endl; 
         } 
};

class A { 
      public: 
         operator B() const //conversion operator
         { 
              cout << "called A's conversion operator" << endl; 
              return B(); 
         } 
};

int main()
{
    A a;

    //Original code, This is ambiguous, 
    //because both operator and constructor have same cv qualification (use -pedantic flag)
    B b = a;

    //Why isn't this ambiguous, Why is conversion constructor called, 
    //if both constructor and  operator have same c-v qualification
    B c = static_cast<B>(a); 
    return 0;
}

I expected it to not compile, because both constructor and operator have same c-v qualification. However it compiled, successfully and static_cast calls constructor instead of operator. Why?

(compiled using gcc 4.8.1 with pedantic and Wall flags)

like image 986
PcAF Avatar asked May 03 '16 20:05

PcAF


People also ask

Does static_cast call constructor?

Static casting an object into their own type can call the copy constructor. When you use static_cast , by defaut (i.e. without optimizations activated) it calls the conversion constructor of the object you are trying to cast into (if it exists). For instance, in this code.

What is a conversion constructor?

A conversion constructor is a single-parameter constructor that is declared without the function specifier explicit . The compiler uses conversion constructors to convert objects from the type of the first parameter to the type of the conversion constructor's class.

Does static_cast create a copy?

What you might not expect is that it does not invoke that function on the current object! Instead, the cast creates a new, temporary copy of the base class part of *this, then invokes onResize on the copy!

Is static_cast an operator?

The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.


1 Answers

The C++11 Standard says (emphasis mine):

5.2.9 Static cast

4 Otherwise, an expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t (8.5). The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The expression e is used as a glvalue if and only if the initialization uses it as a glvalue.

5 Otherwise, the static_cast shall perform one of the conversions listed below. No other conversion shall be performed explicitly using a static_cast.

That explains why the static_cast in

B c = static_cast<B>(a); 

ends up calling the constructor of B.

The conversion operator is used only if the T t(e) is not well-formed.

like image 147
R Sahu Avatar answered Oct 22 '22 13:10

R Sahu