Pass enum in a scope to another as function argument

How pass enum in a scope to another as function argument ?
Why is it fail ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

enum class L;

struct TestClass {

void foo(L n)	{
	int v=static_cast<int>(n);
	int r[v] = {9};
	cout<<"\n"<< v <<"\n";
}

};


int main(){

	enum class L : int	{
		A, B, C, D
	};

	TestClass main;
	main.foo(L::D);
 
	return 0;
}



 error: cannot convert ‘main()::L’ to ‘L’
   80 |  main.foo(L::D);
      |           ~~~^
      |              |
      |              main()::L


How to solve this (exact place, not move enum to a scope else) ?
Last edited on
You could use a template declaration
1
2
template <typename T>
void foo(T n)	{

But I'm not sure what the syntax would to declare the actual type, if it's possible.

Note that your line 8 is using a GCC-extension VLA.
Last edited on
why not move the enum out to the global scope (and if for a large project, put a namespace on it) -- or use the enum class?
Last edited on
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.
Last edited on
abdulbadii, suppose the language allowed what you want. Consider this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

enum class L;

struct TestClass {

    void foo(L n) {
	int v=static_cast<int>(n);
	int r[v] = {9};
	std::cout<<"\n"<< v <<"\n";
    }
};


void g() {
    enum class L : int { x, y, z, A, B, C};
    TestClass g;
    g.foo(L::D);
}

			
int main(){

    enum class 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.

You can do it with a template:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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() {
    enum class L : int { x, y, z, A, B, C};
    TestClass<L> g;
    g.foo(L::A);
}

			
int main(){

    enum class 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.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.