.

  • ​​学习资料​​
  • ​​实现机制​​
  • ​​下面实现一个`is_int`对于是否是int的提问​​
  • ​​对于其他类型直接继承m_false_type表示不是int类型​​
  • ​​对于int类型继承了m_true_type​​
  • ​​完整代码​​
  • ​​在vs2019头文件 xtr1common可以查看到完整实现​​
  • ​​`std::enable_if`​​

学习资料

​https://light-city.club/sc/src_analysis/stl/traits/​

通俗解释为算法(func)问 iterator_traits(我),但是 iterator_traits(我)发现手上是指针的时候,就由我来替它回答。如果是 class type,iterator_traits(我)就继续问(他—T::value_type)。

总结:通过定义内嵌类型,我们获得了知晓 iterator 所指元素类型的方法,通过 traits 技法,我们将函数模板对于原生指针和自定义
iterator 的定义都统一起来,我们使用 traits 技法主要是为了解决原生指针和自定义 iterator
之间的不同所造成的代码冗余,这就是 traits 技法的妙处所在。


traits:萃取机
用来获取类型,回答问题的
例如 is_void is_null_pointer等
​​​https://en.cppreference.com/w/cpp/header/type_traits​

实现机制

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

template <bool b>
using m_bool_constant = m_integral_constant<bool, b>;

using m_true_type = m_bool_constant<true>;
using m_false_type = m_bool_constant<false>;

m_true_type m_false_type表示正确和错误的类型基本类型
想要表示false的概念的时候直接继承即可

下面实现一个is_int对于是否是int的提问

namespace goal {
template<typename T>
struct is_int :false_type {};

template<>
struct is_int<int> :m_true_type {};
}

对于其他类型直接继承m_false_type表示不是int类型

对于int类型继承了m_true_type

完整代码

#include <iostream>
using namespace std;

#define debug(x) cout<<#x<<": "<<(x)<<endl;

namespace goal {

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

template <bool b>
using m_bool_constant = m_integral_constant<bool, b>;

using m_true_type = m_bool_constant<true>;
using m_false_type = m_bool_constant<false>;

}

namespace goal {
template<typename T>
struct is_int :false_type {};

template<>
struct is_int<int> :m_true_type {};
}


int main()
{
debug(goal::is_int<int>::value)
debug(goal::is_int < double > ::value)
return 0;
}

​outPut​

goal::is_int<int>::value: 1
goal::is_int < double > ::value: 0

在vs2019头文件 xtr1common可以查看到完整实现

我使用的是以下配置

【STL源码阅读】type_traits_#include

template <class _Ty, _Ty _Val>
struct integral_constant {
static constexpr _Ty value = _Val;

using value_type = _Ty;
using type = integral_constant;

constexpr operator value_type() const noexcept {
return value;
}

_NODISCARD constexpr value_type operator()() const noexcept {
return value;
}
};

// ALIAS TEMPLATE bool_constant
template <bool _Val>
using bool_constant = integral_constant<bool, _Val>;

using true_type = bool_constant<true>;
using false_type = bool_constant<false>;

ref:

我用阿里云盘分享了「42. type traits.mp4」,你可以不限速下载🚀
复制这段内容打开「阿里云盘」App 即可获取
链接:​​​https://www.aliyundrive.com/s/CsmsdRRRYJ6​


std::enable_if

​https://en.cppreference.com/w/cpp/types/enable_if​

【STL源码阅读】type_traits_type_traits_02

如果B是true,那么type就是T
否则就啥也没有

#include<iostream>
#include<vector>

using namespace std;

template <class Iter,typename std::enable_if<true,int>::type = 0>
void f(Iter first, Iter last) {

for (auto i = first; i != last; ++i) {
cout << *i << endl;
}
}

int main() {

vector<int> v = { 1,2,3,4,5,6,7 };
f(v.begin(), v.end());
return 0;
}

函数f功能是打印容器元素
函数参数等价于<class Iter,int = 0>
加上默认参数,这样调用的时候就不用指定第二个参数
直接可以经过函数参数推导
通过f调用