【发布时间】:2014-08-04 11:05:05
【问题描述】:
给定一个 C++11 枚举类,嵌套在几个长而丑陋的命名空间中:
namespace
long_and_ugly
{
enum class
colour
{
red,
green,
blue
};
}
可以为枚举值制作别名吗?使用 clang++ 3.5,可以执行以下操作:
using long_and_ugly::colour; // take all the values into the current namespace
using long_and_ugly::colour::red; // take only 'red' into the current namespace
function_taking_colour_argument( red ); // instead of fully referring to the value
g++ 4.9 然而,抱怨。我无法复制它的错误消息,因为我无法访问代码,但它明确抱怨 using 指令或声明的使用。我也试过这个:
using red = long_and_ugly::colour::red;
但它也失败了。很抱歉没有粘贴错误。不过,我相信您应该能够复制它。
问题
-
是否可以在标准 C++11 中为枚举值声明别名,或者我是否使用了 clang 扩展?
-
如果是,正确的语法是什么?
【问题讨论】:
-
using red = long_and_ugly::colour::red;这是类型别名的语法。我怀疑它很快就会被用于其他任何事情。 -
@dyp 谢谢,您链接的页面回答了我的问题。关于你最后的评论,我记得 g++ 也抱怨过那个表格,但无论如何我会尽快再试一次。
-
其实,你确定clang++在写
using long_and_ugly::colour;的时候把所有的枚举数都“放到了当前的命名空间”吗? The clang++ at coliru disagrees -
@dyp 我很开心。它奏效了,因为我能够使用别名作为模板参数。
标签: c++ c++11 enums alias language-lawyer