Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

Update

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.

like image 412
KnowItAllWannabe Avatar asked Sep 22 '14 22:09

KnowItAllWannabe


Video Answer


2 Answers

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.

like image 127
Richard Smith Avatar answered Oct 21 '22 00:10

Richard Smith


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

like image 36
Deduplicator Avatar answered Oct 20 '22 22:10

Deduplicator