Skip to content

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

Open

Description

@GoogleCodeExporter
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 agro...@onlinehome.de 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions