function template explicit specialization not chosen
{ Please limit your lines to 70 characters - mod }
in the code below
++++++++++++++++++++++++++++++ example-1 ++++++++++++++++++++++++++++++
++++++++
struct dummy { };
template <typename T> int hash (T);
template <typename T> int hash (T t) { return t; } // (1)
template <> int hash (dummy const&) { return 0; } // (2)
int main ()
{
dummy a;
dummy const&b = a;
hash(b); // expect (2) called
return 0;
}
-------------------------------------------------------------------------------
my compiler seems to select the primary function template and that
caused a
compiler error of not being able to convert dummy to int. comeau
online test
compiler also generates the same error. In either case, the
specialization is
not chosen.
If I comment out (1) as in the next example
++++++++++++++++++++++++++++++ example-2 ++++++++++++++++++++++++++++++
++++++++
struct dummy { };
template <typename T> int hash (T);
// template <typename T> int hash (T t) { return t; } // (1)
template <> int hash (dummy const&) { return 0; } // (2)
int main ()
{
dummy a;
dummy const&b = a;
hash(b); // expect (2) called
return 0;
}
-------------------------------------------------------------------------------
g++ then says undefined reference and this means it still selected the
primary
template. but comeau online this time compiles just fine and this
seems to
suggest it used the specialization.
My question is :
(1) What does the standard (c++11) say about the call to hash with b,
should T
be deduced to be dummy const&, or should it be dummy const?
(2) Is the specialization in the candidate function set at all?
(3) For the difference between g++ and comeau as in example-2, which
one is more
standard compliant?
Please note that I do know solutions, but I am only interested in why
the
specialization is not chosen and what should T be deduced in this
particular
context. I would like to see explanations referring to the standard (c+
+11)
which regulates the behaviors in both examples.
My test environment is ubuntu 10.04 with g++-4.7.0 and the comeau
online
compiler is http://www.comeaucomputing.com/tryitout/
Thank you in advance.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]