相关疑难解决方法(0)

如何在C++中将枚举导入不同的命名空间?

我在命名空间中有一个枚举,我想使用它,就像它在不同的命名空间中一样.直观地说,我认为我可以使用'using'或'typedef'来实现这一目标,但实际上并不起作用.用于证明它的代码片段,在GCC和Sun CC上测试:

namespace foo
{

enum bar {
    A
};

}

namespace buzz
{
// Which of these two methods I use doesn't matter,
// the results are the same.
using foo::bar;
//typedef foo::bar bar;
}

int main()
{
    foo::bar f; // works
    foo::bar g = foo::A; // works

    buzz::bar x; // works
    //buzz::bar y = buzz::A; // doesn't work
    buzz::bar z = foo::A;
}
Run Code Online (Sandbox Code Playgroud)

问题是枚举本身是导入的,但没有导入.不幸的是,我无法将原始枚举更改为包含在额外的虚拟命名空间或类中,而不会破坏许多其他现有代码.我能想到的最好的解决方案是手动重现枚举:

namespace buzz
{
enum bar
{
    A = foo::A
};
}
Run Code Online (Sandbox Code Playgroud)

但它违反了DRY原则.有没有更好的办法?

c++ enums typedef namespaces language-lawyer

28
推荐指数
3
解决办法
1万
查看次数

标签 统计

c++ ×1

enums ×1

language-lawyer ×1

namespaces ×1

typedef ×1