c++templatesexplicitcopy-initialization

Why here template Vector3<int> cannot convert to Vector3<int>?


It seems quite weird. Here you can see the error message is that a convert happens between one type and it fails. If I remove the explicit modifier from Vector3's copy constructor it is fine, no error. Could someone explain why? I'm confused.

template<typename T>
class Vector3 {
public:
    explicit Vector3(const Vector3& v) :x(v.x), y(v.y), z(v.z) {}
    Vector3() :x(0), y(0), z(0) {}
    T x, y, z;
};
template<typename T>
Vector3<T> getVec3() {
    return Vector3<T>(); //c2440 "return":cannot convert Vector3<int> to Vector3<int>
}
int main()
{   
    getVec3<int>();
}

Solution

  • return Vector3<T>(); performs copy initialization, which won't consider explicit constructors: including the copy constructor. That's why you should mark the copy constructor non-explicit.

    Copy-initialization is less permissive than direct-initialization: explicit constructors are not converting constructors and are not considered for copy-initialization.

    BTW: Since C++17 your code would work fine because of mandatory copy elision, the copy (and move construction) will be omitted completely.