Why is inheritance of a const/non-const function overload ambiguous?
I was trying to create two classes, the first with a non-const implementation of the functions, the second with a const implementation. Here is a small example:
class Base {
protected:
int some;
};
class A : public virtual Base {
const int& get() const {
return some;
}
};
class B : public virtual Base {
int& get() {
return some;
}
};
class C : public A, B {};
C test;
test.get(); // ambiguous
The call to the get
function is ambiguous. No matter that the const version needs to match more requirements. (Calling get
on const C
is ambiguous as well, but there is one possible function to call.)
Is there a reason for such behaviour in the standard? Thanks!
c++ inheritance overloading multiple-inheritance
add a comment |
I was trying to create two classes, the first with a non-const implementation of the functions, the second with a const implementation. Here is a small example:
class Base {
protected:
int some;
};
class A : public virtual Base {
const int& get() const {
return some;
}
};
class B : public virtual Base {
int& get() {
return some;
}
};
class C : public A, B {};
C test;
test.get(); // ambiguous
The call to the get
function is ambiguous. No matter that the const version needs to match more requirements. (Calling get
on const C
is ambiguous as well, but there is one possible function to call.)
Is there a reason for such behaviour in the standard? Thanks!
c++ inheritance overloading multiple-inheritance
stackoverflow.com/questions/5103543/…
– Jesper Juhl
Jan 18 at 17:10
add a comment |
I was trying to create two classes, the first with a non-const implementation of the functions, the second with a const implementation. Here is a small example:
class Base {
protected:
int some;
};
class A : public virtual Base {
const int& get() const {
return some;
}
};
class B : public virtual Base {
int& get() {
return some;
}
};
class C : public A, B {};
C test;
test.get(); // ambiguous
The call to the get
function is ambiguous. No matter that the const version needs to match more requirements. (Calling get
on const C
is ambiguous as well, but there is one possible function to call.)
Is there a reason for such behaviour in the standard? Thanks!
c++ inheritance overloading multiple-inheritance
I was trying to create two classes, the first with a non-const implementation of the functions, the second with a const implementation. Here is a small example:
class Base {
protected:
int some;
};
class A : public virtual Base {
const int& get() const {
return some;
}
};
class B : public virtual Base {
int& get() {
return some;
}
};
class C : public A, B {};
C test;
test.get(); // ambiguous
The call to the get
function is ambiguous. No matter that the const version needs to match more requirements. (Calling get
on const C
is ambiguous as well, but there is one possible function to call.)
Is there a reason for such behaviour in the standard? Thanks!
c++ inheritance overloading multiple-inheritance
c++ inheritance overloading multiple-inheritance
edited Jan 18 at 20:35
Boann
37k1290121
37k1290121
asked Jan 18 at 17:06
6yry6e6yry6e
934
934
stackoverflow.com/questions/5103543/…
– Jesper Juhl
Jan 18 at 17:10
add a comment |
stackoverflow.com/questions/5103543/…
– Jesper Juhl
Jan 18 at 17:10
stackoverflow.com/questions/5103543/…
– Jesper Juhl
Jan 18 at 17:10
stackoverflow.com/questions/5103543/…
– Jesper Juhl
Jan 18 at 17:10
add a comment |
2 Answers
2
active
oldest
votes
Ambiguity occurs when compiler tries to figure out to what entity does the name get
refer to, prior to overload resolution. It can be a name of function from class A or from class B. In order to build a list of overloads complier needs to select just one of the classes to pull functions from. In order to fix it you can bring that name from both of the base classes into derived class (and make them public):
class C : public A, public B { public: using A::get; public: using B::get; };
2
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
11
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
add a comment |
The problem is that you don't actually have one unified overload-set, in which the mutable variant would be unambiguously best, but two distinct overload-sets, in A
and B
, and the compiler will not automatically merge them.
Put
using A::get;
using B::get;
in C
to merge the overload-sets and thus resolve the ambiguity.
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%2f54258524%2fwhy-is-inheritance-of-a-const-non-const-function-overload-ambiguous%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
Ambiguity occurs when compiler tries to figure out to what entity does the name get
refer to, prior to overload resolution. It can be a name of function from class A or from class B. In order to build a list of overloads complier needs to select just one of the classes to pull functions from. In order to fix it you can bring that name from both of the base classes into derived class (and make them public):
class C : public A, public B { public: using A::get; public: using B::get; };
2
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
11
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
add a comment |
Ambiguity occurs when compiler tries to figure out to what entity does the name get
refer to, prior to overload resolution. It can be a name of function from class A or from class B. In order to build a list of overloads complier needs to select just one of the classes to pull functions from. In order to fix it you can bring that name from both of the base classes into derived class (and make them public):
class C : public A, public B { public: using A::get; public: using B::get; };
2
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
11
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
add a comment |
Ambiguity occurs when compiler tries to figure out to what entity does the name get
refer to, prior to overload resolution. It can be a name of function from class A or from class B. In order to build a list of overloads complier needs to select just one of the classes to pull functions from. In order to fix it you can bring that name from both of the base classes into derived class (and make them public):
class C : public A, public B { public: using A::get; public: using B::get; };
Ambiguity occurs when compiler tries to figure out to what entity does the name get
refer to, prior to overload resolution. It can be a name of function from class A or from class B. In order to build a list of overloads complier needs to select just one of the classes to pull functions from. In order to fix it you can bring that name from both of the base classes into derived class (and make them public):
class C : public A, public B { public: using A::get; public: using B::get; };
edited Jan 18 at 17:13
answered Jan 18 at 17:09
VTTVTT
25.4k42447
25.4k42447
2
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
11
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
add a comment |
2
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
11
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
2
2
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
An annoying C++ niggle!
– Lightness Races in Orbit
Jan 18 at 17:12
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
thanks a lot! c++ in his best... :)
– 6yry6e
Jan 18 at 17:19
11
11
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
An important C++ protection: if adding a function to a base class changed the overload set your code could quietly break; with this rule it breaks noisily.
– Pete Becker
Jan 18 at 17:28
add a comment |
The problem is that you don't actually have one unified overload-set, in which the mutable variant would be unambiguously best, but two distinct overload-sets, in A
and B
, and the compiler will not automatically merge them.
Put
using A::get;
using B::get;
in C
to merge the overload-sets and thus resolve the ambiguity.
add a comment |
The problem is that you don't actually have one unified overload-set, in which the mutable variant would be unambiguously best, but two distinct overload-sets, in A
and B
, and the compiler will not automatically merge them.
Put
using A::get;
using B::get;
in C
to merge the overload-sets and thus resolve the ambiguity.
add a comment |
The problem is that you don't actually have one unified overload-set, in which the mutable variant would be unambiguously best, but two distinct overload-sets, in A
and B
, and the compiler will not automatically merge them.
Put
using A::get;
using B::get;
in C
to merge the overload-sets and thus resolve the ambiguity.
The problem is that you don't actually have one unified overload-set, in which the mutable variant would be unambiguously best, but two distinct overload-sets, in A
and B
, and the compiler will not automatically merge them.
Put
using A::get;
using B::get;
in C
to merge the overload-sets and thus resolve the ambiguity.
answered Jan 18 at 17:13
DeduplicatorDeduplicator
34.5k64888
34.5k64888
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%2f54258524%2fwhy-is-inheritance-of-a-const-non-const-function-overload-ambiguous%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
stackoverflow.com/questions/5103543/…
– Jesper Juhl
Jan 18 at 17:10