Il giorno 27/giu/2012, alle ore 10:55, Keean Schupke ha scritto:
> Can you explain to me what technical problem requires the code not to run?
Because it doesn't compile? :-D More seriously, even "compile" is not the correct term here. It's not that code "does not run" nor that it "does not compile" but rather that the code "is ill-formed". An ill-formed program may still compile and run, but if it does, it would have an undefined behavior.
> I am interested in why the standard is worded to exclude this code. The standard currently allows this if the type is an enum for example:
>
>
> template <typename T> T f(T t) {
> return g(t);
> }
> enum E {e};
> int g(E x) {
> return x + 1;
> }
> main() {
> E x(e);
> x = f(x);
> }
>
>
> This code works (according to the standard).
Yes. This code is well-formed, because E is not a fundamental type, so it has an associated namespace (the global namespace, in this case).
> Why are we treating one type (enum, class) differenly from others (int, float). What is the reason for treating them differently?
Fundamental types do not have associated namespaces. That is the key point. So the instantiation-point lookup, which only looks in the associated namespaces, finds nothing. Frankly, I agree that it might have made more sense to have the global namespace be associated with the fundamental types. In that case, the lookup would find g() and the code would be well-formed. However, it was like this since the beginning, so I have to presume that either there is a valid reason or it was a mistake that created a precedent which couldn't be corrected for backward compatibility issues.
For example, consider the code:
class A {
public:
A(int) {}
};
int g(A) {
return 0;
}
template <typename T> T f(T t) {
return g(t);
}
int g(int x) {
return x + 1;
}
int main() {
int x(1);
std::cout << f(x) << "\n";
}
This is well-formed in C++03 and C++11 and produces the output "0". If we were to change the associated namespaces of the fundamental types to include the global namespace, the code would still be well-formed but would produce the output "2". From the point of view of the standard committee, a silent change in the behavior of a well-formed program is usually unacceptable. It may be acceptable only if the benefits significantly outweighs the potential backward compatibility issues. I don't think that's the case, here.
So my point is: I don't know why this decision was taken, but it seems anyway too late to change direction.
Ganesh