喜欢这篇文章吗?喜欢的话去看博主的置顶博客,即可依据分类找到此文章的原版得到更好的体验,
图片及代码显示的问题,笔者深感抱歉,想要更好的体验去原博文即可。
title: Boost 源码分析笔记3 - integral_constant
mathjax: true
date: 2020-03-17 15:36:30
categories: [c++笔记,Boost源码分析笔记]
tags: [c++笔记,Boost源码分析笔记]
keywords: [c++笔记,Boost源码分析笔记]
integral_consant
这也是一个模版元技术,他储存了自己的类型,模版的类型,模版的值的类型,他的实现如下
template <class T, T val>
struct integral_constant
{
typedef mpl::integral_c_tag tag;
typedef T value_type;
typedef integral_constant<T, val> type;
static const T value = val;
operator const mpl::integral_c<T, val>& ()const
{
static const char data[sizeof(long)] = { 0 };
static const void* pdata = data;
return *(reinterpret_cast<const mpl::integral_c<T, val>*>(pdata));
}
BOOST_CONSTEXPR operator T()const { return val; }
};
这里很明显了,value是值,value_type是value的类型,type是自己的类型。
true_type false_type
这里就很有意思了,看看就懂
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
可能有人会问这个有什么用,其实这样的,很多时候我们需要为我们的类添加一个value,表示true或者false,正常的实现方法是写两遍,一遍处理全部,另一遍特化false,这样写的话,代码复用就太low了,这时候,其实我们只需要实现一遍基类,派生的时候一个继承true,另一个继承false就OK了。