C++14 permits the [[deprecated]]
attribute to be applied to (per 7.6.5/2) "the declaration of a class, a typedef-name, a variable, a non-static data member, a function, an enumeration, or a template specialization." Notably missing are templates. So given a template:
template<class T>
class MyOldRefCountingPointer {
...
};
I can deprecate, say, MyOldRefCountingPointer<void>
,
template<>
class
[[deprecated ("Use std::shared_ptr<void> instead of MyOldRefCountingPointer")]]
MyOldRefCountingPointer<void> {
...
};
but I can't deprecate the general template:
template<class T>
class
[[deprecated ("Use std::shared_ptr instead of MyOldRefCountingPointer")]]
MyOldRefCountingPointer {
...
};
Why is it not permitted to deprecate templates?
An example of how a deprecated template can be used without yielding a warning is this:
template<class T>
class
[[deprecated]]
OldClass {};
template<template<class> class C = OldClass> // use deprecated template as
void f() // default template parameter
{
}
Neither g++ nor Clang issue warnings here. Example at Coliru.
In C++11 and C++14, attributes cannot appertain to a template. Given:
template<typename T> struct [[deprecated]] C { ... };
[[deprecated]]
appertains to the classes instantiated from the template, not to the template itself. In particular, if you write this:
template<typename T> struct C<T*> { ... };
... then C<int>
is deprecated, but C<int*>
is not.
The natural way to support deprecation of templates would be to allow an attribute-specifier-seq on a template-declaration:
[[attribute]] template<typename T> struct C { ... };
... but that syntax is not currently supported, and so far there have been no proposals to add it.
I'm quite sure that falls under the general deprecating a class (Same for everything else that can be templated).
Anyway, neither g++ nor clang++ complain: coliru
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With