在编译时检测模板的存在

dou*_*lep 5 c++ configuration templates c++11

GCC高达4.5没有标准的C++ 0x类型特征模板has_nothrow_move_constructor.我可以在我的包中使用它进行优化,但我不想排除其中一个常见的编译器,并且不希望使用符号等重载配置HAVE_STD_HAS_NOTHROW_MOVE_CONSTRUCTOR.是否可以使用该模板(如果存在),如果不存在而不使用任何预定义的配置符号,则回退到复制?我也不想依赖Boost,因为我的库很小,并且由于任何其他原因不需要Boost.

在伪代码中,我需要这样的东西:

template <typename type>
struct has_nothrow_move_constructor_robust
  : public integral_constant <bool,
           /* if possible */  has_nothrow_move_constructor <type>::value
           /* otherwise   */  false>
{ };
Run Code Online (Sandbox Code Playgroud)

由于移动构造函数仅适用于C++ 0x,因此我不介意使用其他C++ 0x功能进行上述定义,如果可能的话.

bdo*_*lan 1

boost::variant有一个供其内部使用的实现has_nothrow_move- 您可以使用它,尽管它不如正确的编译器实现那么可靠。它的来源在这里- 我不知道它有多可靠,所以 YMMV。

除此之外,您可以测试编译器版本宏(__GNUC____GNUC_MINOR__)来确定是否存在,如果不存在则将其存根。不幸的是,似乎has_nothrow_move_constructor尚未在任何发布的 G++ 版本中支持它,因此您必须等待一段时间才能知道要使用的正确版本。