Constant reference data member












-2















Let's suppose we have a structure with a constant reference data member.



struct A {
A() : i{5} {}
const int& foo() const { return i; }
const int& i;
};


Do you have any idea why the output for an integer literal 5 is different?



A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

5
-858993460









share|improve this question


















  • 5





    I get a compiler warning for this. Clang actually gives an error. Are you asking why this isn't correct, or specifically why the two outputs are different knowing the program is incorrect?

    – chris
    Nov 24 '18 at 8:25








  • 1





    See here also. Looks like you have a case of undefined behavior.

    – πάντα ῥεῖ
    Nov 24 '18 at 8:29











  • BTW, VS 2017 doesn't output any warnings. I don't understand why an integer literal goes out of the scope only when accessing the referenced value in function foo.

    – dubrava
    Nov 24 '18 at 8:41






  • 1





    @dubrava UB means anything is possible; so you might get the result of 5, -858993460, or 42, or something else like segfault.

    – songyuanyao
    Nov 24 '18 at 8:47


















-2















Let's suppose we have a structure with a constant reference data member.



struct A {
A() : i{5} {}
const int& foo() const { return i; }
const int& i;
};


Do you have any idea why the output for an integer literal 5 is different?



A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

5
-858993460









share|improve this question


















  • 5





    I get a compiler warning for this. Clang actually gives an error. Are you asking why this isn't correct, or specifically why the two outputs are different knowing the program is incorrect?

    – chris
    Nov 24 '18 at 8:25








  • 1





    See here also. Looks like you have a case of undefined behavior.

    – πάντα ῥεῖ
    Nov 24 '18 at 8:29











  • BTW, VS 2017 doesn't output any warnings. I don't understand why an integer literal goes out of the scope only when accessing the referenced value in function foo.

    – dubrava
    Nov 24 '18 at 8:41






  • 1





    @dubrava UB means anything is possible; so you might get the result of 5, -858993460, or 42, or something else like segfault.

    – songyuanyao
    Nov 24 '18 at 8:47
















-2












-2








-2


0






Let's suppose we have a structure with a constant reference data member.



struct A {
A() : i{5} {}
const int& foo() const { return i; }
const int& i;
};


Do you have any idea why the output for an integer literal 5 is different?



A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

5
-858993460









share|improve this question














Let's suppose we have a structure with a constant reference data member.



struct A {
A() : i{5} {}
const int& foo() const { return i; }
const int& i;
};


Do you have any idea why the output for an integer literal 5 is different?



A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

5
-858993460






c++ c++11






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 8:22









dubravadubrava

92




92








  • 5





    I get a compiler warning for this. Clang actually gives an error. Are you asking why this isn't correct, or specifically why the two outputs are different knowing the program is incorrect?

    – chris
    Nov 24 '18 at 8:25








  • 1





    See here also. Looks like you have a case of undefined behavior.

    – πάντα ῥεῖ
    Nov 24 '18 at 8:29











  • BTW, VS 2017 doesn't output any warnings. I don't understand why an integer literal goes out of the scope only when accessing the referenced value in function foo.

    – dubrava
    Nov 24 '18 at 8:41






  • 1





    @dubrava UB means anything is possible; so you might get the result of 5, -858993460, or 42, or something else like segfault.

    – songyuanyao
    Nov 24 '18 at 8:47
















  • 5





    I get a compiler warning for this. Clang actually gives an error. Are you asking why this isn't correct, or specifically why the two outputs are different knowing the program is incorrect?

    – chris
    Nov 24 '18 at 8:25








  • 1





    See here also. Looks like you have a case of undefined behavior.

    – πάντα ῥεῖ
    Nov 24 '18 at 8:29











  • BTW, VS 2017 doesn't output any warnings. I don't understand why an integer literal goes out of the scope only when accessing the referenced value in function foo.

    – dubrava
    Nov 24 '18 at 8:41






  • 1





    @dubrava UB means anything is possible; so you might get the result of 5, -858993460, or 42, or something else like segfault.

    – songyuanyao
    Nov 24 '18 at 8:47










5




5





I get a compiler warning for this. Clang actually gives an error. Are you asking why this isn't correct, or specifically why the two outputs are different knowing the program is incorrect?

– chris
Nov 24 '18 at 8:25







I get a compiler warning for this. Clang actually gives an error. Are you asking why this isn't correct, or specifically why the two outputs are different knowing the program is incorrect?

– chris
Nov 24 '18 at 8:25






1




1





See here also. Looks like you have a case of undefined behavior.

– πάντα ῥεῖ
Nov 24 '18 at 8:29





See here also. Looks like you have a case of undefined behavior.

– πάντα ῥεῖ
Nov 24 '18 at 8:29













BTW, VS 2017 doesn't output any warnings. I don't understand why an integer literal goes out of the scope only when accessing the referenced value in function foo.

– dubrava
Nov 24 '18 at 8:41





BTW, VS 2017 doesn't output any warnings. I don't understand why an integer literal goes out of the scope only when accessing the referenced value in function foo.

– dubrava
Nov 24 '18 at 8:41




1




1





@dubrava UB means anything is possible; so you might get the result of 5, -858993460, or 42, or something else like segfault.

– songyuanyao
Nov 24 '18 at 8:47







@dubrava UB means anything is possible; so you might get the result of 5, -858993460, or 42, or something else like segfault.

– songyuanyao
Nov 24 '18 at 8:47














2 Answers
2






active

oldest

votes


















3














The code is ill-formed. You're initializing i from literal 5, which requires a temporary object to be contructed and then bound to i. The temporary will be destroyed when the constructor exits, then i becomes dangled, any dereference on it later leads to UB, means anything is possible.



From the standard, [class.base.init]/8




A temporary expression bound to a reference member in a
mem-initializer is ill-formed. [ Example:



struct A {
A() : v(42) { } // error
const int& v;
};


— end example ]




BTW: Since the standard states it's ill-formed, the compilers are required to issue a diagnostic for it. Both the behavior of gcc (gives a warning) and clang (gives an error) are conforming; if VS2017 doesn't issue any diagnostic then it's non-conforming to the standard.






share|improve this answer


























  • OK, thank you for the reference and detailed explanation!

    – dubrava
    Nov 24 '18 at 9:20



















0














Use of a constant value not a literal.



This is Class::Class() : member{arg1, arg2, ...} {... direct-list-initialization since c++11.



Try this :



 struct A {
const int t = 5;
A() : i{ t } { }
const int& foo() const { return i; }
const int& i;
};

int main()
{
A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

return 0;
}





share|improve this answer


























  • I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

    – dubrava
    Nov 24 '18 at 9:14











  • @dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

    – Mohammadreza Panahi
    Nov 24 '18 at 13:10













  • If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

    – dubrava
    Nov 24 '18 at 16:45













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%2f53456439%2fconstant-reference-data-member%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









3














The code is ill-formed. You're initializing i from literal 5, which requires a temporary object to be contructed and then bound to i. The temporary will be destroyed when the constructor exits, then i becomes dangled, any dereference on it later leads to UB, means anything is possible.



From the standard, [class.base.init]/8




A temporary expression bound to a reference member in a
mem-initializer is ill-formed. [ Example:



struct A {
A() : v(42) { } // error
const int& v;
};


— end example ]




BTW: Since the standard states it's ill-formed, the compilers are required to issue a diagnostic for it. Both the behavior of gcc (gives a warning) and clang (gives an error) are conforming; if VS2017 doesn't issue any diagnostic then it's non-conforming to the standard.






share|improve this answer


























  • OK, thank you for the reference and detailed explanation!

    – dubrava
    Nov 24 '18 at 9:20
















3














The code is ill-formed. You're initializing i from literal 5, which requires a temporary object to be contructed and then bound to i. The temporary will be destroyed when the constructor exits, then i becomes dangled, any dereference on it later leads to UB, means anything is possible.



From the standard, [class.base.init]/8




A temporary expression bound to a reference member in a
mem-initializer is ill-formed. [ Example:



struct A {
A() : v(42) { } // error
const int& v;
};


— end example ]




BTW: Since the standard states it's ill-formed, the compilers are required to issue a diagnostic for it. Both the behavior of gcc (gives a warning) and clang (gives an error) are conforming; if VS2017 doesn't issue any diagnostic then it's non-conforming to the standard.






share|improve this answer


























  • OK, thank you for the reference and detailed explanation!

    – dubrava
    Nov 24 '18 at 9:20














3












3








3







The code is ill-formed. You're initializing i from literal 5, which requires a temporary object to be contructed and then bound to i. The temporary will be destroyed when the constructor exits, then i becomes dangled, any dereference on it later leads to UB, means anything is possible.



From the standard, [class.base.init]/8




A temporary expression bound to a reference member in a
mem-initializer is ill-formed. [ Example:



struct A {
A() : v(42) { } // error
const int& v;
};


— end example ]




BTW: Since the standard states it's ill-formed, the compilers are required to issue a diagnostic for it. Both the behavior of gcc (gives a warning) and clang (gives an error) are conforming; if VS2017 doesn't issue any diagnostic then it's non-conforming to the standard.






share|improve this answer















The code is ill-formed. You're initializing i from literal 5, which requires a temporary object to be contructed and then bound to i. The temporary will be destroyed when the constructor exits, then i becomes dangled, any dereference on it later leads to UB, means anything is possible.



From the standard, [class.base.init]/8




A temporary expression bound to a reference member in a
mem-initializer is ill-formed. [ Example:



struct A {
A() : v(42) { } // error
const int& v;
};


— end example ]




BTW: Since the standard states it's ill-formed, the compilers are required to issue a diagnostic for it. Both the behavior of gcc (gives a warning) and clang (gives an error) are conforming; if VS2017 doesn't issue any diagnostic then it's non-conforming to the standard.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 8:55

























answered Nov 24 '18 at 8:36









songyuanyaosongyuanyao

92.1k11175240




92.1k11175240













  • OK, thank you for the reference and detailed explanation!

    – dubrava
    Nov 24 '18 at 9:20



















  • OK, thank you for the reference and detailed explanation!

    – dubrava
    Nov 24 '18 at 9:20

















OK, thank you for the reference and detailed explanation!

– dubrava
Nov 24 '18 at 9:20





OK, thank you for the reference and detailed explanation!

– dubrava
Nov 24 '18 at 9:20













0














Use of a constant value not a literal.



This is Class::Class() : member{arg1, arg2, ...} {... direct-list-initialization since c++11.



Try this :



 struct A {
const int t = 5;
A() : i{ t } { }
const int& foo() const { return i; }
const int& i;
};

int main()
{
A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

return 0;
}





share|improve this answer


























  • I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

    – dubrava
    Nov 24 '18 at 9:14











  • @dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

    – Mohammadreza Panahi
    Nov 24 '18 at 13:10













  • If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

    – dubrava
    Nov 24 '18 at 16:45


















0














Use of a constant value not a literal.



This is Class::Class() : member{arg1, arg2, ...} {... direct-list-initialization since c++11.



Try this :



 struct A {
const int t = 5;
A() : i{ t } { }
const int& foo() const { return i; }
const int& i;
};

int main()
{
A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

return 0;
}





share|improve this answer


























  • I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

    – dubrava
    Nov 24 '18 at 9:14











  • @dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

    – Mohammadreza Panahi
    Nov 24 '18 at 13:10













  • If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

    – dubrava
    Nov 24 '18 at 16:45
















0












0








0







Use of a constant value not a literal.



This is Class::Class() : member{arg1, arg2, ...} {... direct-list-initialization since c++11.



Try this :



 struct A {
const int t = 5;
A() : i{ t } { }
const int& foo() const { return i; }
const int& i;
};

int main()
{
A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

return 0;
}





share|improve this answer















Use of a constant value not a literal.



This is Class::Class() : member{arg1, arg2, ...} {... direct-list-initialization since c++11.



Try this :



 struct A {
const int t = 5;
A() : i{ t } { }
const int& foo() const { return i; }
const int& i;
};

int main()
{
A a{};
std::cout << a.i << std::endl;
std::cout << a.foo() << std::endl;

return 0;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 13:04

























answered Nov 24 '18 at 8:55









Mohammadreza PanahiMohammadreza Panahi

2,73021433




2,73021433













  • I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

    – dubrava
    Nov 24 '18 at 9:14











  • @dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

    – Mohammadreza Panahi
    Nov 24 '18 at 13:10













  • If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

    – dubrava
    Nov 24 '18 at 16:45





















  • I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

    – dubrava
    Nov 24 '18 at 9:14











  • @dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

    – Mohammadreza Panahi
    Nov 24 '18 at 13:10













  • If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

    – dubrava
    Nov 24 '18 at 16:45



















I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

– dubrava
Nov 24 '18 at 9:14





I didn't mean to store a temporary literal, just was wondering if its scope is a class or constructor.

– dubrava
Nov 24 '18 at 9:14













@dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

– Mohammadreza Panahi
Nov 24 '18 at 13:10







@dubrava I improved my answer, please check it. But about your question, honestly i didnt work with c++11 but I offered this answer to solving your problem . by the way this link might help you.

– Mohammadreza Panahi
Nov 24 '18 at 13:10















If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

– dubrava
Nov 24 '18 at 16:45







If I'd like to initialize the const reference to default value, I'd use in-class initializer const int& i {5};

– dubrava
Nov 24 '18 at 16:45




















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%2f53456439%2fconstant-reference-data-member%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

Costa Masnaga

Fotorealismo

Sidney Franklin