P
Piotrek
Below are two short programs:
1)
template<class T> void foo(){}
int main()
{
&foo<int>;
return 0;
}
2)
template<int N> void foo() {int i = N;}
int main()
{
&foo<4>;
return 0;
}
None of them compiles when tested here:
http://codepad.org/
and compilation fails with "error: statement
cannot resolve address of overloaded function"
(which can be bypassed for program 2. with
(void(*)())&foo<4> instead of &foo<4>)
while both compile when tested here:
http://www.compileonline.com/compile_cpp11_online.php
Two things:
1) I realize it's caused by a rule saying a template
name is considered to always name a set of overloaded
functions (hence the ambiguity), but is there any reason
behind it other than "because the standard says so"?
To me it looks like in each case there's only one
possible instantiation with a given type/value, so where
does the ambiguity come from? After all, what's
the difference between my example and perfectly valid:
template<class T> void foo(){}
int main()
{
foo<int>();
return 0;
}
?
2) Why do the examples compile fine with the second
compiler? Is the discussed rule loosened or removed
from C++11 standard?
1)
template<class T> void foo(){}
int main()
{
&foo<int>;
return 0;
}
2)
template<int N> void foo() {int i = N;}
int main()
{
&foo<4>;
return 0;
}
None of them compiles when tested here:
http://codepad.org/
and compilation fails with "error: statement
cannot resolve address of overloaded function"
(which can be bypassed for program 2. with
(void(*)())&foo<4> instead of &foo<4>)
while both compile when tested here:
http://www.compileonline.com/compile_cpp11_online.php
Two things:
1) I realize it's caused by a rule saying a template
name is considered to always name a set of overloaded
functions (hence the ambiguity), but is there any reason
behind it other than "because the standard says so"?
To me it looks like in each case there's only one
possible instantiation with a given type/value, so where
does the ambiguity come from? After all, what's
the difference between my example and perfectly valid:
template<class T> void foo(){}
int main()
{
foo<int>();
return 0;
}
?
2) Why do the examples compile fine with the second
compiler? Is the discussed rule loosened or removed
from C++11 standard?