CRTP refer to typedef in derived class from base class

Alexander Bily

I have following code:

template <typename T>
class A
{
    typedef typename T::Type MyType;
};

template <typename T>
class B : public A<B<T>>
{
    typedef T Type;
};

When I try to instantiate B, I get following error message using MSVS 2015:

'Type': is not a member of 'B<int>'

Is this code valid C++ or is MSVS right?

Barry

The problem is at this point

template <typename T>
class A
{
    typedef typename T::Type MyType;
                     ^^^
};

T needs to be a complete type. But in your case, when A<T> is instantiated here:

template <typename T>
class B : public A<B<T>>
                 ^^^^^^^

B<T> is not yet a complete type! So this cannot work unfortunately.

The simple solution is just to pass in Type separately:

template <typename T, typename Type>
class A
{
    typedef Type MyType;
};    

template <typename T>
class B : public A<B<T>, T>
{

};

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

Convert base class to derived class

Convert base class to derived class

Get Derived DTO From Base Class Request Body DTO

How to obtain the derived class type from base when calling a method

Resolve Services derived from base class using DryIoc

How to get the name of a derived class from a vector of base classes

Visibility of a base class constructor in a derived template class

Executing base class overridden method in derived class

C++ replace base class in derived class

class B derived from an abstract base class A, and how can i use singleton in class B?

Unexpectedly able to call derived-class virtual function from base class ctor

Why can't I use alias from a base class in a derived class with templates?

How to derived from the Effect class

overriding the template base method in derived class?

Why is derived class move constructible when base class isn't?

Specify implement interface on derived class when base class implements it abtract

Derived class thinks my base class got deleted

Static method in base class reflect the derived class name

How to call the base class constructor when the derived class can not easily pass the parameters to the base class?

template class derived from `const` specialized version

Accessing private member from derived class

How to refer to implementor class with @ClosureParams from trait

Is there a sensible difference between having class inheritence and having a base class as a data member in derived class?

How to tell which derived class calling override base method

Why must virtual base classes be constructed by the most derived class?

Can you call a constructor for a base class to create a derived object?

C++ Dummy template parameter inducing error while using a typedef in derived template class

Dispose Derived Class

Why do gcc/clang complain about the base class having a protected destructor, but not about the derived class?

TOP Lista

CalienteEtiquetas

Archivo