c++standards

Is a c-style cast calling an explicit conversion constructor correct?


Can a c-style cast call an explicit conversion constructor?

Consider the following code:

class Vec3
{
public:

    explicit Vec3(float All) 
        : X(All), Y(All), Z(All)
    {
    }

    Vec3(float InX, float InY, float InZ)
        : X(InX), Y(InY), Z(InZ)
    {
    }

    Vec3()
        : X(0), Y(0), Z(0)
    {
    }

    float X, Y, Z;
};

void Morph(const Vec3& In, Vec3& Out)
{
    Out = In;
}

int main(void)
{
    float Array[3] = {1.0f, 2.0f, 3.0f};
    Vec3 Morphed;
    Morph((const Vec3&)Array[0], Morphed);
}

On Microsoft VS2010's compiler, it converts the line (const Vec3&)Array[0] to a reinterpret_cast and a copy constructor to pass the parameter to the function. Morphed gets a value of [1.0f, 2.0f, 3.0f].

On another compiler from a third party, it converts (const Vec3&)Array[0] to a call to explicit Vec3(float) and then the copy constructor to pass the parameter to the function. Morphed gets a value of [1.0f, 1.0f, 1.0f].

I verified this by looking at the disassembly from both compilers:

VS2010:

   483:     float Array[3] = {1.0f, 2.0f, 3.0f};
0000000143E68049  movss       xmm0,dword ptr [__real@3f800000 (145DA1F18h)]  
0000000143E68051  movss       dword ptr [rsp+28h],xmm0  
0000000143E68057  movss       xmm0,dword ptr [__real@40000000 (145DC9704h)]  
0000000143E6805F  movss       dword ptr [rsp+2Ch],xmm0  
0000000143E68065  movss       xmm0,dword ptr [__real@40400000 (145DC9708h)]  
0000000143E6806D  movss       dword ptr [rsp+30h],xmm0  
   484:     Vec3 Morphed;
0000000143E68073  xorps       xmm0,xmm0  
0000000143E68076  movss       dword ptr [rsp+58h],xmm0  
0000000143E6807C  xorps       xmm0,xmm0  
0000000143E6807F  movss       dword ptr [rsp+5Ch],xmm0  
0000000143E68085  xorps       xmm0,xmm0  
0000000143E68088  movss       dword ptr [rsp+60h],xmm0  
   485:     Morph((const Vec3&)Array[0], Morphed);
0000000143E6808E  lea         rdx,[rsp+58h]  
0000000143E68093  lea         rcx,[rsp+28h]  
0000000143E68098  call        Morph (143E67FB0h) 

Other compiler:

   184:     float Array[3] {1.0f, 2.0f, 3.0f};
0000000001E1F3B0  mov          rax,qword ptr [000000000432D1B0h] 
0000000001E1F3B7  mov          qword ptr [rbp-14h],rax 
0000000001E1F3BB  mov          edi,dword ptr [000000000432D1B8h] 
0000000001E1F3C1  mov          dword ptr [rbp-0Ch],edi 
   185:     Vec3 Morphed;
0000000001E1F3C4  lea          rdi,[rbp-130h] 
0000000001E1F3CB  call         Vec3::Vec3() (0000000003C119C0h) 
   186:     Morph((const Vec3&)Array[0], Morphed);
0000000001E1F3D0  lea          rdi,[rbp-140h] 
0000000001E1F3D7  vmovss       xmm0,dword ptr [rbp-14h] 
0000000001E1F3DC  call         Vec3::Vec3(float) (0000000003C119E0h) 
0000000001E1F3E1  lea          rdi,[rbp-140h] 
0000000001E1F3E8  lea          rsi,[rbp-130h] 
0000000001E1F3EF  call         Morph(Vec3 const&,Vec3&) (0000000001E1F350h) 

Needless to say, this is causing many problems. Which compiler is correct? The third party code relies on Microsoft's implementation, but we are compiling with a different compiler for a different platform.


Solution

  • As far as the C++ standard is concerned, the conversion of a float & into a const Vec3 & is undefined behavior. Both compilers are right because the code is outside of the protection of C++.

    If you want this to be a protected conversion, you have to stop the type punning and do a real copy of the float array into a Vec3.