如何使用std :: enable_if条件本身取决于另一个条件?

时间:2015-03-17 22:54:55

标签: c++ templates c++11 metaprogramming enable-if

我有一种情况需要使用foo来区分两个重载,例如std::enable_if。赋予std::enable_if本身的条件取决于foo的模板参数的依赖类型。

使用std::enable_if表达此问题的最佳方式是什么?

以下测试代码是我目前所拥有的。我意识到除了std::enable_if之外,还有更好的方法可以在测试代码中实现我想要的行为。但是,以下是我的用例的简化版本,它本身需要std::enable_if

#include <type_traits>
#include <cassert>

struct bar
{
  using baz = int;
};

template<class T> struct is_bar : std::false_type {};
template<> struct is_bar<bar> : std::true_type {};

template<class Bar>
struct baz_type
{
  using type = typename Bar::baz;
};


template<class T>
typename std::enable_if<
  std::is_integral<
    typename baz_type<T>::type
  >::value,
  int
>::type
  foo(T x)
{
  return 7;
}

template<class T>
typename std::enable_if<
  !is_bar<T>::value,
  int
>::type
  foo(T x)
{
  return 13;
}

int main()
{
  assert(foo(bar()) == 7);
  assert(foo(0) == 13);

  return 0;
}

编译器输出:

$ g++ --version ; echo ; g++ -std=c++11 repro.cpp 
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


repro.cpp: In instantiation of ‘struct baz_type<int>’:
repro.cpp:29:3:   required by substitution of ‘template<class T> typename std::enable_if<std::is_integral<typename baz_type<Bar>::type>::value, int>::type foo(T) [with T = int]’
repro.cpp:49:3:   required from here
repro.cpp:18:33: error: ‘int’ is not a class, struct, or union type
   using type = typename Bar::baz;

此代码无法编译,因为enable_if的第一个重载中使用的foo取决于嵌套类型T::baz。因为int没有这种嵌套类型,所以代码是非法的。

表达我想要的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

当我在Coliru上写下文时,@ dyp已经在他的评论中显示了重要的部分。以下是什么工作,什么是恕我直言,相当可读:

template<
    class T,
    typename=typename std::enable_if<is_bar<T>::value>::type,
    typename=typename std::enable_if<std::is_integral<typename baz_type<T>::type>::value>::type
>
int foo(T x)
{
  return 7;
}

template<
    class T,
    typename=typename std::enable_if<!is_bar<T>::value>::type
>
int foo(T x)
{
  return 13;
}

Live example

使用C ++ 14,可以使用std::enable_if_t来缩短它。

相关问题