samedi 31 octobre 2015

Is it possible to have defined both explicit and implicit templated conversion operators?

Question is quite simple, not much to explain.

#include <iostream>

class test
{
private:
    int a, b;
public:
    test(int x, int y)
        : a(x), b(y)
    {}

    template<class T>
    explicit operator T()
    {
        return a + b;
    }

    template<class T>
    operator T()
    {
        return a + b;
    }
};

int main()
{
    test obj(1, 2);

    int a = obj; //implicit conversion

    int b = static_cast<int>(obj); //explicit conversion

    std::cin.get();
}

Aucun commentaire:

Enregistrer un commentaire