类对象的复制列表初始化是用户定义的转换吗 [英] Is the copy-list-initialization for class object a user-defined conversion
问题描述
#include <iostream>
struct Data{
Data(int){
}
}
int main(){
Data d = {0}; //#1
}
如上面的代码所示,#1调用是否包含用户定义的转换?在我对标准的理解中,我认为它不
对于复制列表初始化规则 [dcl.init.list]
As the above code show,Does the #1 invocation contain a user-defined conversion?In my understanding about the standard ,I think it does not
For copy-list-initialization rules [dcl.init.list]
否则,如果T是类类型,则考虑构造函数.枚举适用的构造函数,并通过重载解析([over.match],[over.match.list])选择最佳的构造函数.如果需要缩小转换(请参见下文)以转换任何参数,则程序格式不正确
Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution ([over.match], [over.match.list]). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed
如果找不到可行的初始值设定项列表构造函数,则再次执行重载解析,其中候选函数是T类的所有构造函数,并且参数列表由初始值设定项列表的元素组成
If no viable initializer-list constructor is found, overload resolution is performed again, where the candidate functions are all the constructors of the class T and the argument list consists of the elements of the initializer list
标准仅表示最佳匹配构造函数用于初始化使用initializer list
元素初始化的对象,它与copy-initialization不同(copy-initialization表示"用户定义" 可以从源类型转换为目标类型的转换序列",明确定义复制初始化需要用户定义的转换)
所以Data d = {0}; => Data d(0);
除了标准转换外没有用户定义的转换?我的理解正确吗?
但是,另外一个 [class.conv]
The standard only said the best match constructor is used to initialize the object which is initialized by using the element of initializer list
,it is different with copy-initialization(the copy-initialization say that "user-defined conversion sequences that can convert from the source type to the destination type",explicit define the copy-initialization need a user-defined conversion)
So Data d = {0}; => Data d(0);
there's no user-defined conversion other than standard conversions?Is my understanding right?
However another terms [class.conv]
类对象的类型转换可以由构造函数和转换函数指定.这些转换称为用户定义的转换,用于隐式类型转换(Clause [conv]),初始化和显式类型转换
Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (Clause [conv]), for initialization, and for explicit type conversions
以上术语表示如果初始化的目标类型是类类型并且需要使用构造函数或转换函数,则转换为用户定义的转换"
我对这些术语感到困惑,实际上Data d = {0};
是不是用户定义的转换?
the above term means if the initialized destination type is class type and need to use constructors or conversion functions,then the conversions are "user-defined conversions"
I'm confused by these terms,what actually the Data d = {0};
is a user-defined conversion or not?
推荐答案
类对象的类型转换可以由构造函数和转换函数指定.
Type conversions of class objects can be specified by constructors and by conversion functions.
用户定义的转换首先是类型转换.在初始化Data d = {0};
中,甚至没有类型转换,因此也没有用户定义的转换.
User-defined conversion is first a type conversion. In the initialization Data d = {0};
, there is even no type conversion, thus no user-defined conversion.
这篇关于类对象的复制列表初始化是用户定义的转换吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!