C++14 允许 [[deprecated]]
应用于(根据 7.6.5/2)“类声明、typedef 名称、变量、非静态数据成员、函数、枚举或模板特化”的属性。值得注意的是缺少模板。所以给定一个模板:
template<class T>
class MyOldRefCountingPointer {
...
};
我可以弃用,比如说,MyOldRefCountingPointer<void>
,
template<>
class
[[deprecated ("Use std::shared_ptr<void> instead of MyOldRefCountingPointer")]]
MyOldRefCountingPointer<void> {
...
};
但我不能弃用通用模板:
template<class T>
class
[[deprecated ("Use std::shared_ptr instead of MyOldRefCountingPointer")]]
MyOldRefCountingPointer {
...
};
为什么不允许弃用模板?
更新
如何在不产生警告的情况下使用已弃用的模板的示例如下:
template<class T>
class
[[deprecated]]
OldClass {};
template<template<class> class C = OldClass> // use deprecated template as
void f() // default template parameter
{
}
g++ 和 Clang 都不会在这里发出警告。 Example at Coliru .
最佳答案
在 C++11 和 C++14 中,属性不能属于模板。鉴于:
template<typename T> struct [[deprecated]] C { ... };
[[deprecated]]
属于从模板实例化的类,不属于模板本身。特别是,如果您这样写:
template<typename T> struct C<T*> { ... };
... 然后 C<int>
已弃用,但 C<int*>
不是。
支持模板弃用的自然方式是在模板声明上允许属性说明符序列:
[[attribute]] template<typename T> struct C { ... };
...但是目前不支持该语法,到目前为止还没有添加它的建议。
关于c++ - 为什么不能使用 [[deprecated]] 弃用模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25984196/