On 23/11/2017 01:11, Ireneusz Szcześniak wrote:
However, it bugs me that I'm still not sure whether I've implemented a function-template specialization or a template for function overloading. You claim that this is an overload:
template <typename Graph> auto get_cost(const Label1<Graph> &l) { return std::get<0>(l); }
Could you please elaborate on how to tell one from the other? Had it been a specialization, a compiler should accept the explicit template argument:
template <typename Graph> auto get_cost
(const Label1<Graph> &l) { return std::get<0>(l); } but I get (with gcc version 7.2.0):
error: non-class, non-variable partial specialization ‘get_cost
’ is not allowed get_cost (const Label1<Graph> &l) So it seems that I was defining overloads (these don't have template arguments), not specializations as I thought. The C++ Programming Language, 4th edition, the bottom of page 737, says that you can drop the explicit template argument in the definition of a template specialization, if it can be deduced. So I dropped the explicit template argument, and still considered the definition the specialization.
I have a hard time understanding the above error message, thought. I understand that "non-class" simply means that it's not a member function, OK. But what "non-variable partial specialization" could possibly mean? What is "non-variable" here, and why "partial specialization" when I gave all (i.e., one) template argument. I consider it a complete specialization albeit dependent on a template parameter Graph.
I suggest reading http://www.gotw.ca/publications/mill17.htm. The short version is that function templates are a bit weird, and if you have anything other than template<> it's an overload, because they don't do partial specialization.