PatrykB
PatrykB

Reputation: 1699

template class alias inside other class body produces compilation error

I am working on Visual Studio 2015 community edition.

The following code produces an error:

#include <list>
using std::list;

template <typename T>
class enumerator
{
// typedef: 
public:
    using list_      = list<T*>;                        // line:
    using list_fwd_c = list_::const_iterator;           // 188
    using list_fwd   = list_::iterator;                 // 189
    using list_rvs_c = list_::const_reverse_iterator;   // 190
    using list_rvs   = list_::reverse_iterator;         // 191
};                                                      // 192

and the error:

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(188): error C2061: syntax error: identifier 'const_iterator'

1>  d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(192): note: see reference to class template instantiation 'cpplib::eval::utils::enumerator<T>' being compiled

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(188): error C2238: unexpected token(s) preceding ';'

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(189): error C2061: syntax error: identifier 'iterator'

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(189): error C2238: unexpected token(s) preceding ';'

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(190): error C2061: syntax error: identifier 'const_reverse_iterator'

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(190): error C2238: unexpected token(s) preceding ';'

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(191): error C2061: syntax error: identifier 'reverse_iterator'

1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(191): error C2238: unexpected token(s) preceding ';'

I have confirmed this is the troublesome code piece (error disappear when I comment out the 'enumerator' class).

As its name suggests, it "will be" a simple enumerator class (something like a IEnumerator from C#. I will use it in order to store a list of objects derived from a base class passed as a class template parameter T.

Question:
1. Why the code above produces this error.
2. How can I fix this error?
3. I know this is not a place for this, but if you know a good implementation (boost ?) for this kind of enumerator, please point me in the right direction.

Upvotes: 2

Views: 210

Answers (1)

AndyG
AndyG

Reputation: 41100

You still need to add a typename despite how convenient using is:

using list_fwd_c = typename list_::const_iterator;           // 188
using list_fwd   = typename list_::iterator;                 // 189
using list_rvs_c = typename list_::const_reverse_iterator;   // 190
using list_rvs   = typename list_::reverse_iterator;         // 191

The problem is that these are dependent types (they depend on the template parameter) so the compiler cannot just know they are all types (and not, say functions or static data members)

Upvotes: 4

Related Questions