A
Alexander Stippler
Hi,
I have got a question concerning the overload resolution rules of C++ and
enumeration types. The following little test program illustrates the
situation:
enum { red, green, blue };
template <class A, class B>
A
operator+(const A &a, const B& b)
{
return a;
}
int
main()
{
int i = 0;
i = i + green;
return 0;
}
The code does not really make much sense, but illustrates my problem: I
would never have guessed that every compiler I tried uses the template
operator+ instead of a builtin +.
IMO the compiler has to prefer the builtin + according to the standard:
1) both the user-defined + and the builtin + are in the candidate set.
[13.3.1/13.3.2] and [13.6.2/13.6.12].
2) both are viable.
3) but the built-in + is better [13.3.3].
By the way, I have neglected the fact, that the enum is an unnamed type, so
that the compiler should not choose the template version anyway (or should
it?)
Are my considerations wrong or the compilers (gcc3.3, como4.3, icc7.01)?
regards,
alex
I have got a question concerning the overload resolution rules of C++ and
enumeration types. The following little test program illustrates the
situation:
enum { red, green, blue };
template <class A, class B>
A
operator+(const A &a, const B& b)
{
return a;
}
int
main()
{
int i = 0;
i = i + green;
return 0;
}
The code does not really make much sense, but illustrates my problem: I
would never have guessed that every compiler I tried uses the template
operator+ instead of a builtin +.
IMO the compiler has to prefer the builtin + according to the standard:
1) both the user-defined + and the builtin + are in the candidate set.
[13.3.1/13.3.2] and [13.6.2/13.6.12].
2) both are viable.
3) but the built-in + is better [13.3.3].
By the way, I have neglected the fact, that the enum is an unnamed type, so
that the compiler should not choose the template version anyway (or should
it?)
Are my considerations wrong or the compilers (gcc3.3, como4.3, icc7.01)?
regards,
alex