The question in the title may sound trivial, so I better explain with some code what I want to do...
In C++11 I can do this:
#include <iostream>
namespace X {
enum class FOO { A,B };
}
template <typename T> void foo(T t) {
if (t == T::A) { std::cout << "A"; }
}
int main() {
foo(X::FOO::A);
}
The important point here is that the template foo does not need to know in what namespace the enum is declared. I could as well call foo(Y::FOO::B) (provided there is a enum class called FOO in namespace Y having members A and B).
Now the question is: How to get the same with plain old enums (and only C++98 stuff)?
This works:
#include <iostream>
namespace X {
enum FOO { A,B };
}
template <typename T> void foo(T t) {
if (t == X::A) { std::cout << "A"; }
}
int main() {
foo(X::A);
}
but only because foo knows in what namespace the enum is declared. And it wont work for a Y::FOO::B ! (In C++11 it also works if I replace the line with if (t == T::A) ..., even with a plain enum)
Is there a way to get this working in C++98/03 without refering to X in the template explicitly?
For the sake of completeness, in C++98, this
template <typename T> void foo(T t) {
if (t == T::A) { std::cout << "A"; }
}
results in
error: ‘A’ is not a member of ‘X::FOO’
PS: I am not allowed to change the enum and the template has to live in a different namespace than the enum.
PPS: a simple if (t == 0) would probably work, but thats something I would like to avoid