Re: no ADL / Koenig lookup for function calls with template parameters
cgv ha scritto:
namespace test {
template <int i, typename T> void func(const T&) {...}
template <typename T> void func(int i, const T&) {...}
}
If we now declare a struct A inside the same namespace:
namespace test {
struct A { ... };
}
ADL should allow us to use func on an instance of A in the main function
without specifying the namespace test:
void main(int,char**) {
test::A a;
func<2>(a); // error under visual studio 8 (.Net 2005)
func(2,a); // this is fine
}
In this code the ADL does not work for the compile time version, where the
int is passed as template argument. Now my questions:
1. Is this a bug of the Visual Studio 8 compiler or is this use of ADL not
intended by the standard?
It's not a bug. You may find it surprising but the expression
"func<2>(a)" does *not* trigger ADL. 3.4.2/1 says that ADL is applied
only "when an unqualified name is used as the postfix-expression in a
function call". So:
1) you need to have an unqualified name: that's ok, we have "func"
2) that name shall be used as a postfix-expression in a function call:
hmmm... what we have here? "func<2"? But it's not a function call! No
function call, no ADL!
"func<2>" is not a name, it's a template-id, and ADL only works with
names, not template-ids. It's deliberately made to work that way. This
is the kind of situations that would make the parser too complex if they
were allowed. Bear in mind that "func<2" can a valid expression of
boolean type if, for example, "func" is the name of an int variable. I
guess this is the price to pay for having used "<>" to delimit template
parameters...
2. Has anyone a good idea of circumvent this problem without messing up the
notation too much?
Vladimir Marko's suggestion looks intriguing to me. Maxim Yegorushkin's
post is also insightful.
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]