Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending namespace std to implement make_unique when using C++11

Tags:

c++

c++11

c++14

I came across a codebase that is fixed on C++11 features but implements std::make_unique. That is been done extending namespace std to add the feature if C++14 is not use, i.e. wrapping the implementation around

#if defined(__cplusplus) && __cplusplus < 201402L

namespace std {
  ...
}

#endif

I know that is undefined behavior to extend namespace std (with some exception). Is the case above still acceptable or should it be avoided in any case?

like image 209
BeppeNanoso Avatar asked Jul 26 '19 16:07

BeppeNanoso


Video Answer


1 Answers

No, this is forbidden—even though, via

#define make_unique ? ? ?

a conforming C++11 program can be quite sure that the library never mentions the name (outside of a stringization) and would thus be unable to detect the extension.

like image 189
Davis Herring Avatar answered Nov 14 '22 00:11

Davis Herring