Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloading an operator with templates in global namespace is just evil. You shall not do that. #40

Open
GoogleCodeExporter opened this issue Apr 16, 2015 · 7 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
Suppose a template function in global  namespace:
template <class Enum>
inline bitwise_enum<Enum> operator | (Enum a, Enum b)
{
    return bitwise_enum<Enum>(a) | bitwise_enum<Enum>(b);
}


This will overload and instantiate any operators with arbitrary type of 
operands:

#include "bitwise_enums.h"

enum E {A = 1, B = 2};

int main (int argc, const char * argv[])
{
    int x = A | B;
}


What is the expected output? What do you see instead?
It should compile.
The above would not compile because it selects the above template, and there is 
no conversion from bitwise_enum to int. Anyway, it should not select this 
operator at all.


Please provide any additional information below.
There is no easy solution to this problem, unless using strong type safe enums 
proposed in C++0x.
A more elaborated solution would involve to use traits for selected types of 
enums and use these traits to enable the operators defined in global namespace. 
Using boost type traits and boost utility library may help to implement this.

Btw, embedding the bitwise_enum into a namespace does NOT solve the problem.


Original issue reported on code.google.com by [email protected] on 31 Aug 2011 at 2:06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant