Conditional ? : operator with class constructor











up vote
11
down vote

favorite
1












could someone explain me why c and c1 are constructed different way.
I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance



#include <vector>

class foo {
public:
foo(const std::vector<int>& var) :var{ var } {};
const std::vector<int> & var;
};

std::vector<int> f(){
std::vector<int> x{ 1,2,3,4,5 };
return x;
};

int main(){
std::vector<int> x1{ 1,2,3,4,5 ,7 };
std::vector<int> x2{ 1,2,3,4,5 ,6 };
foo c{ true ? x2 : x1 }; //c.var has expected values
foo c1{ true ? x2 : f() }; //c.var empty
foo c2{ false ? x2 : f() }; //c.var empty
foo c3{ x2 }; //c.var has expected values
}









share|improve this question




























    up vote
    11
    down vote

    favorite
    1












    could someone explain me why c and c1 are constructed different way.
    I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
    I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance



    #include <vector>

    class foo {
    public:
    foo(const std::vector<int>& var) :var{ var } {};
    const std::vector<int> & var;
    };

    std::vector<int> f(){
    std::vector<int> x{ 1,2,3,4,5 };
    return x;
    };

    int main(){
    std::vector<int> x1{ 1,2,3,4,5 ,7 };
    std::vector<int> x2{ 1,2,3,4,5 ,6 };
    foo c{ true ? x2 : x1 }; //c.var has expected values
    foo c1{ true ? x2 : f() }; //c.var empty
    foo c2{ false ? x2 : f() }; //c.var empty
    foo c3{ x2 }; //c.var has expected values
    }









    share|improve this question


























      up vote
      11
      down vote

      favorite
      1









      up vote
      11
      down vote

      favorite
      1






      1





      could someone explain me why c and c1 are constructed different way.
      I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
      I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance



      #include <vector>

      class foo {
      public:
      foo(const std::vector<int>& var) :var{ var } {};
      const std::vector<int> & var;
      };

      std::vector<int> f(){
      std::vector<int> x{ 1,2,3,4,5 };
      return x;
      };

      int main(){
      std::vector<int> x1{ 1,2,3,4,5 ,7 };
      std::vector<int> x2{ 1,2,3,4,5 ,6 };
      foo c{ true ? x2 : x1 }; //c.var has expected values
      foo c1{ true ? x2 : f() }; //c.var empty
      foo c2{ false ? x2 : f() }; //c.var empty
      foo c3{ x2 }; //c.var has expected values
      }









      share|improve this question















      could someone explain me why c and c1 are constructed different way.
      I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
      I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance



      #include <vector>

      class foo {
      public:
      foo(const std::vector<int>& var) :var{ var } {};
      const std::vector<int> & var;
      };

      std::vector<int> f(){
      std::vector<int> x{ 1,2,3,4,5 };
      return x;
      };

      int main(){
      std::vector<int> x1{ 1,2,3,4,5 ,7 };
      std::vector<int> x2{ 1,2,3,4,5 ,6 };
      foo c{ true ? x2 : x1 }; //c.var has expected values
      foo c1{ true ? x2 : f() }; //c.var empty
      foo c2{ false ? x2 : f() }; //c.var empty
      foo c3{ x2 }; //c.var has expected values
      }






      c++ c++14 conditional-operator






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 14:55









      Deduplicator

      33.9k64787




      33.9k64787










      asked Nov 23 at 14:30









      geniculata

      1618




      1618
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          19
          down vote













          The type of a conditional expression is the common type of the two branches. For true ? x2 : x1, the common type is std::vector<int>&. For true ? x2 : f(), the common type is std::vector<int>.



          Therefore you are storing a dangling reference in c1. Any access to c1.var is undefined behavior.






          share|improve this answer



















          • 4




            ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
            – Deduplicator
            Nov 23 at 14:57






          • 5




            It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
            – Nicol Bolas
            Nov 23 at 14:59










          • Oh C++. Oh you.
            – Lightness Races in Orbit
            Nov 23 at 15:23











          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',
          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%2f53448536%2fconditional-operator-with-class-constructor%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








          up vote
          19
          down vote













          The type of a conditional expression is the common type of the two branches. For true ? x2 : x1, the common type is std::vector<int>&. For true ? x2 : f(), the common type is std::vector<int>.



          Therefore you are storing a dangling reference in c1. Any access to c1.var is undefined behavior.






          share|improve this answer



















          • 4




            ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
            – Deduplicator
            Nov 23 at 14:57






          • 5




            It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
            – Nicol Bolas
            Nov 23 at 14:59










          • Oh C++. Oh you.
            – Lightness Races in Orbit
            Nov 23 at 15:23















          up vote
          19
          down vote













          The type of a conditional expression is the common type of the two branches. For true ? x2 : x1, the common type is std::vector<int>&. For true ? x2 : f(), the common type is std::vector<int>.



          Therefore you are storing a dangling reference in c1. Any access to c1.var is undefined behavior.






          share|improve this answer



















          • 4




            ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
            – Deduplicator
            Nov 23 at 14:57






          • 5




            It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
            – Nicol Bolas
            Nov 23 at 14:59










          • Oh C++. Oh you.
            – Lightness Races in Orbit
            Nov 23 at 15:23













          up vote
          19
          down vote










          up vote
          19
          down vote









          The type of a conditional expression is the common type of the two branches. For true ? x2 : x1, the common type is std::vector<int>&. For true ? x2 : f(), the common type is std::vector<int>.



          Therefore you are storing a dangling reference in c1. Any access to c1.var is undefined behavior.






          share|improve this answer














          The type of a conditional expression is the common type of the two branches. For true ? x2 : x1, the common type is std::vector<int>&. For true ? x2 : f(), the common type is std::vector<int>.



          Therefore you are storing a dangling reference in c1. Any access to c1.var is undefined behavior.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 at 14:59

























          answered Nov 23 at 14:41









          Vittorio Romeo

          55.6k17146287




          55.6k17146287








          • 4




            ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
            – Deduplicator
            Nov 23 at 14:57






          • 5




            It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
            – Nicol Bolas
            Nov 23 at 14:59










          • Oh C++. Oh you.
            – Lightness Races in Orbit
            Nov 23 at 15:23














          • 4




            ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
            – Deduplicator
            Nov 23 at 14:57






          • 5




            It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
            – Nicol Bolas
            Nov 23 at 14:59










          • Oh C++. Oh you.
            – Lightness Races in Orbit
            Nov 23 at 15:23








          4




          4




          ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
          – Deduplicator
          Nov 23 at 14:57




          ...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
          – Deduplicator
          Nov 23 at 14:57




          5




          5




          It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
          – Nicol Bolas
          Nov 23 at 14:59




          It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
          – Nicol Bolas
          Nov 23 at 14:59












          Oh C++. Oh you.
          – Lightness Races in Orbit
          Nov 23 at 15:23




          Oh C++. Oh you.
          – Lightness Races in Orbit
          Nov 23 at 15:23


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448536%2fconditional-operator-with-class-constructor%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

          Plaza Victoria

          In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

          How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...