首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

从C++容器中获取存储数据的类型

  • 24-03-18 06:43
  • 2104
  • 10448
blog.csdn.net

相关系列文章

C++之std::decay-CSDN博客

目录

1.案例

2.解决方法

参考


1.案例

现在有这样一个函数:

  1. template <typename Container>
  2. void func(Container t, const char* pMessage){ }

需要从容器 t 中获取元素的类型,首先我们想到的是: 

  1. #include
  2. #include
  3. template <typename Container>
  4. void  func(Container& t, const char* pMessage)
  5. {
  6. using TYPE = decltype(*t.begin());
  7. std::cout << pMessage << boost::typeindex::type_id_with_cvr<decltype(TYPE)>().pretty_name() << std::endl;
  8. }

函数调用:

  1. std::vector<int> array4;
  2. func(array4, "array4 value type is: "); //编译正确
  3. int array1[5] = {0, 1,3,4,8};
  4. func(array1, "array1 value type is: "); //编译报错

屏蔽编译错误,输出:

array4 value type is: int&

如果需要定义跟t中元素一样类型的变量, 如:

TYPE  temp;

编译报错, 那是因为TYPE是int&,如果需要定义,就必须去掉引用,于是想到了std::decay, 它的作用是取出基本类型, 为类型T应用从左值到右值(lvalue-to-rvalue)、数组到指针(array-to-pointer)和函数到指针(function-to-pointer)的隐式转换。转换将移除类型T的cv限定符(const和volatile限定符),上面的代码就可以这样写:

  1. #include
  2. #include
  3. template <typename Container>
  4. void  func(Container& t, const char* pMessage)
  5. {
  6. using TYPE = std::decay<decltype(*t.begin())>;
  7. std::cout << pMessage << boost::typeindex::type_id_with_cvr<decltype(TYPE)>().pretty_name() << std::endl;
  8. }

函数调用:

  1. std::vector<int> array4;
  2. func(array4, "array4 value type is: "); //编译正确
  3. int array1[5] = {0, 1,3,4,8};
  4. func(array1, "array1 value type is: "); //编译还是报错

屏蔽编译错误,输出:

array4 value type is: int

此时函数调用func(array1, "array1 value type is: ")还是报错,主要是func没有考虑容器是数组的情况,利用C++11之后系统提供的std::begin()就可以满足要求,于是代码修改如下:

  1. #include
  2. #include
  3. template <typename Container>
  4. void  func(Container& t, const char* pMessage)
  5. {
  6. using TYPE = std::decay<decltype(*std::begin(t)>;
  7. std::cout << pMessage << boost::typeindex::type_id_with_cvr<decltype(TYPE)>().pretty_name() << std::endl;
  8. }

函数调用:

  1. std::vector<int> array4;
  2. func(array4, "array4 value type is: "); //编译正确
  3. int array1[5] = {0, 1,3,4,8};
  4. func(array1, "array1 value type is: "); //编译正确

输出:

  1. array4 value type is: int
  2. array1 value type is: int

2.解决方法

上面的代码都是基于C++标准容器的,从std::begin()可以联想到STL中标准容器中有个value_type,可以直接拿来使用,于是先定义个类型萃取器:

  1. template<typename Container>
  2. struct typeFromContainer
  3. {
  4. using type = typename Container::value_type;
  5. };
  6. template<typename T, size_t N>
  7. struct typeFromContainer
  8. {
  9. using type = T;
  10. };
  11. template<typename T>
  12. struct typeFromContainer
  13. {
  14. using type = T;
  15. };

函数修改为:

  1. #include
  2. #include
  3. template <typename Container>
  4. void  func(Container& t, const char* pMessage)
  5. {
  6. using type = typename typeFromContainer::type;
  7. std::cout << pMessage << boost::typeindex::type_id_with_cvr().pretty_name() << std::endl;
  8. }

测试代码为:

  1. int main()
  2. {
  3. std::vector<int> array4;
  4. func(array4, "array4 value type is: ");
  5. int array1[5] = {0, 1,3,4,8};
  6. func(array1, "array1 value type is: ");
  7. std::initializer_list<int> intArray{ 9, 10, 33,44 };
  8. func(intArray, "intArray value type is: ");
  9. std::array<int, 5> arArray{ 1,2,555,666,777888 };
  10. func(arArray, "arArray value type is: ");
  11. std::string strArray("32095235252");
  12. func(strArray, "strArray value type is: ");
  13. const int array2[6] = { 2,3,4,5,6,7 };
  14. func(array2, "array2 value type is: ");
  15. std::list<int> list1;
  16. func(list1, "list1 value type is: ");
  17. return 0;
  18. }

输出:

  1. array4 value type is: int
  2. array1 value type is: int
  3. intArray value type is: int
  4. arArray value type is: int
  5. strArray value type is: char
  6. array2 value type is: const int
  7. list1 value type is: int

参考

std::begin()

std::decay

注:本文转载自blog.csdn.net的流星雨爱编程的文章"https://blog.csdn.net/haokan123456789/article/details/135251777"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top