1

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

Here is the fragment code:

template <typename alloc_t>
int Protocol_v2<alloc_t>::create(..., alloc_t *alloc, ...) {
    Protocol_v2<alloc_t> * pack = alloc->template malloc<Protocol_v2<alloc_t> >();

Protocol_v2 is a template class, as follows:

    template <typename alloc_t>
class Protocol_v2  { ...}

alloc_t is a class, as follows:

class reverse_allocator { 
...
template<typename T>
    inline T * malloc() {}
...
}

What is bothering me is this line:

Protocol_v2<alloc_t> * pack = alloc->template malloc<Protocol_v2<alloc_t> >();

What is that mean? I haven't seen that in c++ primer so far.

Thanks in advance.

Community
  • 1
  • 1
zhanglistar
  • 447
  • 4
  • 15
  • 2
    It's because `malloc` is a dependent name here. There isn't anything C++11 specific about it though. – Flexo Sep 18 '12 at 13:26

1 Answers1

1

Sounds like you're getting thrown off by the "template" keyword showing up in the middle of the line, specifically the call

alloc->template malloc<Protocol_v2<alloc_t> >();

The point is that you wanted to do this:

alloc->malloc<Protocol_v2<alloc_t> >();

... but the parser doesn't know that the malloc member of alloc_t is supposed to be a template, so it will error out. Adding the template keyword gives it just enough information to continue.

This is explained more extensively in the link Flexo posted in the comments to the original question.

Nathan Monteleone
  • 5,430
  • 29
  • 43