5

Ok so I have this code below and when I execute it I get the following error:

type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
     vector<s> v;
template <class T>
class A {
public:
    struct s{T x;};
};

template <class T>
class B: public A<T> {
public:
    using A<T>::s;
    vector<s> v;
};

Can someone please explain the problem.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Keloo
  • 1,368
  • 1
  • 17
  • 36
  • See also https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords. – melpomene Sep 16 '19 at 07:33

1 Answers1

7

The issue is that the compiler doesn't know whether s is a type or a value. This is the case where you add typename or template, but neither of those worked when I tested. Using the full type directly in the vector does work however:

template <class T>
class B: public A<T> {
public:
    using A<T>::s;
    std::vector<typename A<T>::s> v;
};

(Edit) Kept playing with it because why not, and of course typename was needed in the one place I didn't trying it: the using line. The following code also works.

template <class T>
class B: public A<T> {
public:
    using typename A<T>::s;
    std::vector<s> v;
};
melpomene
  • 84,125
  • 8
  • 85
  • 148
Stephen Newell
  • 7,330
  • 1
  • 24
  • 28