0

In the code below I have a class template named zipcode, which has a member function template named get_postbox. It compiles in GCC but not in clang 3.9. Why doesn't clang accept this code?

In clang I get this error:

<source>:34:41: error: expected expression
return my_postoffice.get_postbox<K>();
^

Furthermore, calling the same code (same as zipcode::get_postbox) from a non-member function template named non_member_function_template() does not cause the error!

To see it for yourself and play with it, here's the code in Compiler Explorer: https://godbolt.org/g/MpYzGP


Here's the code:

template <int K>
struct postbox
{
    int val() const 
    {
      return K;
    }
};

template <int A, int B, int C>
struct postoffice
{
  postbox<A> _a;

  template<int I>
  postbox<I> get_postbox()
  {
    switch( I )
    {
      case A: return _a;
    }
  }
};

template <typename PO>
struct zipcode
{
  PO my_postoffice;

  template<int K>
  postbox<K> get_postbox()
  {
    // The error is on this line
    return my_postoffice.get_postbox<K>();
  }
};

// Here's a function template that isn't a member, and it compiles.
template<int D>
int non_member_function_template()
{
  postoffice<123,345,678> po;
  auto box = po.get_postbox<D>();
  return box.val(); 
}

int test_main()
{
  return non_member_function_template<123>();
}
Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106

1 Answers1

1

You need to use the template keyword when using template member functions like this:

my_postoffice.template get_postbox<K>()

and

po.template get_postbox<D>()

c.f. here: http://ideone.com/W0owY1 for the code and here: Where and why do I have to put the "template" and "typename" keywords? for the exact explanation when to use the template keyword

Community
  • 1
  • 1
midor
  • 5,487
  • 2
  • 23
  • 52