- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一段代码,其中同时包含转换构造函数和转换运算符。
#include <iostream>
struct ClassFloat;
struct ClassInt{
int value;
ClassInt(int c) : value(c){std::cout << "From integer\n";};
ClassInt(ClassFloat x);
//explicit ClassInt(ClassFloat x);
};
struct ClassFloat{
float val;
explicit operator ClassInt() {std::cout << "Conversion operator called\n"; return ClassInt{999};}
//operator ClassInt() { std::cout << "Conversion operator called\n"; return ClassInt{999};}
};
ClassInt::ClassInt(ClassFloat x){
std::cout << "Conversion constructor called!\n";
value = (int)x.val;
}
int main(){
ClassFloat floatObj{3.5f};
ClassInt instance1 = floatObj; // (1)
ClassInt instance2 = (ClassInt)floatObj; // (2)
return 1;
}
我不明白为什么在第二个场景的第二个表达式中没有调用转换运算符。
我还期望在第四种情况(类似于第一种情况)中出现歧义错误,但选择了构造函数。
我使用带有 -pedantic 和 -std=c++17 标志的 g++ 7.4.0 进行编译。
最佳答案
首先,c-style cast表演 static_cast
, 然后
(强调我的)
1) If there is an implicit conversion sequence from
expression
tonew_type
, or if overload resolution for a direct initialization of an object or reference of typenew_type
fromexpression
would find at least one viable function, thenstatic_cast<new_type>(expression)
returns the imaginary variableTemp
initialized as if bynew_type Temp(expression);
, which may involve implicit conversions, a call to the constructor ofnew_type
or a call to a user-defined conversion operator.
所以给出(ClassInt)floatObj;
(初始化为 ClassInt Temp(floatObj);
),转换构造函数将始终是首选,它将直接用于构造 ClassInt
.应用转换运算符时需要从 floatObj
进行隐式转换至 ClassInt
(然后在概念上复制初始化临时 ClassInt
)。
对于第一种情况,您观察到的结果似乎与上述总结略有不同,
- If both are non-explicit. I get a compiler error saying it is ambiguous.
只有第一个表达式导致模棱两可的问题,第二个表达式将使用转换构造函数。
顺便说一句:我试过 gcc 7.3.0 , 给出了预期的结果。
回答你的问题,
I didn't understand why conversion operator wasn't called at the second expression in the second scenario.
因为转换构造函数更适合第二个表达式(c-ctyle 转换)。
I was also expecting an ambiguity error in the fourth scenario (similar to the first scenario) but constructor was picked.
同上,第2个表达式优先使用转换构造函数;第一个表达式会导致错误,因为转换构造函数和转换运算符都标记为 explicit
.
还要注意第一个表达式需要隐式转换,那么无论是转换构造函数还是转换运算符都标记为explicit
确实很重要。另一方面,第二个表达式是 explicit conversion ,这并不关心。
关于c++ - 存在转换构造函数和运算符并且涉及显式时的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57373901/
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个在 ab 时间内运行的算法,其中 a 和 b 都是单独的输入。 我的算法还是多项式时间复杂度算法还是nn?我认为 nn 不是多项式,但我仍然不确定。 我看到 n 算法的阶乘仍然评估为 nn 复
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个回答) 1年前关闭。 由于“is.na(NA)”返回真,“
假设我有一个具有以下结构的 Pandas 数据框: df = pd.DataFrame(dict(a=["x", "x", "y"], b=[0, 1, 1], c=[1, 2, 2])) 我想按 a
谁能帮我处理一些相当复杂的 Django 查询? 这些是我的模型: class County(models.Model): name = models.CharField(max_length
我想从某个表中选择一行并根据另一个表对结果进行排序。 这是我的表: lang1_words: word_id - word statuses: word_id - status 在每个表中 word_
我是单元测试的新手,所以请对我宽容一些。我有一些查询 RESTful API 的模块。我发现在每个测试套件中,我都使用几行代码来启动一个简单的 ExpressJS Web 服务器,以模拟一些我可以从测
假设我有以下代码: var blinker = function(element){ if(stopped){ return; } else { var sampleMappi
我正在用 JavaScript 制作一个选择你自己的冒险风格的游戏,在本节中: evade = prompt("Go out of your way to avoid them, just in ca
我的代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Events1 extends
我正在使用 scipy ode 来解决钟摆问题。 from scipy import * import matplotlib.pyplot as plt from scipy.integrate im
我有一个 Google 表格,用于收集客户的注册数据。收集的数据包括学生的姓名、学生选择参加的类(class)以及信用卡号。提交后,我会收到通知。收到通知后,我会转到我的 Google 表格并从信用卡
我需要定义一个操作的两个版本,定义略有不同。它是一系列包含Nat指数的成分。 open import Data.Nat data Hom : ℕ → ℕ → Set where id : (
我正在研究游戏引擎 http://ducttape-dev.org使用 boost 作为依赖项之一。有一天,当我正在编写一个链接到我的游戏引擎的测试应用程序时,OgreProcedural 的 Ext
我正在 Android 中制作一个表达式计算器,所以我想在实际计算答案之前检查字符串是否符合有效表达式的条件。 我在 Java 中试过这个正则表达式: ^\s*([-+]?)(\d+)(?:\s*([
我有以下 postgresql 查询(为便于阅读而简化): select * from a_view where a in (select * from a_function(a_input))
我开始更好地掌握 PostgreSQL 索引,但我遇到了 OR 条件的问题,我不知道如何优化我的索引以加快查询速度。 我有 6 个条件,当单独运行时,它们的成本似乎很小。下面是修剪查询的示例,包括查询
有谁知道为什么下面的代码接受诸如123-123-1234这样的答案: [1-9]\\d{2}-[1-9]\\d{2}-\\d{4} 我想到了代码,它只接受先接受 2 个数字,再接受 2 个数字,然后再
在使用 Java 1.8u40 打开带有提示类型和附加的 StringConverter 的组合框时,我遇到了以下错误。这可以追溯到执行 FXML 的团队留下的示例字符串,与 Controller 中
在 MySQL 中,我有三个不同的数据库 - 我们将它们称为 A、B 和 C。 是否可以执行涉及所有三个数据库(A、B、C)中的表的事务? (所有数据库都在同一服务器上) 最佳答案 是的,你可以。这是
我是一名优秀的程序员,十分优秀!