Tom Saunders
Tom Saunders

Reputation: 13

inherit from property of templated class within templated class

I'm trying to do the following:

#include <memory>
#include <vector>

template<typename T>
class Base
{
public:
    using SharedPtr = std::shared_ptr<Base<T>>;
};

template<typename T>
class BaseVector : public std::vector<Base<T>::SharedPtr>
{};

int main(int argc, char* argv[])
{
    BaseVector<int> v;
    return 0;
}

But I get the following error:

$ g++ -std=c++11 -o template template.cpp
template.cpp:12:57: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
class BaseVector : public std::vector<Base<T>::SharedPtr>
template.cpp:12:57: note:   expected a type, got ‘Base<T>::SharedPtr’
template.cpp:12:57: error: template argument 2 is invalid

I gather this is because the compiler does not know that Base::SharedPtr is a type. How do I define it in the template arguments of BaseVector such that I can inherit from properties of Base?

Upvotes: 1

Views: 73

Answers (1)

Quentin
Quentin

Reputation: 63144

Clang is more helpful here:

main.cpp:12:39: fatal error: template argument for template type parameter
                             must be a type; did you forget 'typename'?

And indeed, you need typename to disambiguate the dependent type:

class BaseVector : public std::vector<typename Base<T>::SharedPtr>
//                                    ^^^^^^^^

Upvotes: 1

Related Questions