I'm just going to bump this in case a C++ wizard might see it and know if there's actually magic syntax to allow such a construct without templates. Solely for curiosity.
#include <iostream>
enumclass L;
struct TestClass {
void foo(L n) {
int v=static_cast<int>(n);
int r[v] = {9};
std::cout<<"\n"<< v <<"\n";
}
};
void g() {
enumclass L : int { x, y, z, A, B, C};
TestClass g;
g.foo(L::D);
}
int main(){
enumclass L : int {
A, B, C, D
};
TestClass main;
main.foo(L::D);
return 0;
}
Without templates, types are bound at compile time. So which enum class L should TestClass::foo() take as parameter? Is it ::L, foo::L or main::L? There can be only one.
#include <iostream>
template <typename EnumClass>
struct TestClass {
void foo(EnumClass n) {
int v=static_cast<int>(n);
int r[v] = {9};
std::cout<<"\n"<< v <<"\n";
}
};
void g() {
enumclass L : int { x, y, z, A, B, C};
TestClass<L> g;
g.foo(L::A);
}
int main(){
enumclass L : int {
A, B, C, D
};
TestClass<L> main;
main.foo(L::D);
return 0;
}
I'm curious if there's a way to specify the correctly scoped type without using templates. GCC's error message is interesting, because it implies the scoped type name is "main()::L", but this is not valid C++ syntax in this context.
it works fine for me if you just replace the forward decl of L with the enum class itself and take that out of main. (This gets it done without a template, but isnt what was wanted?) It seems to be a scope problem, a weird one. There isn't any way I could find to say main::L in foo().
Honestly this may be a compiler bug? It can resolve that main::L is the L in question for foo() well enough until it tries to call the function, then it gets scope scrambled. I am clearly not the best with language things but one of those two things seems wrong: either it should not be able to resolve what L is for foo at all, or it should not be confused by the fact that L is scoped in main.
It compiles fine if you take the call to foo out, as well, is what I meant. That should not happen if it can't resolve the scope for L. If it can resolve it at that level, it should not struggle with the call.