c++crtp

Inherit from class template nested in template argument


I have a trait class that specifies base classes and derived classes for a class hierarchy. I'm trying to inherit from a class template nested inside the trait class.

Here is a tiny example:

template<typename GraphTypes, bool IsNode>
class Graph : public GraphTypes::BaseGraph<IsNode>
{};

class MyGraphTypes {
public:
    template<bool IsNode>
    class BaseGraph {};
};

Graph<MyGraphTypes, true> myGraph;

It compiles on GCC, but not on Clang or MSVC:

I really need it in MSVC. Is it a bug or limitation, or is there something I can do to make MSVC understand the code?

Here you can try it out: https://godbolt.org/z/8rTzGzxPG


Solution

  • Clang provides the better error message here than MSVC.

    BaseGraph is a dependent type because it depends on another template parameter. For these you need to add an additional typename like this:

    template<typename GraphTypes, bool IsNode>
    class Graph : public GraphTypes::template BaseGraph<IsNode>
    {};
    

    This compiles fine on MSVC as well.