3

I have the following code which compiles successfully in .

template<class T, class ...Args>
class B 
{
public:
   using AbcData = int;
};

template<typename ...Args>
class D : public B<float, Args...>
{
public:
   AbcData m_abc;
};

But when compiled in , it gives the following error.

error C2061: syntax error: identifier 'AbcData'

What is wrong with the code and how to fix this?

JeJo
  • 30,635
  • 6
  • 49
  • 88
acegs
  • 2,621
  • 1
  • 22
  • 31

1 Answers1

4

When the base class B class depends upon the template parameters, even though derived class D here type alias AbcData inherited from the B, using simply AbcData in D class, is not enough.

You need to be explicit, from where you have it

template<typename ...Args>
class D : public B<float, Args...>
{
public:
    typename B<float, Args...>::AbcData m_abc; // --> like this
};
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • is this backward compatible with c++14 syntax? – acegs Jan 08 '20 at 07:08
  • 1
    @acegs Did you mean this: https://godbolt.org/z/qczVg7 ? – JeJo Jan 08 '20 at 07:11
  • If `AbcData` is not ambiguous, you can use `D` itself instead of the base class `B` to make the name dependent. That might be more straight-forward. – walnut Jan 08 '20 at 07:16
  • i'm using the `AbcData` multiple times so i used `using AbcData = B::AbcData`. it's working now. is there better alternative that is shorter and less specifying too much redundant details? i also have other data types from base class not just `AbcData`. – acegs Jan 08 '20 at 07:16
  • 2
    @acegs Your line should not compile. It needs a `typename` before `B` and you can use `using typename B::AbcData;` instead. – walnut Jan 08 '20 at 07:17
  • o yeah, i actually added `typename` in my actual code. i just typed manually the sample code in the comments. i forgot to add the `typename`. – acegs Jan 08 '20 at 07:19