This question is a continuation. Moving a member function from a base class to a derived class breaks the program for no apparent reason (this is a prime example of why you cannot use using namespace std; )
where the answers suggested qualifying this-> dependent template name (which really is a way to go when accessing such dependent members). However, it seems to be a problem, so I have listed a minimal example that reproduces the problem.
Consider the code:
#include <iostream> #include <bitset> using namespace std; template<class T> struct B { T bitset{}; }; template<class T> struct D : B<T> { bool foo() { return this->bitset < 32; } }; int main(){}
Live on coliru
Surprisingly, even if this->bitset should refer to the B<T>::bitset element, the compiler is still confused and believes that we are trying to access std::bitset<std::size_t> . The error appears on both gcc6 and clang3.7. Any ideas why this is happening? Qualifying it with B<T>::bitset works though.
Error (verbatim):
In member function 'bool D<T>::foo(T, std::__cxx11::string)': cpp/scratch/minimal.cpp:24:22: error: invalid use of 'class std::bitset<1ul>'
EDIT
It looks like a parsing / name lookup error. If we replace < with any other comparison operator (thanks @Leon for the remark), for example
return this->bitset == 32;
the program is compiled. Therefore, I believe that in this->bitset < 32 analyzer believes that we are trying to create a template ( < sign), and we forgot to close > . But again, I canβt imagine if this is really a mistake or that the language should work.
c ++ c ++ 11 templates
vsoftco Nov 07 '16 at 17:06 2016-11-07 17:06
source share