在编程中,将命名空间std::experimental
注入到std
中是否是一种好的或者不好的实践方式?
namespace std
{
namespace experimental
{
}
using namespace experimental;
}
#include <experimental/optional>
int main()
{
std::optional< int > o;
return 0;
}
或者更现代化的形式:
#if __has_include(<optional>)
# include <optional>
#elif __has_include(<experimental/optional>)
# include <experimental/optional>
namespace std
{
using namespace experimental;
}
#else
#error !
#endif
int main()
{
std::optional< int > o;
return 0;
}
引入
std::experimental
"子命名空间" 的意图很明确,因为 std::experimental
目前包含 大量< strong>新的库。我认为很可能所有这些库都将在不做任何实质性更改的情况下迁移到 namespace std
,当前编写的用户代码可以依赖于此(我完全错了吗?)。否则,所有这些代码都应该在未来进行重构,从 std::experimental ::
更改为std ::
。这并不是大问题,但可能有不这样做的理由。
问题涉及生产代码和不太严肃的代码。
std
或命名空间内的命名空间时,其行为是未定义的。” - user657267namespace xp = std::experimental
,然后在实验版被采用时替换为std
即可。 - user657267