当前位置: 首页 > news >正文

无用知识研究:对std::common_type以及问号表达式类型的理解

先说结论:

如果问号表达式能编译通过,那么std::common_type就能通过。因为common_type的底层依赖的就是?:

common_type的实现里,利用了问号表达式:ternary conditional operator (?:)

https://stackoverflow.com/questions/14328034/what-is-the-point-of-this-condition-found-in-the-implementation-of-stdcommon-tstd::common_type的部分实现:template <class T, class U>
struct common_type<T, U> {typedef decltype(true ? declval<T>() : declval<U>()) type;
};

?:这个表达式,有类型两个属性。有以下知识点:

1、表达式的。是运行时的逻辑,这个大家都很熟悉。比如一个问号表达式true : 2 : 1.0。它有一个值,这个值是根据condition判断出来的(这个例子里是true)。咱们可以用auto val来接住该值,auto val = true : 2 : 1.0; 那么val的值就是2.0。(注意,为什么是2.0,看下面分解

2、表达式的类型。是编译时获取的。也就是说true : 2 : 1.0作为一个整体,它有一个类型,系统会取其“共同”的类型。至于怎么提取的,是系统决定的。这个类型可以用decltype()在编译器来取,比如decltype(true : 2 : 1.0)。记住它是在编译期来确定的,而不是运行期来确定,所以它不管condition的。所以第一条里面,true : 2 : 1.0的值为什么是2.0呢,因为decltype对其分析的结果就是double。为了验证这一点,参看下面的写法,编译不成功:

auto x = true ? 1 : L"123";编译错误:
error C2446: “:”: 没有从“const wchar_t *”到“int”的转换
note: 没有使该转换得以执行的上下文
error C3536: “x”: 初始化之前无法使用为什么编译不成功呢,那是因为int和const wchar_t*是没有共同类型的。using T = decltype(true? std::declval<int>() : std::declval<const wchar_t *>());
或者
using T = decltype(false ? std::declval<int>() : std::declval<const wchar_t *>());结果都一样,编译错误:
error C2446: “:”: 没有从“const wchar_t *”到“_Ty1”的转换
1>        with
1>        [
1>            _Ty1=int
1>        ]
note: 没有使该转换得以执行的上下文
而以下的写法,表达式的类型均为double输出double
using T = decltype(1 ? 1 : 1.0);
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;输出double
using T = decltype(0 ? 1 : 1.0);
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;输出double
using T = decltype(0 ? 1.0 : 1);
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;输出double
using T = decltype(true ? std::declval<int>() : std::declval<double>());
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;输出double
using T = decltype(false? std::declval<int>() : std::declval<double>());
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;

再看自定义指针的例子: 

提取shit0*和shit1*的共同类型。
运行结果:struct shit0 * __ptr64struct shit0
{};
struct shit1 : shit0
{};void Test()
{using T = decltype(true ? std::declval<shit0*>() : std::declval<shit1*>());
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;}///
提取shit0和shit1的共同类型。
运行结果:struct shit0struct shit0
{};
struct shit1 : shit0
{};void Test()
{using T = decltype(true ? std::declval<shit0*>() : std::declval<shit1*>());
std::cout << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;}

貌似等于号=也有类似属性,之后再研究吧

http://www.lryc.cn/news/529316.html

相关文章:

  • MapReduce概述
  • 循环神经网络(RNN)+pytorch实现情感分析
  • Mac cursor设置jdk、Maven版本
  • WPS数据分析000005
  • CTF从入门到精通
  • Flutter使用Flavor实现切换环境和多渠道打包
  • Springboot如何使用面向切面编程AOP?
  • 51单片机(STC89C52)开发:点亮一个小灯
  • 基于MinIO的对象存储增删改查
  • Ubuntu Server 安装 XFCE4桌面
  • MySQL 存储函数:数据库的自定义函数
  • 代码随想录_栈与队列
  • 【微服务与分布式实践】探索 Sentinel
  • 深入研究异常处理机制
  • 【memgpt】letta 课程4:基于latta框架构建MemGpt代理并与之交互
  • 讯飞智作 AI 配音技术浅析(二):深度学习与神经网络
  • 基于单片机的超声波液位检测系统(论文+源码)
  • Autogen_core: test_code_executor.py
  • 从0开始使用面对对象C语言搭建一个基于OLED的图形显示框架
  • Java实现.env文件读取敏感数据
  • Go反射指南
  • Fullcalendar @fullcalendar/react 样式错乱丢失问题和导致页面卡顿崩溃问题
  • 【电工基础】4.低压电器元件,漏电保护器,熔断器,中间继电器
  • 有限元分析学习——Anasys Workbanch第一阶段笔记梳理
  • C++中常用的十大排序方法之1——冒泡排序
  • vscode+WSL2(ubuntu22.04)+pytorch+conda+cuda+cudnn安装系列
  • 手撕Diffusion系列 - 第十一期 - lora微调 - 基于Stable Diffusion(代码)
  • 【Block总结】OutlookAttention注意力,捕捉细节和局部特征|即插即用
  • 网络攻防实战指北专栏讲解大纲与网络安全法
  • 【已解决】windows7虚拟机安装VMtools频繁报错