ctta0s
ctta0s

Reputation: 53

Template keyword for dependent types in C++11

This is an even more in-depth follow-on to: this question

Consider the following code:

template <typename T>
class A {
 public:
  template <typename T2>
  const T2* DoSomething() { ... }
};

template <typename T>
class B : public A<T> {
 public:
  const int* DoSomethingElse() {
    return this->DoSomething<int>();  // Compiler wants 'template' keyword here:
 // return this->template DoSomething<int>();
  }
};

Why doesn't this compile? I understand that the relevant section of the standard is 14.2/4, but I'm not sure I understand the nuts and bolts of why this doesn't work. Can someone break down the wording in that section to explain why this doesn't work? Additionally, can you describe (generally) under what circumstances the template keyword can be omitted?

Note that in C++11 the following code does compile:

template <typename T>
class A {
 public:
  template <typename T2>
  const T2* DoSomething() { ... }
};

class B {
 public:
  scoped_ptr<A<int>> var_;

  const int* DoSomethingElse() {
    return var_->DoSomething<int>();
  }
};

What's the difference?

Upvotes: 5

Views: 867

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477610

Suppose you have this:

template <> class A<float>
{
    int DoSomething;
    // ...
};

Suddenly the expression this->DoSomething < ... means something very different. There's no way to determine what a given name means if it is a dependent name. That's why you have be explicit about whether a name is a variable, a typename or a template.

Upvotes: 3

Ben Voigt
Ben Voigt

Reputation: 283901

It's because C++ is not a context-free grammar.

Normally, the compiler looks at previously declared symbols to decide whether the angle brackets in the token sequence DoSomething, <, int, > are relational operators or part of a template name. Since this is a template, and it isn't yet known whether A<T> will be specialized, the compiler cannot rely on prior declarations in the symbol table and needs help from the programmer.

Upvotes: 3

Related Questions