C ++中的attribute-list中的省略号应该用于什么?

时间:2018-02-25 23:56:31

标签: c++ attributes ellipsis

C++ reference中,我找到了有关C ++中允许的属性语法的信息,它是:

[[attribute-list]]
[[ using attribute-namespace : attribute-list ]]

“其中attribute-list是一个逗号分隔的零个或多个属性序列(可能以省略号结尾...表示包扩展)”

我试过使用它,但我认为没有区别:

[[deprecated]] void f() 
{
}

[[deprecated...]] void f() 
{
}

在这两种情况下,输出都是相同的。

1 个答案:

答案 0 :(得分:1)

这是为了保持一致性而添加到规范中的,也因为仍在讨论属性的未来。考虑到我们目前在可变参数模板中进行包扩展(请参见Variadic template pack expansion),如下所示:

// pack expansion in function arguments
template <typename... Args>
void f(Args... args) {}

// pack expansion in inheritance
template <typename... Inherited>
struct MyClass : Inherited... {};

沿着同一行,考虑属性的包扩展也很有意义。一些示例方案可能是:

template <typename... Ts>
class [[Ts...]] MyClass {};

template <typename... Ts>
class [[Ts()...]] MyClass {};

但是,再次,这只是在规范中,目前没有可以像这样使用的属性。

相关问题