enum, declaration outside of namespace and namespace causes "call of overloaded (…) is ambiguous"

  c++, enums, namespaces

Take the following:

foo.h

#ifndef FOO_H
#define FOO_H

#include <stdint.h>

enum directions {
    NORTH,
    EAST,
    SOUTH,
    WEST
};

bool foo_a(uint16_t a);
bool foo_b(enum directions a);

#endif //FOO_H

foo.cpp

#include "foo.h"

#include <iostream>

namespace test {
    bool foo_a(uint16_t a) {
        return a < 22;
    }

    bool foo_b(enum directions a) {
        return a < WEST;
    }

    bool foo(void) {
        return foo_a(16) || foo_b(NORTH);
    }
}

int main(void)
{
    std::cout << test::foo();
    return 0;
}

If we attempt to compile this, we get errors:

g++ foo.cpp
foo.cpp: In function ‘bool test::foo()’:
foo.cpp:17:40: error: call of overloaded ‘foo_b(directions)’ is ambiguous
   15 |         return foo_a(16) || foo_b(NORTH);
      |                                        ^
foo.cpp:12:10: note: candidate: ‘bool test::foo_b(directions)’
   10 |     bool foo_b(enum directions a) {
      |          ^~~~~
In file included from foo.cpp:2:
foo.h:14:6: note: candidate: ‘bool foo_b(directions)’
   14 | bool foo_b(enum directions a);
      |

So the compiler complains about an overloaded foo_b, but not about an overloaded foo_a. Why does it complain about foo_b at all? Why does it not just select the one in this namespace? And why is the enum treated different from the uint16_t (there are no errors about foo_a)?

Source: Windows Questions C++

LEAVE A COMMENT