":: value"在哪里获得价值?

Tee*_*eej 3 c++ templates type-traits c++11

我一直在阅读有关SFINAE的内容,并查看以下某些变体的一些示例:

#include <iostream>
#include <type_traits>

template <typename... Ts> using void_t = void;

template <typename T, typename = void>
struct has_typedef_foobar : std::false_type {};

template <typename T>
struct has_typedef_foobar<T, void_t<typename T::foobar>> : std::true_type {};

struct foo {
  using foobar = float;
};

int main() {
  std::cout << std::boolalpha;
  std::cout << has_typedef_foobar<int>::value << std::endl;
  std::cout << has_typedef_foobar<foo>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

(摘自https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error)

我很困惑value会员来自哪里.has_typedef_foobar的两个定义似乎都没有指定名为value的布尔成员.

哪里::value有价值?我怀疑它是某种编译器提供的值,并且想要阅读它,但我不确定谷歌的术语,因为我的查询带来了其他与C++ 11相关的价值相关主题.

谢谢.

101*_*010 7

std::true_typestd::false_type定义为:

using true_type = std::integral_constant<bool, true>
using false_type = std::integral_constant<bool, false>
Run Code Online (Sandbox Code Playgroud)

分别.也就是说,它们是两个独立的实例std::integral_constant.

现在,如果你看一下可能的实现std::integeral_constant:

template<class T, T v>
struct integral_constant {
  ...
  static constexpr T value = v;
  ...
};
Run Code Online (Sandbox Code Playgroud)

除此之外,您还会看到一个static constexpr名为变量的变量value.当然,如果你实例化std::integeral_constant,std::integral_constant<bool, true>那么value在instatiatation中成员变量被设置为true.以同样的方式,如果您实例化std::integral_constant,std::integral_constant<bool, false>则其value成员变量设置为false.

std::true_type您继承也继承了value设置为true和从std::false_type类型继承的value成员变量,您还继承了设置为的成员变量false.