根据元素类型获取tuple中的元素
最近做cinatra遇到这样的需求,根据一个type来获取对应的第一个元素,需要注意的一个问题是,如果没有这个类型的时候,通过编译期断言提醒使用者,实现代码如下:
1.C++14实现
template <class T, std::size_t N, class... Args>
struct indexOf; template <class T, std::size_t N, class... Args>
struct indexOf<T, N, T, Args...>
{
static constexpr auto value = N;
}; template <class T, std::size_t N, class U, class... Args>
struct indexOf<T, N, U, Args...>
{
static constexpr auto value = indexOf<T, N + , Args...>::value;
}; template <class T, std::size_t N>
struct indexOf<T, N>
{
static constexpr auto value = -;
static_assert(value!=-, "the type is not exist");
}; template <class T, class... Args>
T get_element_by_type(const std::tuple<Args...>& t)
{
return std::get<indexOf<T, , Args...>::value>(t);
}
2.C++11的实现方法
template <class T, std::size_t N, class... Args>
struct indexOf; template <class T, std::size_t N, class... Args>
struct indexOf<T, N, T, Args...>
{
enum { value = N };
}; template <class T, std::size_t N, class U, class... Args>
struct indexOf<T, N, U, Args...>
{
enum { value = indexOf<T, N + , Args...>::value};
}; template <class T, std::size_t N>
struct indexOf<T, N>
{
enum { value = - };
static_assert(value!=-, "the type is not exist");
};
把enum换成const int也没问题,如果完全用C++11的方式去做那就用std::integral_constant吧,代码如下:
template <class T, std::size_t N, class... Args>
struct indexOf; template <class T, std::size_t N, class... Args>
struct indexOf<T, N, T, Args...> : std::integral_constant<int, N>
{
}; template <class T, std::size_t N, class U, class... Args>
struct indexOf<T, N, U, Args...> : std::integral_constant<int, indexOf<T, N + 1, Args...>::value>
{
}; template <class T, std::size_t N>
struct indexOf<T, N> : std::integral_constant < int, -1 >
{
}; template <class T, class... Args>
T get_element_by_type(const std::tuple<Args...>& t)
{
return std::get<indexOf<T, 0, Args...>::value>(t);
}
注意,integral_constant方式,加断言不起作用,不过会返回一个无效的-1,在外层也会出现断言提示,问题不大。
测试代码:
std::tuple<int, double, char, short> tp = std::make_tuple(1, 2.3, 2, 1);
//auto r = get_element_by_type<string>(tp); //编译期断言错误
auto r = get_element_by_type<double>(tp); //返回2.3
根据元素类型获取tuple中的元素的更多相关文章
- 获取父窗口元素或者获取iframe中的元素(相同域名下)
jquery方法 在父窗口中获取iframe中的元素 //方法1 $("#iframe的ID").contents().find("iframe中的元素"); ...
- Js/Jquery获取iframe中的元素
转载: Js/Jquery获取iframe中的元素 - - ITeye技术网站http://java-my-life.iteye.com/blog/1275205 在web开发中,经常会用到ifram ...
- js 获取iframe中的元素
今天要修改编辑器插件中的元素遇到的问题 jquery 在父窗口中获取iframe中的元素 1.Js代码 格式:$("#iframe的ID").contents().find(&qu ...
- 获取iframe中的元素
父窗口中获取iframe中的元素 var ifr = document.getElementById('suggustion').contentWindow.document.body; 在ifram ...
- Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素 1. 格式:window ...
- 【学习】如何用jQuery获取iframe中的元素
(我的博客网站中的原文:http://www.xiaoxianworld.com/archives/292,欢迎遇到的小伙伴常来瞅瞅,给点评论和建议,有错误和不足,也请指出.) 说实在的,以前真的很少 ...
- 父窗口中获取iframe中的元素
js 在父窗口中获取iframe中的元素 1. 格式:window.frames["iframe的name值"].document.getElementById("ifr ...
- 在iframe窗体内 获取父级的元素;;在父窗口中获取iframe中的元素
在iframe中获取父窗口的元素 $(‘#父窗口中的元素ID’, parent.document).click(); 在父窗口中获取iframe中的元素 $(“#iframe的ID”).content ...
- [JavaScript] 获取数组中相同元素的个数
/** * 获取数组中相同元素的个数 * @param val 相同的元素 * @param arr 传入数组 */ function getSameNum(val,arr){ processArr ...
随机推荐
- bochs配置文件解释说明
############################################### # Configuration file for Bochs ##################### ...
- CSS3 根据屏幕大小显示内容(@media)
@media (min-width: 993px) { .footer .addZ1{display:none;} .footer .addZ2{display:none;} .footer . ...
- BufferedReader的小例子
注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用: BufferedReader buf = ...
- 10.31 正睿停课训练 Day13
目录 2018.10.31 正睿停课训练 Day13 A Poker(期望) B Label(高斯消元) C Coin(二分图染色 博弈) 考试代码 A(打表) B 2018.10.31 正睿停课训练 ...
- bzoj 4737: 组合数问题
Description 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3)三个物品中选择两个物品可以有( 1,2),(1,3),(2,3)这三种选择方法.根据组合数 ...
- Python3练习题系列(10)——项目骨架构建
目标: 如何创建<项目“骨架”目录> 包含:项目文件布局.自动化测试代码,模组,以及安装脚本. 由于编写一个Python文件可以作为一个模块,一个带__init__.py的目录算一个包. ...
- 2016年3月4日Android实习笔记
1.让水平LinearLayout中的两个子元素分别居左和居右 在LinearLayout中有两个子元素,LinearLayout的orientation是horizontal.需要让第一个元素居左, ...
- 支付宝支付集成过程中如何生成商户订单号(out_trade_no)
out_trade_no是指商户网站唯一订单号,在商户端唯一,每个商户订单号会对应一个支付宝订单号 ,此订单号由珊瑚自己生成,商户订单号要求64个字符以内.可包含字母.数字.下划线:需保证在商户端不重 ...
- libctemplate——源码分析
前言 在阅读此文章前,建议先阅读我之前写的<libctemplate——C语言模块引擎简介及使用>,以便对这个库有一个初步的认识.通过对库的代码阅读,对库有了一定的认识,提练一些重要的知识 ...
- ORB-SLAM2(一)----使用Eclipse进行开发
1.导入项目 准备工作 1, first we should make sure the compile with build.sh under ORB_SLAM2-master is OK. 2, ...