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>();
}