Is the copy-list-initialization for class object a user-defined conversion

自作多情 提交于 2020-04-17 22:54:10

问题


#include <iostream>
struct Data{
   Data(int){
   }
}
int main(){
  Data d = {0};  //#1
}

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]

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

[over.match.list]

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

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]

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

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?


回答1:


Type conversions of class objects can be specified by constructors and by conversion functions.

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.



来源:https://stackoverflow.com/questions/60414790/is-the-copy-list-initialization-for-class-object-a-user-defined-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!