c++ - 如何进入 C++ 容器的 T 如果没有提供 Container::value_type?
问题描述
value_type
容器模板包含typedef是很常见的。这使得创建其他模板代码变得容易,最近的概念,它们能够提取T
if only have been givenContainer
和 not Container<T>
。
但是,并非所有容器(或其他模板类)都定义了这样的 a value_type
,尤其是较旧的容器。T
即使没有它也有可能到达收容物吗?
我知道有一些技巧,比如“如果它是一个迭代器,那么.begin()
应该返回一个该类型的值”,但这并不能帮助我们编写一个概念要求来检查一个类是否.begin()
确实遵循迭代器的要求。
解决方案
这是一个类似于Quimby 的解决方案但使用部分模板特化的解决方案:
template<typename...>
struct inner_type_impl;
template<template<typename...> typename C, typename T, typename...Args>
struct inner_type_impl<C<T,Args...>>
{
using type = T;
};
template<typename T>
using inner_type = typename inner_type_impl<T>::type;
这是从 Quimby 的解决方案中借用的演示。
推荐阅读
- android - 如何完成应用程序并返回上一屏幕
- javascript - React:如何在不改变状态的情况下根据属性过滤状态?
- arrays - 219. Contains Duplicate II - 为什么使用二分查找的解决方案适用于未排序的数组?
- php - 如何在php mysql的单个页面中多次调用存储过程
- c++ - 编写单元测试和使用 for 循环时的最佳方法?
- python - 如何检查python函数是否使用while循环?
- reactjs - 如何将按钮添加到 react-date-range
- python - 基于元数据列出 azure blob - python sdk
- python-3.x - 具有不同进程的多个 tqdm 进度条
- excel - 损坏的工作表对象