Overloading operator[] and NOT getting “lvalue required as left operand of assignment” errorPreventing copy construction and assignment of a return value referenceWhy doesn't Java offer operator overloading?What are the basic rules and idioms for operator overloading?Why is it required to return a reference from overloading bracket operators (OR: why is an lvalue not returned otherwise)?Should post increment operator overloading in c++ return const data typeerror C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)Unable to understand overloading of assignment operatorerror: lvalue required as unary ‘&’ operand when passing std::stringPriority Queue overloading error: “binary '<': no operator found which takes a left-hand operand of type.”Why is const required for 'operator>' but not for 'operator<'?separate handlers lvalue and rvalue [] operators
Why is oilcloth made with linseed oil?
What is the highest voltage from the power supply a Raspberry Pi 3 B can handle without getting damaged?
Should the party get XP for a monster they never attacked?
Umlaut character order when sorting
Is there a name for the trope when there is a moments dialogue when someone pauses just before they leave the room?
How could empty set be unique if it could be vacuously false
Should I include an appendix for inessential, yet related worldbuilding to my story?
Why does independence imply zero correlation?
Did the CIA blow up a Siberian pipeline in 1982?
Why don't countries like Japan just print more money?
Very tricky nonogram - where to go next?
How do I see debug logs for Change Data Capture triggers in Salesforce?
What is the most suitable position for a bishop here?
Too early in the morning to have SODA?
Overloading operator[] and NOT getting "lvalue required as left operand of assignment" error
Is it illegal to withhold someone's passport and green card in California?
A word for delight at someone else's failure?
What happened to Hopper's girlfriend in season one?
Boss wants someone else to lead a project based on the idea I presented to him
Has a life raft ever been successfully deployed on a modern commercial flight?
Counterfeit checks were created for my account. How does this type of fraud work?
Prisoner on alien planet escapes by making up a story about ghost companions and wins the war
Do I have to explain the mechanical superiority of the player-character within the fiction of the game?
Dmesg full of I/O errors, smart ok, four disks affected
Overloading operator[] and NOT getting “lvalue required as left operand of assignment” error
Preventing copy construction and assignment of a return value referenceWhy doesn't Java offer operator overloading?What are the basic rules and idioms for operator overloading?Why is it required to return a reference from overloading bracket operators (OR: why is an lvalue not returned otherwise)?Should post increment operator overloading in c++ return const data typeerror C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)Unable to understand overloading of assignment operatorerror: lvalue required as unary ‘&’ operand when passing std::stringPriority Queue overloading error: “binary '<': no operator found which takes a left-hand operand of type.”Why is const required for 'operator>' but not for 'operator<'?separate handlers lvalue and rvalue [] operators
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This is a bit of an inverse of all the "lvalue required as left operand of assignment" error questions.
I have a class that overloads operator[], but only the version that returns a temporary. If it were to return an int:
struct Foo
int operator[]( int idx ) const return int( 0 );
;
Foo f;
f[1] = 5;
I would rightfully get the lvalue compiler error. If it returns a struct type however, the compiler ( GCC 7.2 in this case ) doesn't complain at all:
struct Bar ;
struct Foo
Bar operator[]( int idx ) const return Bar();
;
Foo f;
f[1] = Bar();
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Alternate question, is there some way to make this complain? Clearly this is a coding error if it were to be used this way
c++ operator-overloading lvalue
add a comment |
This is a bit of an inverse of all the "lvalue required as left operand of assignment" error questions.
I have a class that overloads operator[], but only the version that returns a temporary. If it were to return an int:
struct Foo
int operator[]( int idx ) const return int( 0 );
;
Foo f;
f[1] = 5;
I would rightfully get the lvalue compiler error. If it returns a struct type however, the compiler ( GCC 7.2 in this case ) doesn't complain at all:
struct Bar ;
struct Foo
Bar operator[]( int idx ) const return Bar();
;
Foo f;
f[1] = Bar();
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Alternate question, is there some way to make this complain? Clearly this is a coding error if it were to be used this way
c++ operator-overloading lvalue
2
The test case collapses to:Bar() = Bar();
which compiles for me! I would hope for at-least one warning live: godbolt.org/z/_IPcrd
– Richard Critten
8 hours ago
Not sure if this is an option, but if you make the assignment operator ofBar
private it will not compile. See also Preventing copy construction and assignment of a return value reference
– Håkon Hægland
8 hours ago
Perhaps the struct has a default assignment operator that allows you to assign when doingobject[index] = Object()
? This seems fairly obvious to me.. imgur.com/cGk4RbU So I tested it.. turns out if you delete the assignment operator, it complains so.. seems confirmed to me..
– Brandon
8 hours ago
add a comment |
This is a bit of an inverse of all the "lvalue required as left operand of assignment" error questions.
I have a class that overloads operator[], but only the version that returns a temporary. If it were to return an int:
struct Foo
int operator[]( int idx ) const return int( 0 );
;
Foo f;
f[1] = 5;
I would rightfully get the lvalue compiler error. If it returns a struct type however, the compiler ( GCC 7.2 in this case ) doesn't complain at all:
struct Bar ;
struct Foo
Bar operator[]( int idx ) const return Bar();
;
Foo f;
f[1] = Bar();
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Alternate question, is there some way to make this complain? Clearly this is a coding error if it were to be used this way
c++ operator-overloading lvalue
This is a bit of an inverse of all the "lvalue required as left operand of assignment" error questions.
I have a class that overloads operator[], but only the version that returns a temporary. If it were to return an int:
struct Foo
int operator[]( int idx ) const return int( 0 );
;
Foo f;
f[1] = 5;
I would rightfully get the lvalue compiler error. If it returns a struct type however, the compiler ( GCC 7.2 in this case ) doesn't complain at all:
struct Bar ;
struct Foo
Bar operator[]( int idx ) const return Bar();
;
Foo f;
f[1] = Bar();
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Alternate question, is there some way to make this complain? Clearly this is a coding error if it were to be used this way
c++ operator-overloading lvalue
c++ operator-overloading lvalue
asked 8 hours ago
ByteMe95ByteMe95
34828
34828
2
The test case collapses to:Bar() = Bar();
which compiles for me! I would hope for at-least one warning live: godbolt.org/z/_IPcrd
– Richard Critten
8 hours ago
Not sure if this is an option, but if you make the assignment operator ofBar
private it will not compile. See also Preventing copy construction and assignment of a return value reference
– Håkon Hægland
8 hours ago
Perhaps the struct has a default assignment operator that allows you to assign when doingobject[index] = Object()
? This seems fairly obvious to me.. imgur.com/cGk4RbU So I tested it.. turns out if you delete the assignment operator, it complains so.. seems confirmed to me..
– Brandon
8 hours ago
add a comment |
2
The test case collapses to:Bar() = Bar();
which compiles for me! I would hope for at-least one warning live: godbolt.org/z/_IPcrd
– Richard Critten
8 hours ago
Not sure if this is an option, but if you make the assignment operator ofBar
private it will not compile. See also Preventing copy construction and assignment of a return value reference
– Håkon Hægland
8 hours ago
Perhaps the struct has a default assignment operator that allows you to assign when doingobject[index] = Object()
? This seems fairly obvious to me.. imgur.com/cGk4RbU So I tested it.. turns out if you delete the assignment operator, it complains so.. seems confirmed to me..
– Brandon
8 hours ago
2
2
The test case collapses to:
Bar() = Bar();
which compiles for me! I would hope for at-least one warning live: godbolt.org/z/_IPcrd– Richard Critten
8 hours ago
The test case collapses to:
Bar() = Bar();
which compiles for me! I would hope for at-least one warning live: godbolt.org/z/_IPcrd– Richard Critten
8 hours ago
Not sure if this is an option, but if you make the assignment operator of
Bar
private it will not compile. See also Preventing copy construction and assignment of a return value reference– Håkon Hægland
8 hours ago
Not sure if this is an option, but if you make the assignment operator of
Bar
private it will not compile. See also Preventing copy construction and assignment of a return value reference– Håkon Hægland
8 hours ago
Perhaps the struct has a default assignment operator that allows you to assign when doing
object[index] = Object()
? This seems fairly obvious to me.. imgur.com/cGk4RbU So I tested it.. turns out if you delete the assignment operator, it complains so.. seems confirmed to me..– Brandon
8 hours ago
Perhaps the struct has a default assignment operator that allows you to assign when doing
object[index] = Object()
? This seems fairly obvious to me.. imgur.com/cGk4RbU So I tested it.. turns out if you delete the assignment operator, it complains so.. seems confirmed to me..– Brandon
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
is there some way to make this complain?
You can use an explicitly defaulted assignment operator with a ref-qualifier:
struct Bar {
Bar& operator=(const Bar&) & = default;
// ^
This makes assignment of an rvalue ill-formed, while assignment of an lvalue remains well-formed.
Note that declaring the assignment operator disables implicit move assignment so you may need to define that as well, if needed (also as defaulted, and possibly with an rvalue ref qualifier, if appropriate).
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Because the implicitly generated assignment operators are not ref-qualified.
Clearly this is a coding error if it were to be used this way
Assignment of an rvalue is not universally an error. For some types that are supposed to behave like references, assignment of an rvalue is natural. That is so because the assignment modifies the referred object, and not the temporary object itself.
A typical use case is assigning to an rvalue std::tie
(example from cppreference):
std::set<S> set_of_s; // S is LessThanComparable
S value42, "Test", 3.14;
std::set<S>::iterator iter;
bool inserted;
// unpacks the return value of insert into iter and inserted
std::tie(iter, inserted) = set_of_s.insert(value);
Yes, it might be better if the implicit operators were qualified, and explicit declaration was required for non-qualified, considering referential types are exceptional rather than the norm. But that's not how the language is and changing it is a backwards incompatible change.
1
I'd add that ifBar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.
– aschepler
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
add a comment |
Yes, there's a way to make this into a compile error by deleting these methods:
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Just note that this will disable auto-generation of the other operators and constructors so you have to define them all:
struct Bar
Bar()=default;
Bar(const Bar&) = default;
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Bar& operator=(const Bar&)& =default;
Bar& operator=(Bar&&)& =default;
;
If you havedefault
ones, aren't the others implicitly deleted, and therefore you don't need thedelete
lines?
– Mooing Duck
7 hours ago
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%2f56639421%2foverloading-operator-and-not-getting-lvalue-required-as-left-operand-of-assig%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
is there some way to make this complain?
You can use an explicitly defaulted assignment operator with a ref-qualifier:
struct Bar {
Bar& operator=(const Bar&) & = default;
// ^
This makes assignment of an rvalue ill-formed, while assignment of an lvalue remains well-formed.
Note that declaring the assignment operator disables implicit move assignment so you may need to define that as well, if needed (also as defaulted, and possibly with an rvalue ref qualifier, if appropriate).
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Because the implicitly generated assignment operators are not ref-qualified.
Clearly this is a coding error if it were to be used this way
Assignment of an rvalue is not universally an error. For some types that are supposed to behave like references, assignment of an rvalue is natural. That is so because the assignment modifies the referred object, and not the temporary object itself.
A typical use case is assigning to an rvalue std::tie
(example from cppreference):
std::set<S> set_of_s; // S is LessThanComparable
S value42, "Test", 3.14;
std::set<S>::iterator iter;
bool inserted;
// unpacks the return value of insert into iter and inserted
std::tie(iter, inserted) = set_of_s.insert(value);
Yes, it might be better if the implicit operators were qualified, and explicit declaration was required for non-qualified, considering referential types are exceptional rather than the norm. But that's not how the language is and changing it is a backwards incompatible change.
1
I'd add that ifBar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.
– aschepler
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
add a comment |
is there some way to make this complain?
You can use an explicitly defaulted assignment operator with a ref-qualifier:
struct Bar {
Bar& operator=(const Bar&) & = default;
// ^
This makes assignment of an rvalue ill-formed, while assignment of an lvalue remains well-formed.
Note that declaring the assignment operator disables implicit move assignment so you may need to define that as well, if needed (also as defaulted, and possibly with an rvalue ref qualifier, if appropriate).
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Because the implicitly generated assignment operators are not ref-qualified.
Clearly this is a coding error if it were to be used this way
Assignment of an rvalue is not universally an error. For some types that are supposed to behave like references, assignment of an rvalue is natural. That is so because the assignment modifies the referred object, and not the temporary object itself.
A typical use case is assigning to an rvalue std::tie
(example from cppreference):
std::set<S> set_of_s; // S is LessThanComparable
S value42, "Test", 3.14;
std::set<S>::iterator iter;
bool inserted;
// unpacks the return value of insert into iter and inserted
std::tie(iter, inserted) = set_of_s.insert(value);
Yes, it might be better if the implicit operators were qualified, and explicit declaration was required for non-qualified, considering referential types are exceptional rather than the norm. But that's not how the language is and changing it is a backwards incompatible change.
1
I'd add that ifBar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.
– aschepler
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
add a comment |
is there some way to make this complain?
You can use an explicitly defaulted assignment operator with a ref-qualifier:
struct Bar {
Bar& operator=(const Bar&) & = default;
// ^
This makes assignment of an rvalue ill-formed, while assignment of an lvalue remains well-formed.
Note that declaring the assignment operator disables implicit move assignment so you may need to define that as well, if needed (also as defaulted, and possibly with an rvalue ref qualifier, if appropriate).
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Because the implicitly generated assignment operators are not ref-qualified.
Clearly this is a coding error if it were to be used this way
Assignment of an rvalue is not universally an error. For some types that are supposed to behave like references, assignment of an rvalue is natural. That is so because the assignment modifies the referred object, and not the temporary object itself.
A typical use case is assigning to an rvalue std::tie
(example from cppreference):
std::set<S> set_of_s; // S is LessThanComparable
S value42, "Test", 3.14;
std::set<S>::iterator iter;
bool inserted;
// unpacks the return value of insert into iter and inserted
std::tie(iter, inserted) = set_of_s.insert(value);
Yes, it might be better if the implicit operators were qualified, and explicit declaration was required for non-qualified, considering referential types are exceptional rather than the norm. But that's not how the language is and changing it is a backwards incompatible change.
is there some way to make this complain?
You can use an explicitly defaulted assignment operator with a ref-qualifier:
struct Bar {
Bar& operator=(const Bar&) & = default;
// ^
This makes assignment of an rvalue ill-formed, while assignment of an lvalue remains well-formed.
Note that declaring the assignment operator disables implicit move assignment so you may need to define that as well, if needed (also as defaulted, and possibly with an rvalue ref qualifier, if appropriate).
Why wouldnt this complain in the same way if Bar is a temporary and it doesnt have an specialized operator =?
Because the implicitly generated assignment operators are not ref-qualified.
Clearly this is a coding error if it were to be used this way
Assignment of an rvalue is not universally an error. For some types that are supposed to behave like references, assignment of an rvalue is natural. That is so because the assignment modifies the referred object, and not the temporary object itself.
A typical use case is assigning to an rvalue std::tie
(example from cppreference):
std::set<S> set_of_s; // S is LessThanComparable
S value42, "Test", 3.14;
std::set<S>::iterator iter;
bool inserted;
// unpacks the return value of insert into iter and inserted
std::tie(iter, inserted) = set_of_s.insert(value);
Yes, it might be better if the implicit operators were qualified, and explicit declaration was required for non-qualified, considering referential types are exceptional rather than the norm. But that's not how the language is and changing it is a backwards incompatible change.
edited 7 hours ago
answered 8 hours ago
eerorikaeerorika
95.6k672141
95.6k672141
1
I'd add that ifBar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.
– aschepler
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
add a comment |
1
I'd add that ifBar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.
– aschepler
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
1
1
I'd add that if
Bar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.– aschepler
7 hours ago
I'd add that if
Bar
has members where move operations aren't the same as copy operations, you'll want to obey the Rule of Five since this declaration necessarily breaks the Rule of Zero.– aschepler
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
@aschepler added mention. Interestingly, this is a clear exception to rule of five, since you don't need to touch the destructor at all.
– eerorika
7 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
The copy assignment operator also means there is no implicitly declared move constructor (and deprecates use of the implicitly declared copy constructor).
– aschepler
3 hours ago
add a comment |
Yes, there's a way to make this into a compile error by deleting these methods:
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Just note that this will disable auto-generation of the other operators and constructors so you have to define them all:
struct Bar
Bar()=default;
Bar(const Bar&) = default;
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Bar& operator=(const Bar&)& =default;
Bar& operator=(Bar&&)& =default;
;
If you havedefault
ones, aren't the others implicitly deleted, and therefore you don't need thedelete
lines?
– Mooing Duck
7 hours ago
add a comment |
Yes, there's a way to make this into a compile error by deleting these methods:
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Just note that this will disable auto-generation of the other operators and constructors so you have to define them all:
struct Bar
Bar()=default;
Bar(const Bar&) = default;
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Bar& operator=(const Bar&)& =default;
Bar& operator=(Bar&&)& =default;
;
If you havedefault
ones, aren't the others implicitly deleted, and therefore you don't need thedelete
lines?
– Mooing Duck
7 hours ago
add a comment |
Yes, there's a way to make this into a compile error by deleting these methods:
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Just note that this will disable auto-generation of the other operators and constructors so you have to define them all:
struct Bar
Bar()=default;
Bar(const Bar&) = default;
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Bar& operator=(const Bar&)& =default;
Bar& operator=(Bar&&)& =default;
;
Yes, there's a way to make this into a compile error by deleting these methods:
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Just note that this will disable auto-generation of the other operators and constructors so you have to define them all:
struct Bar
Bar()=default;
Bar(const Bar&) = default;
Bar& operator=(const Bar&)&& =delete;
Bar& operator=(Bar&&)&& =delete;
Bar& operator=(const Bar&)& =default;
Bar& operator=(Bar&&)& =default;
;
answered 8 hours ago
QuimbyQuimby
2,5461820
2,5461820
If you havedefault
ones, aren't the others implicitly deleted, and therefore you don't need thedelete
lines?
– Mooing Duck
7 hours ago
add a comment |
If you havedefault
ones, aren't the others implicitly deleted, and therefore you don't need thedelete
lines?
– Mooing Duck
7 hours ago
If you have
default
ones, aren't the others implicitly deleted, and therefore you don't need the delete
lines?– Mooing Duck
7 hours ago
If you have
default
ones, aren't the others implicitly deleted, and therefore you don't need the delete
lines?– Mooing Duck
7 hours ago
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%2f56639421%2foverloading-operator-and-not-getting-lvalue-required-as-left-operand-of-assig%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
2
The test case collapses to:
Bar() = Bar();
which compiles for me! I would hope for at-least one warning live: godbolt.org/z/_IPcrd– Richard Critten
8 hours ago
Not sure if this is an option, but if you make the assignment operator of
Bar
private it will not compile. See also Preventing copy construction and assignment of a return value reference– Håkon Hægland
8 hours ago
Perhaps the struct has a default assignment operator that allows you to assign when doing
object[index] = Object()
? This seems fairly obvious to me.. imgur.com/cGk4RbU So I tested it.. turns out if you delete the assignment operator, it complains so.. seems confirmed to me..– Brandon
8 hours ago