Different results when using auto as member function argument












2















I am getting different results when running these two



I am on GNU/Linux 4.14.67



These both are ran using g++ -std=c++14 with/without -O0 and on c++17 as well.



Why am I? Why are the outputs different?



First version is:



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const foo& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The first output is:



foo operator=
888




Second version (only changing const foo& to const auto& ):



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const auto& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The second output is:



0









share|improve this question




















  • 1





    Maybe your forgot "return *this" at the end of op=(...) ?

    – Picaud Vincent
    Nov 16 '18 at 4:46











  • I thought so too, but it was a copy and paste error. Good catch

    – Seoul
    Nov 16 '18 at 4:47






  • 1





    Not only "*this" but "return *this;"

    – Picaud Vincent
    Nov 16 '18 at 4:47






  • 7





    "Why am I?" Hard to tell, very philosophical question. Some say "I am thinking, thus I be (am)"

    – πάντα ῥεῖ
    Nov 16 '18 at 4:48






  • 1





    @Swordfish Watchworthy: youtube.com/watch?v=29pPZQ77cmI

    – πάντα ῥεῖ
    Nov 16 '18 at 4:53
















2















I am getting different results when running these two



I am on GNU/Linux 4.14.67



These both are ran using g++ -std=c++14 with/without -O0 and on c++17 as well.



Why am I? Why are the outputs different?



First version is:



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const foo& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The first output is:



foo operator=
888




Second version (only changing const foo& to const auto& ):



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const auto& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The second output is:



0









share|improve this question




















  • 1





    Maybe your forgot "return *this" at the end of op=(...) ?

    – Picaud Vincent
    Nov 16 '18 at 4:46











  • I thought so too, but it was a copy and paste error. Good catch

    – Seoul
    Nov 16 '18 at 4:47






  • 1





    Not only "*this" but "return *this;"

    – Picaud Vincent
    Nov 16 '18 at 4:47






  • 7





    "Why am I?" Hard to tell, very philosophical question. Some say "I am thinking, thus I be (am)"

    – πάντα ῥεῖ
    Nov 16 '18 at 4:48






  • 1





    @Swordfish Watchworthy: youtube.com/watch?v=29pPZQ77cmI

    – πάντα ῥεῖ
    Nov 16 '18 at 4:53














2












2








2








I am getting different results when running these two



I am on GNU/Linux 4.14.67



These both are ran using g++ -std=c++14 with/without -O0 and on c++17 as well.



Why am I? Why are the outputs different?



First version is:



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const foo& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The first output is:



foo operator=
888




Second version (only changing const foo& to const auto& ):



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const auto& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The second output is:



0









share|improve this question
















I am getting different results when running these two



I am on GNU/Linux 4.14.67



These both are ran using g++ -std=c++14 with/without -O0 and on c++17 as well.



Why am I? Why are the outputs different?



First version is:



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const foo& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The first output is:



foo operator=
888




Second version (only changing const foo& to const auto& ):



#include <iostream>
#include <algorithm>
using namespace std;

class foo {
public:
foo() { }
foo(const foo& f) { }
foo& operator=(const auto& f) {
cout << "foo operator=n";
val = 888;
// Do something important
return *this;
}
int val;
};

int main() {
foo f1;
foo f2;
f1 = f2;

cout << f1.val << endl;
}


The second output is:



0






c++ c++14 c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:48







Seoul

















asked Nov 16 '18 at 4:44









SeoulSeoul

216111




216111








  • 1





    Maybe your forgot "return *this" at the end of op=(...) ?

    – Picaud Vincent
    Nov 16 '18 at 4:46











  • I thought so too, but it was a copy and paste error. Good catch

    – Seoul
    Nov 16 '18 at 4:47






  • 1





    Not only "*this" but "return *this;"

    – Picaud Vincent
    Nov 16 '18 at 4:47






  • 7





    "Why am I?" Hard to tell, very philosophical question. Some say "I am thinking, thus I be (am)"

    – πάντα ῥεῖ
    Nov 16 '18 at 4:48






  • 1





    @Swordfish Watchworthy: youtube.com/watch?v=29pPZQ77cmI

    – πάντα ῥεῖ
    Nov 16 '18 at 4:53














  • 1





    Maybe your forgot "return *this" at the end of op=(...) ?

    – Picaud Vincent
    Nov 16 '18 at 4:46











  • I thought so too, but it was a copy and paste error. Good catch

    – Seoul
    Nov 16 '18 at 4:47






  • 1





    Not only "*this" but "return *this;"

    – Picaud Vincent
    Nov 16 '18 at 4:47






  • 7





    "Why am I?" Hard to tell, very philosophical question. Some say "I am thinking, thus I be (am)"

    – πάντα ῥεῖ
    Nov 16 '18 at 4:48






  • 1





    @Swordfish Watchworthy: youtube.com/watch?v=29pPZQ77cmI

    – πάντα ῥεῖ
    Nov 16 '18 at 4:53








1




1





Maybe your forgot "return *this" at the end of op=(...) ?

– Picaud Vincent
Nov 16 '18 at 4:46





Maybe your forgot "return *this" at the end of op=(...) ?

– Picaud Vincent
Nov 16 '18 at 4:46













I thought so too, but it was a copy and paste error. Good catch

– Seoul
Nov 16 '18 at 4:47





I thought so too, but it was a copy and paste error. Good catch

– Seoul
Nov 16 '18 at 4:47




1




1





Not only "*this" but "return *this;"

– Picaud Vincent
Nov 16 '18 at 4:47





Not only "*this" but "return *this;"

– Picaud Vincent
Nov 16 '18 at 4:47




7




7





"Why am I?" Hard to tell, very philosophical question. Some say "I am thinking, thus I be (am)"

– πάντα ῥεῖ
Nov 16 '18 at 4:48





"Why am I?" Hard to tell, very philosophical question. Some say "I am thinking, thus I be (am)"

– πάντα ῥεῖ
Nov 16 '18 at 4:48




1




1





@Swordfish Watchworthy: youtube.com/watch?v=29pPZQ77cmI

– πάντα ῥεῖ
Nov 16 '18 at 4:53





@Swordfish Watchworthy: youtube.com/watch?v=29pPZQ77cmI

– πάντα ῥεῖ
Nov 16 '18 at 4:53












1 Answer
1






active

oldest

votes


















5














This:



foo& operator=(const auto& f);


will not be standard C++ code until C++20. But gcc has allowed it for quite some time, and what it means is:



template <typename _T>
foo& operator=(const _T& f);


In other words, this is an assignment operator template. It is not a copy assignment operator. That must be a non-template. Since you did not provide a copy assignment operator, the compiler happily generates one for you. In your first code example, you provided your own copy assignment operator.



When you write:



f1 = f2;


In your first example, that expression has one candidate: the copy assignment operator that you wrote. In the second example, there are two candidates: your assignment operator template and the copy assignment operator synthesized by the compiler. The compiler's is a better match (non-template beats template), so it gets called - not yours.






share|improve this answer
























  • You might want to mention P1141 @chris digged out.

    – Swordfish
    Nov 16 '18 at 5:27











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331577%2fdifferent-results-when-using-auto-as-member-function-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














This:



foo& operator=(const auto& f);


will not be standard C++ code until C++20. But gcc has allowed it for quite some time, and what it means is:



template <typename _T>
foo& operator=(const _T& f);


In other words, this is an assignment operator template. It is not a copy assignment operator. That must be a non-template. Since you did not provide a copy assignment operator, the compiler happily generates one for you. In your first code example, you provided your own copy assignment operator.



When you write:



f1 = f2;


In your first example, that expression has one candidate: the copy assignment operator that you wrote. In the second example, there are two candidates: your assignment operator template and the copy assignment operator synthesized by the compiler. The compiler's is a better match (non-template beats template), so it gets called - not yours.






share|improve this answer
























  • You might want to mention P1141 @chris digged out.

    – Swordfish
    Nov 16 '18 at 5:27
















5














This:



foo& operator=(const auto& f);


will not be standard C++ code until C++20. But gcc has allowed it for quite some time, and what it means is:



template <typename _T>
foo& operator=(const _T& f);


In other words, this is an assignment operator template. It is not a copy assignment operator. That must be a non-template. Since you did not provide a copy assignment operator, the compiler happily generates one for you. In your first code example, you provided your own copy assignment operator.



When you write:



f1 = f2;


In your first example, that expression has one candidate: the copy assignment operator that you wrote. In the second example, there are two candidates: your assignment operator template and the copy assignment operator synthesized by the compiler. The compiler's is a better match (non-template beats template), so it gets called - not yours.






share|improve this answer
























  • You might want to mention P1141 @chris digged out.

    – Swordfish
    Nov 16 '18 at 5:27














5












5








5







This:



foo& operator=(const auto& f);


will not be standard C++ code until C++20. But gcc has allowed it for quite some time, and what it means is:



template <typename _T>
foo& operator=(const _T& f);


In other words, this is an assignment operator template. It is not a copy assignment operator. That must be a non-template. Since you did not provide a copy assignment operator, the compiler happily generates one for you. In your first code example, you provided your own copy assignment operator.



When you write:



f1 = f2;


In your first example, that expression has one candidate: the copy assignment operator that you wrote. In the second example, there are two candidates: your assignment operator template and the copy assignment operator synthesized by the compiler. The compiler's is a better match (non-template beats template), so it gets called - not yours.






share|improve this answer













This:



foo& operator=(const auto& f);


will not be standard C++ code until C++20. But gcc has allowed it for quite some time, and what it means is:



template <typename _T>
foo& operator=(const _T& f);


In other words, this is an assignment operator template. It is not a copy assignment operator. That must be a non-template. Since you did not provide a copy assignment operator, the compiler happily generates one for you. In your first code example, you provided your own copy assignment operator.



When you write:



f1 = f2;


In your first example, that expression has one candidate: the copy assignment operator that you wrote. In the second example, there are two candidates: your assignment operator template and the copy assignment operator synthesized by the compiler. The compiler's is a better match (non-template beats template), so it gets called - not yours.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 5:22









BarryBarry

185k21325600




185k21325600













  • You might want to mention P1141 @chris digged out.

    – Swordfish
    Nov 16 '18 at 5:27



















  • You might want to mention P1141 @chris digged out.

    – Swordfish
    Nov 16 '18 at 5:27

















You might want to mention P1141 @chris digged out.

– Swordfish
Nov 16 '18 at 5:27





You might want to mention P1141 @chris digged out.

– Swordfish
Nov 16 '18 at 5:27




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331577%2fdifferent-results-when-using-auto-as-member-function-argument%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly