栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++14 make

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++14 make

std::make_inedex_sequence的作用是产生一个0,1,2,3,....,N-1的数列。我在STL源码中没有找到实现,所以参考网上的资料,尝试着给出一份自己理解的实现。

template
struct index_sequence{};

template
struct make_index_sequence : public make_index_sequence
{

};

template
struct make_index_sequence<0, M...>: public index_sequence
{

};

这种写法的核心是M...起始为空,用头插法由N-1递归地扩展出0,1,2,..., N-1序列。以make_index_sequence<3>为例,递归扩展过程如图所示:

 网上我还看到另一种方法,采用尾插法:

template
struct index_sequence{};

template
struct AppendNewValue;

template
struct AppendNewValue{
    using type = index_sequence;
}

template
struct make_index_sequence
{
    using type = typename AppendNewValue::type, N-1>::type;
}

template<>
struct make_index_sequence<0>
{
    using type = index_sequence<>;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号