Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two namespaces only works partly

When I try to compile this code:

#include <iostream>

namespace Direction
{
    enum Enum { UP, DOWN, LEFT, RIGHT };
}

using namespace std;

void move(int pDir);

int main()
{
    printf("UP: %u\n", Direction::UP);
    printf("DOWN: %u\n", Direction::DOWN);
    printf("LEFT: %u\n", Direction::LEFT);
    printf("RIGHT: %u\n", Direction::RIGHT);

    move(Direction::UP);

    return 0;
}

void move(int pDir)
{
    printf("Move: ");
    switch(pDir) 
    {
        case(Direction::UP):
            printf("UP");
            break;
        case(Direction::DOWN):
            printf("DOWN");
            break;
        case(Direction::RIGHT):
            printf("RIGHT");
            break;
        case(Direction::LEFT):
            printf("LEFT");
            break;
        default:
            printf("nothing");
            break;
    }
}

The result I expect would be:

UP: 0
DOWN: 1
LEFT: 2
RIGHT: 3
Move: UP

Instead the result is:

UP: 0
DOWN: 1
LEFT: 2
RIGHT: 3

It seems like void move(..) just gets ignored.

I already found the problem: It's the using namespace std. When I delete it, I get the result as expected.

So I have three questions:

1) Why does void move(..) just gets "ignored"

2) Why can I access the members of Direction within the first four lines of int main()

3) How can I fix this? ,_,

Have a nice day my friends.

ps: This is an extracted example of my problem, on my project I need to use the namespace std.

like image 819
l. schwarz Avatar asked Jun 17 '26 21:06

l. schwarz


1 Answers

Your move function application has been reviewed and unfortunately, a better application has been found. Please don't hesitate to reach us for comments or questions.

std::move in utility header is the function being called in this situation. This is due to multiple factors :

  • The utility header is included by iostream, so it is defined in your code
  • Because of using namespace std, any function definition in the std namespace is now candidate for overload resolution in the global scope (see Namespace scope).
  • The argument Direction::UP is a rvalue, and the signature of std::move is template< class T >constexpr typename std::remove_reference<T>::type&& move( T&& t ) noexcept;. T can resolve to the enum type without conversion.
  • Yours is void move(int pDir); and will require an implicit conversion from Direction::Enum&& to int

When you remove on of these condition your function will be called. For instance

move((int)Direction::UP);

remove the need for an implicit conversion.

like image 171
UmNyobe Avatar answered Jun 19 '26 11:06

UmNyobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!