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

无用的知识又增加了:is_assignable means?

 std::pair的默认operator=被delete掉了,取而代之的是两个enable_if版本。

为什么这么设计,我的理解是在std::map里,已经保存的元素的key值是不能被修改的,比如

注意,下面的代码会修改key值,编译时出现错误:
两个enable_if版本推导失败,最后落到了:pair& operator=(const volatile pair&) = delete;
这个版本,但这个版本又被删除了。所以出现了编译错误。{std::map<int, int> m;//初始化auto it = m.begin();*it = std::make_pair(1, 1); //问题就出在这里
}就会提示:
error C2679: 二进制“=”: 没有找到接受“std::pair<int,int>”类型的右操作数的运算符(或没有可接受的转换)
1>d:\devtools\vs2017\vc\tools\msvc\14.16.27023\include\utility(276): note: 可能是“std::pair<const _Kty,_Ty> &std::pair<const _Kty,_Ty>::operator =(volatile const std::pair<const _Kty,_Ty> &)”
1>        with
1>        [
1>            _Kty=int,
1>            _Ty=int
1>        ]
1>c:\work\hchx\ceripipe\codestudy\consoleapplication2\consoleapplication1\consoleapplication1.cpp(2114): note: 尝试匹配参数列表“(std::pair<const _Kty,_Ty>, std::pair<int,int>)”时
1>        with
1>        [
1>            _Kty=int,
1>            _Ty=int
1>        ]

代码:
std::pair<int, int> data0 = std::make_pair(1, 2);
std::pair<const int, int> data1 = std::make_pair(3, 4);
data0 = data1;堆栈中可以看到推导结果:
1.exe!std::pair<int,int>::operator=<int const ,int,0>
(const std::pair<int const ,int> & _Right={...}) 行 288	C++推导出的符号,
_Ty1和_Other1类型是:const int
_Ty2和_Other2类型是:int查看pair的operator=的enable_if不分,
is_assignable<_Ty1&, const _Other1&>怎么算,替换下来就是:
is_assignable<const int&, const const int&>
看到const const int&,居然有两个const,能行吗?考虑到is_asignable的实现,也就是相当于,
{using T = decltype(declval<int&>() = declval<const const int&>());int b = 0;T a = b;std::ignore = a;
}
vs2015下编译成功。神奇。
而is_assignable<_Ty1&, const _Other1&>就是:is_assignable<int&, const int&>
{using T = decltype(declval<int&>() = declval<const int&>());int b = 0;T a = b;std::ignore = a;
}
vs2015下也能编译成功。std::cout << boost::typeindex::type_id_with_cvr<const const int&>().pretty_name() << std::endl;
打印结果是:int const & __ptr64所以,enable_if_t<conjunction_v<is_assignable<_Ty1&, const _Other1&>,is_assignable<_Ty2&, const _Other2&>>, int>推导成功,
相当于:enable_if_t<conjunction_v<true,true>, int> 
-> enable_if_t<true, int> 
-> int所以才走到了下面的operator=的版本。结合源码:pair& operator=(const volatile pair&) = delete; //默认的版本已被删除template<class _Other1 = _Ty1, //_Ty1:const int, _Other1: const intclass _Other2 = _Ty2, //_Ty2:int      , _Other2: intenable_if_t<conjunction_v<is_assignable<const int&, const const int&>,is_assignable<int&      , const int&>>, int> = 0>pair& operator=(const pair<const int, int>& _Right)_NOEXCEPT_COND(is_nothrow_assignable_v<_Ty1&, const _Other1&>&& is_nothrow_assignable_v<_Ty2&, const _Other2&>)	// strengthened{first = _Right.first;second = _Right.second;return (*this);}

How to interpret declval<_Dest>() = declval<_Src>() in is_assignable

I am trying to figure out how to interpret declval<_Dest>() = declval<_Src>() in the implementation of is_assignable.

declval turns a type into a reference. Given that, I translate the expression into one of the following four possibilities:

  1. _Dest&& = _Src&&
  2. _Dest&& = _Src&
  3. _Dest& = _Src&&
  4. _Dest& = _Src&

I then created two helper functions.

template <typename T> T rvalue();
template <typename T> T& lvalue();

My understanding is the four expressions can be realized by using the template functions.

  1. _Dest&& = _Src&& -----> rvalue<_Dest>() = rvalue<_Src>()

Same goes for the other three.

Then I simulated decltype(declval<_Dest>() = declval<_Src>(), ..) by compiling the templated function version of each of the possibilities for three pairs of concrete types.

  • _Dest=int, _Src=int. Compiler accepts #3 and #4. is_assignable returned true for #3 and #4. They agreed.
  • _Dest=int, _Src=double. Same result as
  • _Dest=double, _Src=int. For this one, the compiler and is_assignable didn't agree. Compiler again does not like assigning to rvalues. However, is_assignable returns true for all four possibilities.

My questions are

  • Did I interpret declval<_Dest>() = declval<_Src>() correctly? In order words, does this really translate into the four possibilities. If yes, can each one be mapped to a templated function expression?
  • Why the compiler and is_assignable disagree on the _Dest=double, _Src=int case?

Thanks.

  • c++
  • c++11

Share

Improve this question

Follow

edited Dec 6, 2013 at 5:34

ildjarn

63k99 gold badges131131 silver badges216216 bronze badges

asked Dec 6, 2013 at 2:35

Candy Chiu

6,67999 gold badges5151 silver badges7070 bronze badges

  • 1

    Which compiler is "the" compiler? At least one compiler appears to work precisely as you expect.

    – Igor Tandetnik

     Commented Dec 6, 2013 at 3:18 
  • The compiler is VC++2013

    – Candy Chiu

     Commented Dec 6, 2013 at 13:21 

一个回答:

std::declval is actually specified to be (C++11 §20.2.4 [declval] p1):

template <class T>
typename add_rvalue_reference<T>::type declval() noexcept;

The result of the reference collapsing rules (§8.3.2 [dcl.ref] p6) is that declval returns an lvalue reference when T is an lvalue reference type, and an rvalue reference otherwise. So yes, your interpretation is correct.

If your compiler thinks that double&& is assignable from any type, then it has a bug. §5.17 [expr.ass] p1 states:

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.

[emphasis mine].

Many programmers choose to emulate this behavior - assingment only to lvalues - with their own types by declaring the assignment operators with an lvalue reference qualifier:

class foo {foo& operator = (const foo&) & = default;foo& operator = (foo&&) & = default;
};

相关文章:

无用的知识又增加了:is_assignable means?

std::pair的默认operator被delete掉了&#xff0c;取而代之的是两个enable_if版本。 为什么这么设计&#xff0c;我的理解是在std::map里&#xff0c;已经保存的元素的key值是不能被修改的&#xff0c;比如 注意&#xff0c;下面的代码会修改key值&#xff0c;编译时出现错误…...

MOS的体二极管能通多大电流

第一个问题&#xff1a;MOS导通之后电流方向可以使任意的&#xff0c;既可以从D到S&#xff0c;也可以从S到D。 第二个问题&#xff1a;MOS里面的体二极管电流可以达到几百安培&#xff0c;这也就解释了MOS选型的时候很少考虑体二极管的最大电流&#xff0c;而是考虑DS之间电流…...

C语言【基础篇】之流程控制——掌握三大结构的奥秘

流程控制 &#x1f680;前言&#x1f99c;顺序结构&#x1f4af; 定义&#x1f4af;执行规则 &#x1f31f;选择结构&#x1f4af;if语句&#x1f4af;switch语句&#x1f4af;case穿透规则 &#x1f914;循环结构&#x1f4af;for循环&#x1f4af;while循环&#x1f4af;do -…...

Node.js下载安装及环境配置教程 (详细版)

Node.js&#xff1a;是一个基于 Chrome V8 引擎的 JavaScript 运行时&#xff0c;用于构建可扩展的网络应用程序。Node.js 使用事件驱动、非阻塞 I/O 模型&#xff0c;使其非常适合构建实时应用程序。 Node.js 提供了一种轻量、高效、可扩展的方式来构建网络应用程序&#xff0…...

【PySide6拓展】QCalendarWidget 日历控件

文章目录 PySide6拓展&#xff1a;QCalendarWidget 日历控件前言什么是QCalendarWidget&#xff1f;特点&#xff1a; 如何使用QCalendarWidget&#xff1f;1. 基本用法2. 获取当前选中的日期 QCalendarWidget 常用函数分类介绍1. 日期选择与管理函数2. 日期格式与显示函数3. 日…...

新型人工智能“黑帽”工具:GhostGPT带来的威胁与挑战

生成式人工智能的发展既带来了有益的生产力转型机会&#xff0c;也提供了被恶意利用的机会。 最近&#xff0c;Abnormal Security的研究人员发现了一个专门为网络犯罪创建的无审查AI聊天机器人——GhostGPT&#xff0c;是人工智能用于非法活动的新前沿&#xff0c;可以被用于网…...

【文星索引】搜索引擎项目测试报告

目录 一、项目背景二、 项目功能2.1 数据收集与索引2.2 API搜索功能2.3 用户体验与界面设计2.4 性能优化与维护 三、测试报告3.1 功能测试3.2 界面测试3.3 性能测试3.4 兼容性测试3.5 自动化测试 四、测试总结4.1 功能测试方面4.2 性能测试方面4.3 用户界面测试方面 一、项目背…...

【PostgreSQL内核学习 —— (WindowAgg(一))】

WindowAgg 窗口函数介绍WindowAgg理论层面源码层面WindowObjectData 结构体WindowStatePerFuncData 结构体WindowStatePerAggData 结构体eval_windowaggregates 函数update_frameheadpos 函数 声明&#xff1a;本文的部分内容参考了他人的文章。在编写过程中&#xff0c;我们尊…...

Maya快捷键

旋转 ALT鼠标左键 平移 ALT鼠标中键 缩放 ALT鼠标右键 / 滑动鼠标滚轮 切换视图 空格键 [Q] 选择状态 [W] 移动状态 [E] 旋转状态 [R] 绽放状态 1正常显示 2正常也圆滑同时显示 3圆滑显示 4线框显示 5材质显示 6贴图显示 7灯光显示 CTRLZ 撤销命令…...

Go反射指南

概念&#xff1a; 官方对此有个非常简明的介绍&#xff0c;两句话耐人寻味&#xff1a; 反射提供一种让程序检查自身结构的能力反射是困惑的源泉 第1条&#xff0c;再精确点的描述是“反射是一种检查interface变量的底层类型和值的机制”。 第2条&#xff0c;很有喜感的自嘲…...

深入理解 C 语言函数指针的高级用法:(void (*) (void *)) _IO_funlockfile

深入理解 C 语言函数指针的高级用法 函数指针是 C 语言中极具威力的特性&#xff0c;广泛用于实现回调、动态函数调用以及灵活的程序设计。然而&#xff0c;复杂的函数指针声明常常让即使是有经验的开发者也感到困惑。本文将从函数指针的基本概念出发&#xff0c;逐步解析复杂…...

【观察】甲骨文:以AI为中心,开启企业级平台架构“新革命”

知名科技杂志《连线》创始主编凯文凯利曾预测&#xff1a;“在未来的 100 年里&#xff0c;人工智能将超越任何一种人工力量&#xff0c;将人类引领到一个前所未有的时代。” 可以看到&#xff0c;随着近两年AIGC与大模型的快速爆发&#xff0c;AI在千万行业中的重要性愈发突出…...

react native在windows环境搭建并使用脚手架新建工程

截止到2024-1-11&#xff0c;使用的主要软件的版本如下&#xff1a; 软件实体版本react-native0.77.0react18.3.1react-native-community/cli15.0.1Android Studio2022.3.1 Patch3Android SDKAndroid SDK Platform 34 35Android SDKAndroid SDK Tools 34 35Android SDKIntel x…...

C语言从入门到进阶

视频&#xff1a;https://www.bilibili.com/video/BV1Vm4y1r7jY?spm_id_from333.788.player.switch&vd_sourcec988f28ad9af37435316731758625407&p23 //枚举常量 enum Sex{MALE,FEMALE,SECRET };printf("%d\n", MALE);//0 printf("%d\n", FEMALE…...

Python案例--养兔子

兔子繁殖问题是一个经典的数学问题&#xff0c;最早由意大利数学家斐波那契在13世纪提出。这个问题不仅在数学领域具有重要意义&#xff0c;还广泛应用于计算机科学、生物学和经济学等领域。本文将通过一个具体的Python程序&#xff0c;深入探讨兔子繁殖问题的建模和实现&#…...

Mybatis——sql映射文件中的增删查改

映射文件内的增删查改 准备工作 准备一张数据表&#xff0c;用于进行数据库的相关操作。新建maven工程&#xff0c; 导入mysql-connector-java和mybatis依赖。新建一个实体类&#xff0c;类的字段要和数据表的数据对应编写接口编写mybatis主配置文件 public class User {priva…...

goframe 博客分类文章模型文档 主要解决关联

goframe 博客文章模型文档 模型结构 (BlogArticleInfoRes) BlogArticleInfoRes 结构体代表系统中的一篇博客文章&#xff0c;包含完整的元数据和内容管理功能。 type BlogArticleInfoRes struct {Id uint orm:"id,primary" json:"id" …...

人工智能在医疗领域的应用有哪些?

人工智能在医疗领域的应用十分广泛&#xff0c;涵盖了诊断、治疗、药物研发等多个环节&#xff0c;以下是一些主要的应用&#xff1a; 医疗影像诊断 疾病识别&#xff1a;通过分析 X 光、CT、MRI 等影像&#xff0c;人工智能算法能够识别出肿瘤、结节、骨折等病变&#xff0c;…...

学习第七十六行

提高github下载速度方法 1.github转码云 2.https://github.com.cnpmjs.org com后面加东西 对于面试笔试&#xff0c;最好方法刷力扣&#xff0c;1000题包进大厂的...

C#System.Threading.Timer定时器意外回收注意事项

System.Threading.Timer定时器使用时会出现意外回收的情况。具体解释如下: 只要在使用 Timer,就必须保留对它的引用。对于任何托管对象,如果没有对 Timer 的引用,计时器会被垃圾回收。即使 Timer 仍处在活动状态,也会被回收。 实例对比测试 实例 定义两个类,其中一个…...

若依基本使用及改造记录

若依框架想必大家都了解得不少&#xff0c;不可否认这是一款及其简便易用的框架。 在某种情况下&#xff08;比如私活&#xff09;使用起来可谓是快得一匹。 在这里小兵结合自身实际使用情况&#xff0c;记录一下我对若依框架的使用和改造情况。 一、源码下载 前往码云进行…...

Java基础教程(007):方法的重载与方法的练习

文章目录 6.5 方法的重载6.6 方法练习数组遍历数组最大值 6.5 方法的重载 在 Java 中&#xff0c;方法的重载是指在同一个类中定义多个方法&#xff0c;这些方法具有相同的名称&#xff0c;但参数列表不同。方法的重载是一种实现多态的方式&#xff0c;允许一个方法名以不同的…...

Day27-【13003】短文,线性表两种基本实现方式空间效率、时间效率比较?兼顾优点的静态链表是什么?如何融入空闲单元链表来解决问题?

文章目录 本次内容总览第四节&#xff0c;两种基本实现方式概览两种基本实现方式的比较元素个数n大于多少时&#xff0c;使用顺序表存储的空间效率才会更高&#xff1f;时间效率比较&#xff1f;*、访问操作&#xff0c;也就是读运算&#xff0c;读操作1、插入&#xff0c;2、删…...

Linux 小火车

1.添加epel软件源 2.安装sl 3. 安装完成后输入&#xff1a; sl...

论文笔记(六十三)Understanding Diffusion Models: A Unified Perspective(四)

Understanding Diffusion Models: A Unified Perspective&#xff08;四&#xff09; 文章概括学习扩散噪声参数&#xff08;Learning Diffusion Noise Parameters&#xff09;三种等效的解释&#xff08;Three Equivalent Interpretations&#xff09; 文章概括 引用&#xf…...

python 统计相同像素值个数

目录 python 统计相同像素值个数 最大值附近的值 python 统计相同像素值个数 import cv2 import numpy as np import time from collections import Counter# 读取图像 image cv2.imread(mask16.jpg)# 将图像转换为灰度图像 gray_image cv2.cvtColor(image, cv2.COLOR_BGR2…...

YOLOv8:目标检测与实时应用的前沿探索

随着深度学习和计算机视觉技术的迅速发展&#xff0c;目标检测&#xff08;Object Detection&#xff09;一直是研究热点。YOLO&#xff08;You Only Look Once&#xff09;系列模型作为业界广受关注的目标检测框架&#xff0c;凭借其高效、实时的特点&#xff0c;一直迭代更新…...

docker配置mysql并使用mysql connector cpp编程

mysql 配置mysql使用docker 这里使用docker安装了&#xff0c;比较简洁&#xff0c;不想使用了直接就可以把容器删掉&#xff0c;首先获取下镜像&#xff0c;如下命令 docker pull container-registry.oracle.com/mysql/community-server这里直接默认使用最新版本的mysql了 …...

go理论知识——Go Channel 笔记 [特殊字符]

go理论知识——Go Channel 笔记 &#x1f4dd; 1. 基本概念 &#x1f9e0; 1.1 Channel 是什么&#xff1f; Channel 是 Go 语言中用于在不同 Goroutine 之间进行通信的机制。Channel 是类型安全的&#xff0c;意味着你只能发送和接收特定类型的数据。 1.2 Channel 的创建 …...

论文阅读笔记:MambaOut: Do We Really Need Mamba for Vision?

论文阅读笔记&#xff1a;MambaOut: Do We Really Need Mamba for Vision? 1 背景2 创新点3 方法4 模块4.1 Mamba适合什么任务4.2 视觉识别任务是否有很长的序列4.3 视觉任务是否需要因果token混合模式4.4 关于Mamba对于视觉的必要性假设 5 效果 论文&#xff1a;https://arxi…...