dimanche 21 février 2021

Unusual initialization required to construct object in a templated class

I have some code that is structured like so:

enum class Type {a,b, c, d};


template<typename T>
class External {
    public:
        External(Type t) {

        }
};


template<typename T>
class Base {};

template<typename T>
class Derived : Base<T> {
    public:
    private:
        External<int> e(Type::a);
};


int main() {
    Derived<int> der;

    return 0;
}

When compiled with g++, this gives the following error:

<source>:49:25: error: 'Type::a' is not a type
   49 |         External<int> e(Type::a);
      |                         ^~~~

Through some experimentation I found out that it works if I construct the object of type External differently:

template<typename T>
class Derived : Base<T> {
    public:
    private:
        External<int> e = External<int>(Type::a);
};

Why is that the case? Why does the compiler expect a type in the first case?

Aucun commentaire:

Enregistrer un commentaire