C++ 名称空间称为';例外情况';导致编译问题

C++ 名称空间称为';例外情况';导致编译问题,c++,exception,namespaces,C++,Exception,Namespaces,我对名为“exception”的命名空间有问题 让我们考虑下面的例子标题: #include <exception> namespace exception { struct MyException : public std::exception {}; } struct AnotherException : public exception::MyException { AnotherException() : exception::MyException()

我对名为“exception”的命名空间有问题

让我们考虑下面的例子标题:

#include <exception>

namespace exception
{
  struct MyException : public std::exception
  {};
}


struct AnotherException : public exception::MyException
{
    AnotherException() : exception::MyException() { }
};
2) 将命名空间重命名为,例如“Exception”

名称空间“异常”导致混淆的原因是什么?我知道有一个类std::exception。这会引起麻烦吗

我知道有一个类
std::exception
。这会引起麻烦吗


对。在
std::exception
中,非限定名称
exception
是注入的类名。这是继承的,因此在您的类中,一个非限定的
异常
引用该异常,而不是您的命名空间。

+1到@Mike Seymour的答案!作为补充,有比当前解决方案更好的方法来防止歧义:

只需使用
MyException
,无需任何名称空间限定:

struct AnotherException : public exception::MyException
{
    AnotherException() : MyException() { }
};

或者使用C++11继承的构造函数功能:

struct AnotherException : public exception::MyException
{
    using MyException::MyException;
};

struct AnotherException : public exception::MyException
{
    AnotherException() : MyException() { }
};
struct AnotherException : public exception::MyException
{
    using MyException::MyException;
};