Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to deprecate a namespace?

The short: Is there a way to deprecate a namespace in gcc or clang?

The long:

Over the years, we have been accumulating all sort of stuff in a catch-all namespace. Now we have decided to put some order into it, and split the namespace into properly named ones; so:

namespace util
{
uint32_t codecID( const char * name ) ;

void alignStrings( std::vector< std::string > * strings ) ;
}

should become

namespace codec
{

uint32_t codecID( const char * name ) ;

}

namespace fmt
{

void alignStrings( std::vector< std::string > * strings ) ;

}

Just to add to the fun, the old namespace is defined across several include files. And everything in there is inline/template code; so there are no libraries associated with it.

The obvious solution would be to copy all definitions from the old namespace to the new ones and mark everything into the old namespace as deprecated, item by item.

We can't just rename the namespace without breaking several projects.

Now I'm wondering if there is a better way of doing something like this, such as marking the use of namespace util as deprecated.

We use gcc 4.7.3 as our production compiler, but build and test against clang to try and catch gcc specifics; so something working on any of those compilers would help.

like image 311
Diego Sánchez Avatar asked Apr 02 '14 09:04

Diego Sánchez


People also ask

How do I get out of namespace?

No you can't unuse a namespace. The only thing you can do is putting the using namespace -statement a block to limit it's scope.

How do you deprecate a function in C++?

In C++14, you can mark a function as deprecated using the [[deprecated]] attribute (see section 7.6. 5 [dcl. attr. deprecated]).

What does deprecated mean in C++?

Deprecated attribute in C++14 with Examples Deprecated means the use of the name or entity declared with this attribute is allowed but discouraged for some reason.


1 Answers

In C++14 we are allowed to apply attributes to namespaces (although gcc seems to ignore the attribute). This came about via defect report 1657 has CD4 status which means it should apply to C++14.

The paper that brought in the new wording was N4196:

However, attributes are not permitted on enumerators nor on namespaces. In response, CWG issue 1657 and EWG issue 113 were filed and received favourably. This paper proposes resolving these issues by allowing attributes to be specified on enumerators and namespaces, and extends the [[deprecated]] attribute to apply to these entities, as was originally intended.

See it in a live godbolt example:

namespace [[deprecated]] util {

and in clang we see the following warning if we for example use codecID:

warning: 'util' is deprecated [-Wdeprecated-declarations]
util::codecID( "hello") ;
    ^
note: 'util' has been explicitly marked deprecated here
namespace [[deprecated]] util
        ^

Although clang warns this is a C++17 feature (I believe that is a bug) and gcc warns the attribute is ignored although says it supports for it as a C++17 feature.

like image 116
Shafik Yaghmour Avatar answered Sep 28 '22 06:09

Shafik Yaghmour