Why is implicit conversion not ambiguous for non-primitive types?Can you use keyword explicit to prevent automatic conversion of method parameters?Why const for implicit conversion?Implicit conversion when overloading operators for template classesTemplate Constructor for Primitives, avoiding AmbiguityTemplate Type Deduction with Lambdasimplicit conversions from and to class typesConversion is ambiguous. Standard implicit conversion could not choose cast operatorGet type of implicit conversionImplicit conversion of stream to boolC++17: explicit conversion function vs explicit constructor + implicit conversions - have the rules changed?
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
Coordinate position not precise
Applicability of Single Responsibility Principle
voltage of sounds of mp3files
Ways to speed up user implemented RK4
Increase performance creating Mandelbrot set in python
What will be the benefits of Brexit?
How can a jailer prevent the Forge Cleric's Artisan's Blessing from being used?
Products and sum of cubes in Fibonacci
What would happen if the UK refused to take part in EU Parliamentary elections?
What would be the benefits of having both a state and local currencies?
Are there any comparative studies done between Ashtavakra Gita and Buddhim?
Will it be accepted, if there is no ''Main Character" stereotype?
Confused about a passage in Harry Potter y la piedra filosofal
There is only s̶i̶x̶t̶y one place he can be
If you attempt to grapple an opponent that you are hidden from, do they roll at disadvantage?
Can I use my Chinese passport to enter China after I acquired another citizenship?
How could Frankenstein get the parts for his _second_ creature?
What is the oldest known work of fiction?
Is HostGator storing my password in plaintext?
when is out of tune ok?
Can a monster with multiattack use this ability if they are missing a limb?
Have I saved too much for retirement so far?
Can I Retrieve Email Addresses from BCC?
Why is implicit conversion not ambiguous for non-primitive types?
Can you use keyword explicit to prevent automatic conversion of method parameters?Why const for implicit conversion?Implicit conversion when overloading operators for template classesTemplate Constructor for Primitives, avoiding AmbiguityTemplate Type Deduction with Lambdasimplicit conversions from and to class typesConversion is ambiguous. Standard implicit conversion could not choose cast operatorGet type of implicit conversionImplicit conversion of stream to boolC++17: explicit conversion function vs explicit constructor + implicit conversions - have the rules changed?
Given a simple class template with multiple implicit conversion functions (non-explicit constructor and conversion operator), as in the following example:
template<class T>
class Foo
private:
T m_value;
public:
Foo();
Foo(const T& value):
m_value(value)
operator T() const
return m_value;
bool operator==(const Foo<T>& other) const
return m_value == other.m_value;
;
struct Bar
bool m;
bool operator==(const Bar& other) const
return false;
;
int main(int argc, char *argv[])
Foo<bool> a (true);
bool b = false;
if(a == b)
// This is ambiguous
Foo<int> c (1);
int d = 2;
if(c == d)
// This is ambiguous
Foo<Bar> e (Bartrue);
Bar f = false;
if(e == f)
// This is not ambiguous. Why?
The comparison operators involving primitive types (bool
, int
) are ambiguous, as expected - the compiler does not know whether it should use the conversion operator to convert the left-hand template class instance to a primitive type or use the conversion constructor to convert the right-hand primitive type to the expected class template instance.
However, the last comparison, involving a simple struct
, is not ambiguous. Why? Which conversion function will be used?
Tested with compiler msvc 15.9.7.
c++ templates c++17 implicit-conversion ambiguous
add a comment |
Given a simple class template with multiple implicit conversion functions (non-explicit constructor and conversion operator), as in the following example:
template<class T>
class Foo
private:
T m_value;
public:
Foo();
Foo(const T& value):
m_value(value)
operator T() const
return m_value;
bool operator==(const Foo<T>& other) const
return m_value == other.m_value;
;
struct Bar
bool m;
bool operator==(const Bar& other) const
return false;
;
int main(int argc, char *argv[])
Foo<bool> a (true);
bool b = false;
if(a == b)
// This is ambiguous
Foo<int> c (1);
int d = 2;
if(c == d)
// This is ambiguous
Foo<Bar> e (Bartrue);
Bar f = false;
if(e == f)
// This is not ambiguous. Why?
The comparison operators involving primitive types (bool
, int
) are ambiguous, as expected - the compiler does not know whether it should use the conversion operator to convert the left-hand template class instance to a primitive type or use the conversion constructor to convert the right-hand primitive type to the expected class template instance.
However, the last comparison, involving a simple struct
, is not ambiguous. Why? Which conversion function will be used?
Tested with compiler msvc 15.9.7.
c++ templates c++17 implicit-conversion ambiguous
3
Theexplicit
keyword is a nice thing. You should research it.
– Jesper Juhl
Mar 19 at 20:10
e == f
will be ambiguous in C++20, for what its worth.
– Barry
Mar 19 at 20:27
add a comment |
Given a simple class template with multiple implicit conversion functions (non-explicit constructor and conversion operator), as in the following example:
template<class T>
class Foo
private:
T m_value;
public:
Foo();
Foo(const T& value):
m_value(value)
operator T() const
return m_value;
bool operator==(const Foo<T>& other) const
return m_value == other.m_value;
;
struct Bar
bool m;
bool operator==(const Bar& other) const
return false;
;
int main(int argc, char *argv[])
Foo<bool> a (true);
bool b = false;
if(a == b)
// This is ambiguous
Foo<int> c (1);
int d = 2;
if(c == d)
// This is ambiguous
Foo<Bar> e (Bartrue);
Bar f = false;
if(e == f)
// This is not ambiguous. Why?
The comparison operators involving primitive types (bool
, int
) are ambiguous, as expected - the compiler does not know whether it should use the conversion operator to convert the left-hand template class instance to a primitive type or use the conversion constructor to convert the right-hand primitive type to the expected class template instance.
However, the last comparison, involving a simple struct
, is not ambiguous. Why? Which conversion function will be used?
Tested with compiler msvc 15.9.7.
c++ templates c++17 implicit-conversion ambiguous
Given a simple class template with multiple implicit conversion functions (non-explicit constructor and conversion operator), as in the following example:
template<class T>
class Foo
private:
T m_value;
public:
Foo();
Foo(const T& value):
m_value(value)
operator T() const
return m_value;
bool operator==(const Foo<T>& other) const
return m_value == other.m_value;
;
struct Bar
bool m;
bool operator==(const Bar& other) const
return false;
;
int main(int argc, char *argv[])
Foo<bool> a (true);
bool b = false;
if(a == b)
// This is ambiguous
Foo<int> c (1);
int d = 2;
if(c == d)
// This is ambiguous
Foo<Bar> e (Bartrue);
Bar f = false;
if(e == f)
// This is not ambiguous. Why?
The comparison operators involving primitive types (bool
, int
) are ambiguous, as expected - the compiler does not know whether it should use the conversion operator to convert the left-hand template class instance to a primitive type or use the conversion constructor to convert the right-hand primitive type to the expected class template instance.
However, the last comparison, involving a simple struct
, is not ambiguous. Why? Which conversion function will be used?
Tested with compiler msvc 15.9.7.
c++ templates c++17 implicit-conversion ambiguous
c++ templates c++17 implicit-conversion ambiguous
asked Mar 19 at 19:52
CybranCybran
1,082819
1,082819
3
Theexplicit
keyword is a nice thing. You should research it.
– Jesper Juhl
Mar 19 at 20:10
e == f
will be ambiguous in C++20, for what its worth.
– Barry
Mar 19 at 20:27
add a comment |
3
Theexplicit
keyword is a nice thing. You should research it.
– Jesper Juhl
Mar 19 at 20:10
e == f
will be ambiguous in C++20, for what its worth.
– Barry
Mar 19 at 20:27
3
3
The
explicit
keyword is a nice thing. You should research it.– Jesper Juhl
Mar 19 at 20:10
The
explicit
keyword is a nice thing. You should research it.– Jesper Juhl
Mar 19 at 20:10
e == f
will be ambiguous in C++20, for what its worth.– Barry
Mar 19 at 20:27
e == f
will be ambiguous in C++20, for what its worth.– Barry
Mar 19 at 20:27
add a comment |
2 Answers
2
active
oldest
votes
According to [over.binary]/1
Thus, for any binary operator
@
,x@y
can be interpreted
as eitherx.operator@(y)
oroperator@(x,y)
.
According to this rule, in the case of e == f
, the compiler can only interpret it as e.operator==(f)
, not as f.operator==(e)
. So there is no ambiguity; the operator==
you defined as a member of Bar
is simply not a candidate for overload resolution.
In the case of a == b
and c == d
, the built-in candidate operator==(int, int)
(see [over.built]/13) competes with the operator==
defined as a member of Foo<T>
.
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
add a comment |
Operator overloads implemented as member functions don't allow for implicit conversion of their left-hand operand, which is the object on which they are called.
It always helps to write out the explicit call of an operator overload to better understand exactly what it does:
Foo<Bar> e (Bartrue);
Bar f = false;
// Pretty explicit: call the member function Foo<Bar>::operator==
if(e.operator ==(f)) /* ... */
This can't be confused with the comparison operator in Bar
, because it would require an implicit conversion of the left-hand side, which is impossible.
You can trigger an ambiguity similar to the ones you see with the built-in types when you define Bar
and its comparison operator like this:
struct Bar bool m; ;
// A free function allows conversion, this will be ambiguous:
bool operator==(const Bar&, const Bar&)
return false;
This is nicely demonstrated and explained in Scott Meyers's Effective C++, Item 24.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55249017%2fwhy-is-implicit-conversion-not-ambiguous-for-non-primitive-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to [over.binary]/1
Thus, for any binary operator
@
,x@y
can be interpreted
as eitherx.operator@(y)
oroperator@(x,y)
.
According to this rule, in the case of e == f
, the compiler can only interpret it as e.operator==(f)
, not as f.operator==(e)
. So there is no ambiguity; the operator==
you defined as a member of Bar
is simply not a candidate for overload resolution.
In the case of a == b
and c == d
, the built-in candidate operator==(int, int)
(see [over.built]/13) competes with the operator==
defined as a member of Foo<T>
.
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
add a comment |
According to [over.binary]/1
Thus, for any binary operator
@
,x@y
can be interpreted
as eitherx.operator@(y)
oroperator@(x,y)
.
According to this rule, in the case of e == f
, the compiler can only interpret it as e.operator==(f)
, not as f.operator==(e)
. So there is no ambiguity; the operator==
you defined as a member of Bar
is simply not a candidate for overload resolution.
In the case of a == b
and c == d
, the built-in candidate operator==(int, int)
(see [over.built]/13) competes with the operator==
defined as a member of Foo<T>
.
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
add a comment |
According to [over.binary]/1
Thus, for any binary operator
@
,x@y
can be interpreted
as eitherx.operator@(y)
oroperator@(x,y)
.
According to this rule, in the case of e == f
, the compiler can only interpret it as e.operator==(f)
, not as f.operator==(e)
. So there is no ambiguity; the operator==
you defined as a member of Bar
is simply not a candidate for overload resolution.
In the case of a == b
and c == d
, the built-in candidate operator==(int, int)
(see [over.built]/13) competes with the operator==
defined as a member of Foo<T>
.
According to [over.binary]/1
Thus, for any binary operator
@
,x@y
can be interpreted
as eitherx.operator@(y)
oroperator@(x,y)
.
According to this rule, in the case of e == f
, the compiler can only interpret it as e.operator==(f)
, not as f.operator==(e)
. So there is no ambiguity; the operator==
you defined as a member of Bar
is simply not a candidate for overload resolution.
In the case of a == b
and c == d
, the built-in candidate operator==(int, int)
(see [over.built]/13) competes with the operator==
defined as a member of Foo<T>
.
edited 2 days ago
answered Mar 19 at 20:04
BrianBrian
65.9k798186
65.9k798186
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
add a comment |
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
The way you worded the first sentence seems to imply that switching the operands around would also result in ambiguity, which is not the case.
– Rakete1111
Mar 20 at 7:50
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
@Rakete1111 Why does it seem to imply that?
– Brian
Mar 20 at 15:18
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
I'm not native but if you say "not as ..." it implies that the ... would be ambiguous
– Rakete1111
Mar 20 at 15:21
add a comment |
Operator overloads implemented as member functions don't allow for implicit conversion of their left-hand operand, which is the object on which they are called.
It always helps to write out the explicit call of an operator overload to better understand exactly what it does:
Foo<Bar> e (Bartrue);
Bar f = false;
// Pretty explicit: call the member function Foo<Bar>::operator==
if(e.operator ==(f)) /* ... */
This can't be confused with the comparison operator in Bar
, because it would require an implicit conversion of the left-hand side, which is impossible.
You can trigger an ambiguity similar to the ones you see with the built-in types when you define Bar
and its comparison operator like this:
struct Bar bool m; ;
// A free function allows conversion, this will be ambiguous:
bool operator==(const Bar&, const Bar&)
return false;
This is nicely demonstrated and explained in Scott Meyers's Effective C++, Item 24.
add a comment |
Operator overloads implemented as member functions don't allow for implicit conversion of their left-hand operand, which is the object on which they are called.
It always helps to write out the explicit call of an operator overload to better understand exactly what it does:
Foo<Bar> e (Bartrue);
Bar f = false;
// Pretty explicit: call the member function Foo<Bar>::operator==
if(e.operator ==(f)) /* ... */
This can't be confused with the comparison operator in Bar
, because it would require an implicit conversion of the left-hand side, which is impossible.
You can trigger an ambiguity similar to the ones you see with the built-in types when you define Bar
and its comparison operator like this:
struct Bar bool m; ;
// A free function allows conversion, this will be ambiguous:
bool operator==(const Bar&, const Bar&)
return false;
This is nicely demonstrated and explained in Scott Meyers's Effective C++, Item 24.
add a comment |
Operator overloads implemented as member functions don't allow for implicit conversion of their left-hand operand, which is the object on which they are called.
It always helps to write out the explicit call of an operator overload to better understand exactly what it does:
Foo<Bar> e (Bartrue);
Bar f = false;
// Pretty explicit: call the member function Foo<Bar>::operator==
if(e.operator ==(f)) /* ... */
This can't be confused with the comparison operator in Bar
, because it would require an implicit conversion of the left-hand side, which is impossible.
You can trigger an ambiguity similar to the ones you see with the built-in types when you define Bar
and its comparison operator like this:
struct Bar bool m; ;
// A free function allows conversion, this will be ambiguous:
bool operator==(const Bar&, const Bar&)
return false;
This is nicely demonstrated and explained in Scott Meyers's Effective C++, Item 24.
Operator overloads implemented as member functions don't allow for implicit conversion of their left-hand operand, which is the object on which they are called.
It always helps to write out the explicit call of an operator overload to better understand exactly what it does:
Foo<Bar> e (Bartrue);
Bar f = false;
// Pretty explicit: call the member function Foo<Bar>::operator==
if(e.operator ==(f)) /* ... */
This can't be confused with the comparison operator in Bar
, because it would require an implicit conversion of the left-hand side, which is impossible.
You can trigger an ambiguity similar to the ones you see with the built-in types when you define Bar
and its comparison operator like this:
struct Bar bool m; ;
// A free function allows conversion, this will be ambiguous:
bool operator==(const Bar&, const Bar&)
return false;
This is nicely demonstrated and explained in Scott Meyers's Effective C++, Item 24.
edited Mar 19 at 23:00
Cody Gray♦
195k35382471
195k35382471
answered Mar 19 at 20:10
lubgrlubgr
14.3k32052
14.3k32052
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55249017%2fwhy-is-implicit-conversion-not-ambiguous-for-non-primitive-types%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
3
The
explicit
keyword is a nice thing. You should research it.– Jesper Juhl
Mar 19 at 20:10
e == f
will be ambiguous in C++20, for what its worth.– Barry
Mar 19 at 20:27