加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – “:: value”在哪里获得价值?

发布时间:2020-12-16 10:02:54 所属栏目:百科 来源:网络整理
导读:我一直在阅读有关SFINAE的内容,并查看以下某些变体的一些示例: #include iostream#include type_traitstemplate typename... Ts using void_t = void;template typename T,typename = voidstruct has_typedef_foobar : std::false_type {};template typenam
我一直在阅读有关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;
}

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

我对价值成员的来源感到困惑. has_typedef_foobar的两个定义似乎都没有指定名为value的布尔成员.

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

谢谢.

解决方法

std :: true_type和std :: false_type定义为:

using true_type = std::integral_constant<bool,true>
using false_type = std::integral_constant<bool,false>

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

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

template<class T,T v>
struct integral_constant {
  ...
  static constexpr T value = v;
  ...
};

除此之外,您还会看到名为value的静态constexpr变量.当然,如果你将std :: integeral_constant实例化为std :: integral_constant< bool,true>然后将instatiatation中的value成员变量设置为true.以同样的方式,如果你将std :: integral_constant实例化为std :: integral_constant< bool,false>然后它的value成员变量设置为false.

继承自std :: true_type,您还继承了设置为true且继承自std :: false_type类型的值成员变量,您还继承了设置为false的值成员变量.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读