Why can't templates be deprecated with [[deprecated]]?

C ++ 14 permits the [[deprecated]] attribute to be applied to (as per 7.6.5 / 2) “declaration of a class, typedef name, variable, non-static data member, function, Enumeration or specialization pattern.” Especially there are no templates. So the template is given:

 template<class T> class MyOldRefCountingPointer { ... }; 

I can blame, say, MyOldRefCountingPointer<void> ,

 template<> class [[deprecated ("Use std::shared_ptr<void> instead of MyOldRefCountingPointer")]] MyOldRefCountingPointer<void> { ... }; 

but I can not judge the general pattern:

 template<class T> class [[deprecated ("Use std::shared_ptr instead of MyOldRefCountingPointer")]] MyOldRefCountingPointer { ... }; 

Why are templates forbidden?

Refresh

An example of how an obsolete template can be used without warning:

 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 give warnings here. An example on Kolira .

+6
source share
2 answers

In C ++ 11 and C ++ 14, attributes cannot match the pattern . Given:

 template<typename T> struct [[deprecated]] C { ... }; 

[[deprecated]] refers to classes created from the template, not the template itself. In particular, if you write this:

 template<typename T> struct C<T*> { ... }; 

... then C<int> deprecated, but C<int*> not.

A natural way to support obsolescence of templates would be to provide a qualifier-seq attribute in the template declaration:

 [[attribute]] template<typename T> struct C { ... }; 

... but this syntax is not currently supported, and so far there have been no suggestions for adding it.

+5
source

I am pretty sure that it falls under the general depreciation of the class (The same goes for everything you can plan).

In any case, neither g ++ nor clang ++ complain: coliru

+1
source

Source: https://habr.com/ru/post/975683/


All Articles