I want to rename a templated class. To make the transition easier for the users, I'd like to keep the old class for one more version and mark it deprecated with the extensions from GCC / Clang (attribute deprecated). To avoid keeping an exact copy of the deprecated class, the use of template alias would be handy. Unfortunatley it does not seem to work. This is what I tried with Clang 3.3, GCC 4.7, and GCC 4.8:
我想重命名模板类。为了让用户更容易地进行转换,我希望将旧类保留为另一个版本,并使用GCC / Clang的扩展标记为已弃用。为了避免保留已弃用的类的精确副本,使用模板别名将非常方便。不幸的是,这似乎行不通。这就是我对Clang 3.3、GCC 4.7和GCC 4.8的尝试:
template <class blabla>
struct NewClassName
{
// ...
};
template <class blabla> using OldClassName __attribute__ ((deprecated))
= NewClassName<blabla>;
Do I miss something or is this just unsupported by the compilers? Is there an other idea to get deprecation warnings without copying the whole class?
我是不是漏掉了什么,还是编译器不支持它?是否有其他方法可以在不复制整个类的情况下获得弃用警告?
1 个解决方案
#1
6
GCC does support deprecating template alias since version 4.7. This is a test case comparing typedef and template alias:
自版本4.7以来,GCC确实支持弃用模板别名。这是一个比较typedef和模板别名的测试用例:
template <class T>
struct NewClassName
{
// ...
};
template <class T> using OldClassNameUsing __attribute__ ((deprecated))
= NewClassName<T>;
typedef NewClassName<int> OldClassNameTypedef __attribute__ ((deprecated));
int main()
{
OldClassNameUsing<int> objectUsing;
OldClassNameTypedef objectTypedef;
return 0;
}
The reason why it did not work for me was that I did not create an object of the OldClassNameUsing but accessed static members like OldClassNameUsing::myFunction(). This does never trigger a deprecation warning unless the function itself is deprecated.
它对我不起作用的原因是我没有创建OldClassNameUsing的对象,而是访问了静态成员,如OldClassNameUsing::myFunction()。除非函数本身被弃用,否则不会触发弃用警告。
Clang does not yet support deprecating a template alias - tested with version 3.3 and 3.4 from SVN r194323. The corresponding feature request is http://llvm.org/bugs/show_bug.cgi?id=17862
Clang does still not support this feature with version 4. This is also true for C++-14's [[deprecated]].
Clang还不支持弃用模板别名——使用SVN r194323的3.3版和3.4版进行测试。相应的特性请求是http://llvm.org/bugs/show_bug.cgi?id=17862 Clang仍然不支持版本4的这个特性。这也适用于c++ -14的[[已弃用]]]。
#1
6
GCC does support deprecating template alias since version 4.7. This is a test case comparing typedef and template alias:
自版本4.7以来,GCC确实支持弃用模板别名。这是一个比较typedef和模板别名的测试用例:
template <class T>
struct NewClassName
{
// ...
};
template <class T> using OldClassNameUsing __attribute__ ((deprecated))
= NewClassName<T>;
typedef NewClassName<int> OldClassNameTypedef __attribute__ ((deprecated));
int main()
{
OldClassNameUsing<int> objectUsing;
OldClassNameTypedef objectTypedef;
return 0;
}
The reason why it did not work for me was that I did not create an object of the OldClassNameUsing but accessed static members like OldClassNameUsing::myFunction(). This does never trigger a deprecation warning unless the function itself is deprecated.
它对我不起作用的原因是我没有创建OldClassNameUsing的对象,而是访问了静态成员,如OldClassNameUsing::myFunction()。除非函数本身被弃用,否则不会触发弃用警告。
Clang does not yet support deprecating a template alias - tested with version 3.3 and 3.4 from SVN r194323. The corresponding feature request is http://llvm.org/bugs/show_bug.cgi?id=17862
Clang does still not support this feature with version 4. This is also true for C++-14's [[deprecated]].
Clang还不支持弃用模板别名——使用SVN r194323的3.3版和3.4版进行测试。相应的特性请求是http://llvm.org/bugs/show_bug.cgi?id=17862 Clang仍然不支持版本4的这个特性。这也适用于c++ -14的[[已弃用]]]。