vendredi 1 avril 2016

Templated constructor matching better than templated type conversion

The following compiles fine:

#include <iostream>
#include <vector>

class Point : public std::vector<double>
{
public:
    Point() = default;
};

class MyClass
{
public:

    template <typename T>
    operator T() const { return T(); }
};

int main()
{
    MyClass myClass;
    Point test = myClass;

    return 0;
}

However, if I have a templated constructor, it does not:

#include <iostream>
#include <vector>

class Point : public std::vector<double>
{
public:
    Point() = default;

    template <typename TVector>
    Point(const TVector& v)
    {
        (*this)[0] = v[0]; // compiler error on this line (saying no operator[] for MyClass, but the point is that this function is used instead of the type conversion function)
    }

};

class MyClass
{
public:

    template <typename T>
    operator T() const { return T(); }
};

int main()
{
    MyClass myClass;
    Point test = myClass;

    return 0;
}

I cannot change the Point class (to add a second default constructor argument or anything like that), so is there a way to change only MyClass to make this work?

Aucun commentaire:

Enregistrer un commentaire