Question: Namespace alias in c++


Namespace alias in c++

Answers 2
Added at 2017-01-02 02:01

I use c++11 while I need some classes from c++17 library. When using boost from which classes were added I wish to do the following:

#if __cplusplus < CPP17
using std::any = boost::any;  

Such alias is not allowed. Also extending the std namespace causes undefined behaviour. I wish my code to look the same regardles of the c++ version. Is there a clear way?

Answers to

Namespace alias in c++

nr: #1 dodano: 2017-01-02 02:01

The clear way is to add a customized name for it.

#if __cplusplus < CPP17
using my_any = boost::any;
using my_any = std::any;    

// using my_any...
nr: #2 dodano: 2017-01-02 03:01

It seems that you're attempting to create a type alias within a namespace. The corect syntax for that is:

namespace namespace_name {
    using any = boost::any;

However, the standard disallows adding definitions (there are exceptions for template specializations) into std namespace, so if you tried to define std::any, the behaviour of your program will be undefined.

Using any namespace, including the global one is OK, but not the ones reserved to the implementation, which includes std and its subnamespaces.

Source Show
◀ Wstecz