Question

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.

Was it helpful?

Solution 2

If you define the new namespaces in a new header file, you could deprecate the old, redundant header files entirely with #warning directive instead of item by item. libstdc++ does that. Clang supports the directive as well but MSVC for example does not.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top