jeudi 5 octobre 2017

template and explicit constructor [duplicate]

This question already has an answer here:

Template situation:

template<typename T>
class Point3T{
public:
    explicit Point3T(T default=0.0);
    explicit Point3T(const Point3T<T>&);
    explicit Point3T(Point3T<T>&&);

    Point3T<T>& operator =(const Point3T<T>&);
    Point3T<T>& operator =(Point3T<T>&&);

    ... some code ...
};

Function to return a random point:

Point3T<float> randompointF()
{
    Point3T<float> point{};
    ...
    return point;
}

During the compilation I get following error:

In function ‘Point3T<float> randompointF()’:
error: no matching function for call to ‘Point3T<float>::Point3T(Point3T<float>&)’
  return pt;
     ^
In function ‘int main()’:
no matching function for call to ‘Point3T<float>::Point3T(Point3T<float>)’
  auto pt = randompointF();

What am I missing ? I have provided both, copy and move constructors plus the operators. When I remove explicit keyword for the constructors everything is okay, what kind of implicit conversion happens here ?

Aucun commentaire:

Enregistrer un commentaire