C++中的属性列表中的省略号应该用来做什么?
What should ellipsis in attribute-list in C++ be used for?
在C++ reference中我找到了关于C++中允许的属性语法的信息,它是:
[[attribute-list]]
[[ using attribute-namespace : attribute-list ]]
"where attribute-list is a comma-separated sequence of zero or more attributes (possibly ending with an ellipsis ... indicating a pack expansion)"
我试过使用它,但我看不出有什么区别:
[[deprecated]] void f()
{
}
和
[[deprecated...]] void f()
{
}
在这两种情况下输出是相同的。
这是为了一致性而添加到规范中的,也是因为属性的未来仍在讨论中。考虑到我们目前在可变参数模板中有 包扩展 (参见 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 {};
但是,同样,这只是在规范中,目前没有可以这样使用的属性。
在C++ reference中我找到了关于C++中允许的属性语法的信息,它是:
[[attribute-list]]
[[ using attribute-namespace : attribute-list ]]
"where attribute-list is a comma-separated sequence of zero or more attributes (possibly ending with an ellipsis ... indicating a pack expansion)"
我试过使用它,但我看不出有什么区别:
[[deprecated]] void f()
{
}
和
[[deprecated...]] void f()
{
}
在这两种情况下输出是相同的。
这是为了一致性而添加到规范中的,也是因为属性的未来仍在讨论中。考虑到我们目前在可变参数模板中有 包扩展 (参见 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 {};
但是,同样,这只是在规范中,目前没有可以这样使用的属性。