Topic: Rationale for 14.3.2/5 [temp.arg.nontype], pointer to object non-type template-parameters
Author: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Wed, 7 Mar 2007 12:12:35 CST Raw View
On 7 Mrz., 18:37, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
> Daniel Kr gler ha scritto:
> > So, if VS2005 compiles this successfully (modulo function definition),
> > it behaves non-conforming in this regard (Even VS2005-SP1
> > does accept this program).
>
> Interesting. How does VS2005 compile this, then?
>
> struct base{};
> struct derived:base{};
>
> template<base*> void foo() {}
> template<derived*> void foo() {} // overload!
>
> derived d;
>
> int main()
> {
> foo<&d>(); // should unambigously select foo<derived*>
>
> }
>
> For the OP: this example might show the rationale that you were asking.
> If you allow derived-to-base conversion, foo<derived*> can never be
> called, unless complex "best-match" rules are further introduced to
> disambiguate this scenario. I've never seen this case used in practice,
> so I guess the committee decided that writing down those rules just
> wasn't worth the effort.
Good point. VS2005-SP1 behaves exactly as you predict:
"error C2668: 'foo' : ambiguous call to overloaded function
main.cpp(5): could be 'void foo<& d>(void)'
main.cpp(4): or 'void foo<& d>(void)'
while trying to match the argument list '(void)'"
Greetings from Bremen,
Daniel
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: "=?iso-8859-1?B?Sm9hcXXtbiBNIEzzcGV6IE118W96?=" <joaquin@tid.es>
Date: Mon, 5 Mar 2007 16:26:23 CST Raw View
Hello,
What is the reason for the standard to not allow common conversions
like derived-to-base for non-type template-parameters of type pointer
to
object?
struct base{};
struct derived:base{};
template<base*> void foo();
derived d;
int main()
{
// error: no instance of function template "foo" matches the
// argument list
foo<&d>();
}
Thank you in advance,
Joaqu n M L pez Mu oz
Telef nica, Investigaci n y Desarrollo
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: "h.yuzhao@gmail.com" <h.yuzhao@gmail.com>
Date: Tue, 6 Mar 2007 01:35:28 CST Raw View
Did you forgot to define the function body of the foo()? If so, you
will get a linking error, not compilation error. I tested your code in
VS2005 and got the linking error. After defining the foo(), every
thing gone well.
"Joaqu n M L pez Mu oz
"
> Hello,
>
> What is the reason for the standard to not allow common conversions
> like derived-to-base for non-type template-parameters of type pointer
> to
> object?
>
> struct base{};
> struct derived:base{};
>
> template<base*> void foo();
>
> derived d;
>
> int main()
> {
> // error: no instance of function template "foo" matches the
> // argument list
> foo<&d>();
> }
>
> Thank you in advance,
>
> Joaqu n M L pez Mu oz
> Telef nica, Investigaci n y Desarrollo
>
>
> ---
> [ 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://www.comeaucomputing.com/csc/faq.html ]
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Tue, 6 Mar 2007 08:36:18 CST Raw View
On Mar 6, 8:35 am, "h.yuz...@gmail.com" <h.yuz...@gmail.com> wrote:
> Did you forgot to define the function body of the foo()? If so, you
> will get a linking error, not compilation error. I tested your code in
> VS2005 and got the linking error. After defining the foo(), every
> thing gone well.
Strictly speaking you are right. As I learned recently, using an
undefined
entity belongs to the UB realm.
But even if he would fix this issue by simply providing a proper
definition,
e.g.
template<base*> void foo() {}
this program should not compile (as Joaqu n correctly pointed out),
because of what 14.3.2/5 demands:
"for a non-type template-parameter of type pointer to object,
qualification
conversions (4.4) and the array-to-pointer conversion (4.2) are
applied.
[ Note: In particular, neither the null pointer conversion (4.10) nor
the
derived-to-base conversion (4.10) are applied.[..]"
So, if VS2005 compiles this successfully (modulo function definition),
it behaves non-conforming in this regard (Even VS2005-SP1
does accept this program).
Greetings from Bremen,
Daniel Kr gler
---
[ 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://www.comeaucomputing.com/csc/faq.html ]