Boost logo

Boost :

From: Jens Maurer (jmaurer_at_[hidden])
Date: 2000-03-07 06:29:59


Stephen Cleary wrote:
> So the problem is: BCB chokes on the following code:
>
> template <typename T>
> void f(T & x) { }
>
> void test()
> {
> int tmp;
> f(tmp); // works OK: calls f<int>(int & x);
> f(13); // chokes: does NOT find f<const int>(const int & x);
> }
>
> This may be just a problem with BCB, because they do not consider
> 'const' as part of the type. I've briefly looked in the Standard, but
> it's confusing me, and I can't prove that the above is conformant.
> Anyone else want to try? Will this compile on other compilers?

It doesn't compile on gcc 2.95.2, either ("cannot initialize
non-const reference int& from rvalue of type int").

> *Should* it?

No. Reading std:2.13.1 [lex.icon], it says that decimal integer
literals without a suffix have type "int" or "long int".
std:14.8.2.1/2 [temp.deduct.call] says "if P [in this case, T&] is a
reference type, the type referred to by P is used for type deduction."
So we have a simple "T" we need to fulfill for type deduction, which
yields "int". But, we cannot bind the int literal to the non-const
reference.

The case "if the original P is a reference type, the deduced A
(i.e. the type referred to by the reference) can be more
cv-qualified than A [the actual argument type, int in this case]"
(this is what we would need, since we want "const int" for T) will
be "considered only if type deduction would otherwise fail", which
is not the case.

Thus, I think the compilers are correct and you may have to add a
template function overload for "const T&".

Jens Maurer


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk