Leave out the enum class name from function arguments

I now this isn't possible but I think it would be useful if it was.

If you have an enum class
1
2
3
4
5
6
7
enum class Direction
{
	up,
	down,
	left,
	right
};

and a function that takes the enum class as argument
void foo(Direction dir);

When you pass a Direction to this function you have to write Direction::, which is one of the good things about enum class
foo(Direction::up);

but in this case I feel that it shouldn't be needed. We know that the argument is of type Direction so we shouldn't have to specify.
foo(up);


There could be a problem if there was another function
void foo(int i);

and a local variable
int up;

Now calling foo(up); would be ambiguous but I think it's better that it calls the int version of foo because that is what would be called with existing rules.

Now it would be confusing if calling the function as
1
2
foo(up);
foo(down);

calls two different functions so to avoid that I think there should be a rule that says that you can only leave out Direction:: if there is no overloaded function with the same number of arguments that has a different parameter type at that position.
1
2
3
4
5
6
7
8
9
10
11
void foo(Direction);
void foo(int);

foo(down)                   // error, if there is no variable named down.

void foo(Direction, Direction);
void foo(Direction, int);

foo(down, 5);               // OK, calls foo(Direction, int).
foo(down, left);            // error, if there is no variable named left.
foo(down, Direction::left); // OK, calls foo(Direction, Direction). 


What do you think? Something for the next C++ standard?
What about such code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum class Direction
{
	up,
	down,
	left,
	right
};

enum
{
	up,
	down,
	left,
	right
};

void f( Direction ) {}
void f( int ) {}

f( up ); // Can you say seeing this call what function was called? 



EDIT:Even this code

1
2
foo(down, left);            // error, if there is no variable named left.
foo(down, Direction::left); // OK, calls foo(Direction, Direction).   



is very confusing. Seeing the call foo(down, left); I can not say what are down and left that is whether they are some integer variables or of type enum.
Last edited on
vlad from moscow wrote:
f( up ); // Can you say seeing this call what function was called?

Yes, it will call f(int). You would have to use Direction:: here if you want to call f(Direction), exactly like in C++11.

vlad from moscow wrote:
is very confusing. Seeing the call foo(down, left); I can not say what are down and left that is whether they are some integer variables or of type enum.

You can still call the function as foo(Direction::down, Direction::left); if you want. If it's not clear it's probably best to do it that way. I think what I have suggested is more useful when you have functions that doesn't have this overloaded ambiguity. If you have a function setDirection it is obvious that I'm passing is a direction. setDirection(up); is crystal clear. Calling the function as setDirection(Direction::up); I have repeated the word Direction twice which feels a bit redundant.
Last edited on
In my opinion your proposal makes C++ code hard to read. Not all function names look like setDirection.:) And even if a function has such a name it is not obvious that its parameter has type of Direction.:)
Topic archived. No new replies allowed.