Topic: viable != callable?


Author: Stefan Schwarzer <sts@caracal.ica1.uni-stuttgart.de>
Date: 1999/02/03
Raw View
I posted a similar question to comp.std.c++ about a week ago, but did
not receive any comments. I think this question is also
appropriate for comp.lang.c++.

I am interested to know the rationale (or lack thereof) behind the
following behavior required by the C++ standard:

Given a template function

template<class T>
void f(T &){}

the attempt to call it with an array type argument fails

void g(){
 double d[] = {1.};
 f(d);
}

because the compiler will deduce as the signature of the function
(according to 14.8.2) to be instantiated and called

f(int *&){}

and it cannot pass the array as a reference to a pointer. Constness is
violated, since in f() the pointer could be changed.  I find it
truly bizarre that the standard thus implicitly mandates that
_compilation should fail_ in this case (and it does, e.g., on the edg based
compilers that I know - though not on egcs).

Instead the standard could require for template reference arguments
deduction as T=double * const or possibly reference binding with the
perfectly acceptable T=double [1].

I could imagine that the latter could produce some redundant code that is
better avoided, but then why not T=double * const ???
Since references and templates are involved, I also cannot see a deep
compatibility problem with C. So, what's the reason?

C++ never stops surprising me... ;)

--
Stefan Schwarzer                office:  +49-(0)711-685-7606   fax: x-3658
Uni Stuttgart, ICA 1            e-mail:          sts@ica1.uni-stuttgart.de
Pfaffenwaldring 27              pgp public key available on request/finger
70569 Stuttgart, Germany             http://www.ica1.uni-stuttgart.de/~sts
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/02/04
Raw View

Stefan Schwarzer wrote in message
<79a4v8$6eu$1@infosun2.rus.uni-stuttgart.de>...
[snip]
>C++ never stops surprising me... ;)


I guess it's you compiler. The call is resovled correctly by instantiating
the template function with the type double [1].

Andrei




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]