开发者

Detect template presence at compilation time

GCC up to 4.5 doesn't have standa开发者_StackOverflow社区rd C++0x type trait template has_nothrow_move_constructor. I could use it in my package for optimization, but I don't want to rule out one of the common compilers and don't want to overload configuration with symbols like HAVE_STD_HAS_NOTHROW_MOVE_CONSTRUCTOR. Is it somehow possible to use that template if present and just fall back to copying if not present without using any predefined configuration symbols? I also don't want to depend on Boost, since my library is small and doesn't need Boost for any other reasons.

In pseudocode, I need something like:

template <typename type>
struct has_nothrow_move_constructor_robust
  : public integral_constant <bool,
           /* if possible */  has_nothrow_move_constructor <type>::value
           /* otherwise   */  false>
{ };

Since move constructors are only for C++0x anyway, I don't mind using other C++0x features for the above definition, if at all possible.


boost::variant has an implementation of has_nothrow_move for its own internal use - you could use that, although it's not as reliable as a proper compiler implementation would be. The source for it is here - I don't know how reliable it is, so YMMV.

Apart from that, you could test compiler version macros (__GNUC__ and __GNUC_MINOR__) to determine presence, and stub it out if not present. Unfortunately it seems has_nothrow_move_constructor isn't supported in any released version of G++ yet, so you'll have to wait a bit before you'll know the right version to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜