User-defined conversions sequence

Clash Royale CLAN TAG#URR8PPP
up vote
14
down vote
favorite
Before I studied the explicit keyword, my teacher said: "compiler doesn't execute consecutive user defined conversion". If it is true, are there any errors in my code? Or have I misunderstood my teacher? I'm working in VS2017.
#include<iostream>
#include <string>
class Myclass
public:
Myclass()
std::cout << "Myclass" << std::endl;
;
class Myclass1
public:
Myclass1(Myclass m)
std::cout << "Myclass1" << std::endl;
;
class Myclass2
public:
Myclass2(Myclass1 m)
std::cout << "Myclass2" << std::endl;
;
int main()
Myclass2 m2 = Myclass;
c++ type-conversion implicit-conversion
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
14
down vote
favorite
Before I studied the explicit keyword, my teacher said: "compiler doesn't execute consecutive user defined conversion". If it is true, are there any errors in my code? Or have I misunderstood my teacher? I'm working in VS2017.
#include<iostream>
#include <string>
class Myclass
public:
Myclass()
std::cout << "Myclass" << std::endl;
;
class Myclass1
public:
Myclass1(Myclass m)
std::cout << "Myclass1" << std::endl;
;
class Myclass2
public:
Myclass2(Myclass1 m)
std::cout << "Myclass2" << std::endl;
;
int main()
Myclass2 m2 = Myclass;
c++ type-conversion implicit-conversion
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
14
down vote
favorite
up vote
14
down vote
favorite
Before I studied the explicit keyword, my teacher said: "compiler doesn't execute consecutive user defined conversion". If it is true, are there any errors in my code? Or have I misunderstood my teacher? I'm working in VS2017.
#include<iostream>
#include <string>
class Myclass
public:
Myclass()
std::cout << "Myclass" << std::endl;
;
class Myclass1
public:
Myclass1(Myclass m)
std::cout << "Myclass1" << std::endl;
;
class Myclass2
public:
Myclass2(Myclass1 m)
std::cout << "Myclass2" << std::endl;
;
int main()
Myclass2 m2 = Myclass;
c++ type-conversion implicit-conversion
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Before I studied the explicit keyword, my teacher said: "compiler doesn't execute consecutive user defined conversion". If it is true, are there any errors in my code? Or have I misunderstood my teacher? I'm working in VS2017.
#include<iostream>
#include <string>
class Myclass
public:
Myclass()
std::cout << "Myclass" << std::endl;
;
class Myclass1
public:
Myclass1(Myclass m)
std::cout << "Myclass1" << std::endl;
;
class Myclass2
public:
Myclass2(Myclass1 m)
std::cout << "Myclass2" << std::endl;
;
int main()
Myclass2 m2 = Myclass;
c++ type-conversion implicit-conversion
c++ type-conversion implicit-conversion
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
Evg
3,69221334
3,69221334
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked yesterday
User8500049
864
864
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
User8500049 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
10
down vote
accepted
compiler doesn't execute consecutive user defined conversion
Your teacher is right. In your code sample it means Myclass cannot be converted to Myclass1 when you assign in:
Myclass2 m2 = Myclass;
Because constructor expects Myclass1 when creating Myclass2, and compiler cannot consecutively convert Myclass to Myclass1 and then use it for creating Myclass2. But if you have following line:
Myclass1 m2 = Myclass;
It will work, because constructor of Myclass1 takes Myclass as argument.
Update:
You may ask why this works:
Myclass2 m2 Myclass;
Because in this case, constructor is called and conversion can be done implicitly unless you declare Myclass1 as explicit which will fail code compilation (Thanks Fureeish for reminder), but in:
Myclass2 m2 = Myclass;
is like calling copy-constructor which needs reference. so if you write it like this, it will work:
Myclass2 m2 = Myclass1(Myclass);
As EVG mentioned, Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
2
Worth noting that OP mentionedexplicit. In your first example in your edit, there is a temporaryMyclass1object created implicitely fromMyclassinstance (that's why there is aMyclass2constructor invoked, which requiresMyclass1object). If you makeMyclass1's constructorexplicit, it will fail
– Fureeish
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
Myclass2 m2 = Myclass;is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
– Evg
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
add a comment |
up vote
6
down vote
The line
Myclass2 m2 = Myclass;
means copy-initialization. Citing cppreference.com:
If
Tis a class type, and the cv-unqualified version of the type ofotheris notTor derived fromT[...], user-defined conversion sequences that can convert from the type ofothertoT[...] are examined and the best one is selected through overload resolution.
Citing further:
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call.
So, Myclass2 m2 = Myclass; is not acceptable because it would involve two user-defined conversions.
Now let's take a look at
Myclass2 m2 Myclass;
suggested in Afshin's answer. This is a direct-initialization. The rules are different:
The constructors of
Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
The constructor of Myclass2 accepts Myclass1, and you need one user-defined conversion to get Myclass1 from Myclass. Hence, it compiles.
Note that in VS copy-initilization is treated like direct-initilization if conformance mode (/premissive-) is not activated (by default). So, VS accepts Myclass2 m2 = Myclass; treating it as direct-initilization. See this document for examples.
add a comment |
up vote
2
down vote
The other answers are burying the lede: The code you’ve written is indeed invalid. MSVC accepts it by default, but MSVC is wrong to do so. You can force MSVC to be stricter by using the command line switch /permissive-. (You should use that switch.)
Other compilers (GCC, clang), reject it.
All compilers accept the code once you change the copy initialisation to direct initialisation as shown in the other answers.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
compiler doesn't execute consecutive user defined conversion
Your teacher is right. In your code sample it means Myclass cannot be converted to Myclass1 when you assign in:
Myclass2 m2 = Myclass;
Because constructor expects Myclass1 when creating Myclass2, and compiler cannot consecutively convert Myclass to Myclass1 and then use it for creating Myclass2. But if you have following line:
Myclass1 m2 = Myclass;
It will work, because constructor of Myclass1 takes Myclass as argument.
Update:
You may ask why this works:
Myclass2 m2 Myclass;
Because in this case, constructor is called and conversion can be done implicitly unless you declare Myclass1 as explicit which will fail code compilation (Thanks Fureeish for reminder), but in:
Myclass2 m2 = Myclass;
is like calling copy-constructor which needs reference. so if you write it like this, it will work:
Myclass2 m2 = Myclass1(Myclass);
As EVG mentioned, Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
2
Worth noting that OP mentionedexplicit. In your first example in your edit, there is a temporaryMyclass1object created implicitely fromMyclassinstance (that's why there is aMyclass2constructor invoked, which requiresMyclass1object). If you makeMyclass1's constructorexplicit, it will fail
– Fureeish
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
Myclass2 m2 = Myclass;is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
– Evg
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
add a comment |
up vote
10
down vote
accepted
compiler doesn't execute consecutive user defined conversion
Your teacher is right. In your code sample it means Myclass cannot be converted to Myclass1 when you assign in:
Myclass2 m2 = Myclass;
Because constructor expects Myclass1 when creating Myclass2, and compiler cannot consecutively convert Myclass to Myclass1 and then use it for creating Myclass2. But if you have following line:
Myclass1 m2 = Myclass;
It will work, because constructor of Myclass1 takes Myclass as argument.
Update:
You may ask why this works:
Myclass2 m2 Myclass;
Because in this case, constructor is called and conversion can be done implicitly unless you declare Myclass1 as explicit which will fail code compilation (Thanks Fureeish for reminder), but in:
Myclass2 m2 = Myclass;
is like calling copy-constructor which needs reference. so if you write it like this, it will work:
Myclass2 m2 = Myclass1(Myclass);
As EVG mentioned, Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
2
Worth noting that OP mentionedexplicit. In your first example in your edit, there is a temporaryMyclass1object created implicitely fromMyclassinstance (that's why there is aMyclass2constructor invoked, which requiresMyclass1object). If you makeMyclass1's constructorexplicit, it will fail
– Fureeish
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
Myclass2 m2 = Myclass;is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
– Evg
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
compiler doesn't execute consecutive user defined conversion
Your teacher is right. In your code sample it means Myclass cannot be converted to Myclass1 when you assign in:
Myclass2 m2 = Myclass;
Because constructor expects Myclass1 when creating Myclass2, and compiler cannot consecutively convert Myclass to Myclass1 and then use it for creating Myclass2. But if you have following line:
Myclass1 m2 = Myclass;
It will work, because constructor of Myclass1 takes Myclass as argument.
Update:
You may ask why this works:
Myclass2 m2 Myclass;
Because in this case, constructor is called and conversion can be done implicitly unless you declare Myclass1 as explicit which will fail code compilation (Thanks Fureeish for reminder), but in:
Myclass2 m2 = Myclass;
is like calling copy-constructor which needs reference. so if you write it like this, it will work:
Myclass2 m2 = Myclass1(Myclass);
As EVG mentioned, Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
compiler doesn't execute consecutive user defined conversion
Your teacher is right. In your code sample it means Myclass cannot be converted to Myclass1 when you assign in:
Myclass2 m2 = Myclass;
Because constructor expects Myclass1 when creating Myclass2, and compiler cannot consecutively convert Myclass to Myclass1 and then use it for creating Myclass2. But if you have following line:
Myclass1 m2 = Myclass;
It will work, because constructor of Myclass1 takes Myclass as argument.
Update:
You may ask why this works:
Myclass2 m2 Myclass;
Because in this case, constructor is called and conversion can be done implicitly unless you declare Myclass1 as explicit which will fail code compilation (Thanks Fureeish for reminder), but in:
Myclass2 m2 = Myclass;
is like calling copy-constructor which needs reference. so if you write it like this, it will work:
Myclass2 m2 = Myclass1(Myclass);
As EVG mentioned, Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
edited yesterday
answered yesterday
Afshin
2,5251623
2,5251623
2
Worth noting that OP mentionedexplicit. In your first example in your edit, there is a temporaryMyclass1object created implicitely fromMyclassinstance (that's why there is aMyclass2constructor invoked, which requiresMyclass1object). If you makeMyclass1's constructorexplicit, it will fail
– Fureeish
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
Myclass2 m2 = Myclass;is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
– Evg
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
add a comment |
2
Worth noting that OP mentionedexplicit. In your first example in your edit, there is a temporaryMyclass1object created implicitely fromMyclassinstance (that's why there is aMyclass2constructor invoked, which requiresMyclass1object). If you makeMyclass1's constructorexplicit, it will fail
– Fureeish
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
Myclass2 m2 = Myclass;is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.
– Evg
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
2
2
Worth noting that OP mentioned
explicit. In your first example in your edit, there is a temporary Myclass1 object created implicitely from Myclass instance (that's why there is a Myclass2 constructor invoked, which requires Myclass1 object). If you make Myclass1's constructor explicit, it will fail– Fureeish
yesterday
Worth noting that OP mentioned
explicit. In your first example in your edit, there is a temporary Myclass1 object created implicitely from Myclass instance (that's why there is a Myclass2 constructor invoked, which requires Myclass1 object). If you make Myclass1's constructor explicit, it will fail– Fureeish
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
@Fureeish I will add your comment.
– Afshin
yesterday
Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.– Evg
yesterday
Myclass2 m2 = Myclass; is accepted by VS 2017 if the conformance mode (/permissive-) is not activated.– Evg
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
@Evg I didn't know that. I will add it too.
– Afshin
yesterday
add a comment |
up vote
6
down vote
The line
Myclass2 m2 = Myclass;
means copy-initialization. Citing cppreference.com:
If
Tis a class type, and the cv-unqualified version of the type ofotheris notTor derived fromT[...], user-defined conversion sequences that can convert from the type ofothertoT[...] are examined and the best one is selected through overload resolution.
Citing further:
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call.
So, Myclass2 m2 = Myclass; is not acceptable because it would involve two user-defined conversions.
Now let's take a look at
Myclass2 m2 Myclass;
suggested in Afshin's answer. This is a direct-initialization. The rules are different:
The constructors of
Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
The constructor of Myclass2 accepts Myclass1, and you need one user-defined conversion to get Myclass1 from Myclass. Hence, it compiles.
Note that in VS copy-initilization is treated like direct-initilization if conformance mode (/premissive-) is not activated (by default). So, VS accepts Myclass2 m2 = Myclass; treating it as direct-initilization. See this document for examples.
add a comment |
up vote
6
down vote
The line
Myclass2 m2 = Myclass;
means copy-initialization. Citing cppreference.com:
If
Tis a class type, and the cv-unqualified version of the type ofotheris notTor derived fromT[...], user-defined conversion sequences that can convert from the type ofothertoT[...] are examined and the best one is selected through overload resolution.
Citing further:
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call.
So, Myclass2 m2 = Myclass; is not acceptable because it would involve two user-defined conversions.
Now let's take a look at
Myclass2 m2 Myclass;
suggested in Afshin's answer. This is a direct-initialization. The rules are different:
The constructors of
Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
The constructor of Myclass2 accepts Myclass1, and you need one user-defined conversion to get Myclass1 from Myclass. Hence, it compiles.
Note that in VS copy-initilization is treated like direct-initilization if conformance mode (/premissive-) is not activated (by default). So, VS accepts Myclass2 m2 = Myclass; treating it as direct-initilization. See this document for examples.
add a comment |
up vote
6
down vote
up vote
6
down vote
The line
Myclass2 m2 = Myclass;
means copy-initialization. Citing cppreference.com:
If
Tis a class type, and the cv-unqualified version of the type ofotheris notTor derived fromT[...], user-defined conversion sequences that can convert from the type ofothertoT[...] are examined and the best one is selected through overload resolution.
Citing further:
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call.
So, Myclass2 m2 = Myclass; is not acceptable because it would involve two user-defined conversions.
Now let's take a look at
Myclass2 m2 Myclass;
suggested in Afshin's answer. This is a direct-initialization. The rules are different:
The constructors of
Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
The constructor of Myclass2 accepts Myclass1, and you need one user-defined conversion to get Myclass1 from Myclass. Hence, it compiles.
Note that in VS copy-initilization is treated like direct-initilization if conformance mode (/premissive-) is not activated (by default). So, VS accepts Myclass2 m2 = Myclass; treating it as direct-initilization. See this document for examples.
The line
Myclass2 m2 = Myclass;
means copy-initialization. Citing cppreference.com:
If
Tis a class type, and the cv-unqualified version of the type ofotheris notTor derived fromT[...], user-defined conversion sequences that can convert from the type ofothertoT[...] are examined and the best one is selected through overload resolution.
Citing further:
A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call.
So, Myclass2 m2 = Myclass; is not acceptable because it would involve two user-defined conversions.
Now let's take a look at
Myclass2 m2 Myclass;
suggested in Afshin's answer. This is a direct-initialization. The rules are different:
The constructors of
Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
The constructor of Myclass2 accepts Myclass1, and you need one user-defined conversion to get Myclass1 from Myclass. Hence, it compiles.
Note that in VS copy-initilization is treated like direct-initilization if conformance mode (/premissive-) is not activated (by default). So, VS accepts Myclass2 m2 = Myclass; treating it as direct-initilization. See this document for examples.
edited yesterday
answered yesterday
Evg
3,69221334
3,69221334
add a comment |
add a comment |
up vote
2
down vote
The other answers are burying the lede: The code you’ve written is indeed invalid. MSVC accepts it by default, but MSVC is wrong to do so. You can force MSVC to be stricter by using the command line switch /permissive-. (You should use that switch.)
Other compilers (GCC, clang), reject it.
All compilers accept the code once you change the copy initialisation to direct initialisation as shown in the other answers.
add a comment |
up vote
2
down vote
The other answers are burying the lede: The code you’ve written is indeed invalid. MSVC accepts it by default, but MSVC is wrong to do so. You can force MSVC to be stricter by using the command line switch /permissive-. (You should use that switch.)
Other compilers (GCC, clang), reject it.
All compilers accept the code once you change the copy initialisation to direct initialisation as shown in the other answers.
add a comment |
up vote
2
down vote
up vote
2
down vote
The other answers are burying the lede: The code you’ve written is indeed invalid. MSVC accepts it by default, but MSVC is wrong to do so. You can force MSVC to be stricter by using the command line switch /permissive-. (You should use that switch.)
Other compilers (GCC, clang), reject it.
All compilers accept the code once you change the copy initialisation to direct initialisation as shown in the other answers.
The other answers are burying the lede: The code you’ve written is indeed invalid. MSVC accepts it by default, but MSVC is wrong to do so. You can force MSVC to be stricter by using the command line switch /permissive-. (You should use that switch.)
Other compilers (GCC, clang), reject it.
All compilers accept the code once you change the copy initialisation to direct initialisation as shown in the other answers.
answered yesterday
Konrad Rudolph
390k1017671020
390k1017671020
add a comment |
add a comment |
User8500049 is a new contributor. Be nice, and check out our Code of Conduct.
User8500049 is a new contributor. Be nice, and check out our Code of Conduct.
User8500049 is a new contributor. Be nice, and check out our Code of Conduct.
User8500049 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296622%2fuser-defined-conversions-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown