我遇到了一个固定在 C++11 功能上但实现了 std::make_unique
的代码库。如果不使用 C++14,则扩展 namespace std
以添加功能,即围绕实现包装
#if defined(__cplusplus) && __cplusplus < 201402L
namespace std {
...
}
#endif
我知道那是 undefined behavior to extend namespace std
(有一些异常(exception))。上述情况仍然可以接受还是应该避免?
最佳答案
不,这是被禁止的——尽管,通过
#define make_unique ? ? ?
符合标准的 C++11 程序可以非常确定库从不提及名称(在字符串化之外),因此无法检测到扩展名。
关于c++ - 使用 C++11 时扩展命名空间 std 以实现 make_unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57224266/