-4

In Clang compiling this gives me error

template<typename TYPE> struct Base
{
   bool variable;
};
template<typename TYPE> struct Ext : Base<TYPE>
{
   void clear() {variable=false;} // <- error here
};

error: use of undeclared identifier.

From Why do I have to access template base class members through the this pointer?, I know a workaround is to use "this->variable", but that's painful to use always, is there any compiler flag to disable this?

I'm looking for a Clang Compiler Flag option to disable this error completely. On MSVC you can do that with "/permissive" command line flag. I was looking for similar option on Clang, and the "-fpermissive" flag doesn't work.

cigien
  • 57,834
  • 11
  • 73
  • 112
Esenthel
  • 499
  • 4
  • 17

2 Answers2

1

(Correction: apparently, some compilers allow disabling this error, see OP above).

In this context variable is a dependent name because it is part of Base<T> which depends on the template parameter. This is a language requirement.

You can place using Base<Type>::variable in your derived class, i.e.

template<typename TYPE> struct Ext : Base<TYPE>
{
  using Base<TYPE>::variable;  // <- do this once per class

  void clear() {
    variable = false;          // <- this is fine now
  }
};
Cedric
  • 278
  • 1
  • 9
  • 1
    Yes you can, on MSVC compiler you have an option "/permissive" that solves this problem. Clang gives you -fms-extensions, and -fpermissive but that didn't help – Esenthel Dec 10 '20 at 14:00
  • Thanks for the correction, bad phrasing from my part. I meant it as in you can only disable "warnings considered as errors/not considered as errors". That being said, the solution above is the best I could think of. – Cedric Dec 10 '20 at 14:06
  • Thank you, I'm aware of that, however what I'm going for, is not having to do this at all, because imagine you have 100 variables or methods, writing "using" or extra "this->" that's a huge waste of time IMO. I think compilers should make life easy for programmers, not extra hard. – Esenthel Dec 10 '20 at 14:09
  • @Esenthel : That's fair. One option would be to have one class with all non-templated, shared variables and a separate templated class. Then, you can inherit from both. I tend to avoid multiple inheritance if possible, but it would be an alternative. – Cedric Dec 10 '20 at 14:16
-1

It will also work if you use this->variable instead of variable in the subclass because that changes the lookup rules of variable.

Zac Wimer
  • 89
  • 5