Why is there a dummy union member in some implemetations of std::optional?Why is “using namespace std;” considered bad practice?Purpose of Unions in C and C++easy way to access union membersC++ equivalent of Rust's Result<T, E> type?memcpy/memmove to a union member, does this set the 'active' member?Static vector internal data layout - `union` vs `std::aligned_storage_t` - huge performance differenceClang LLVM C++ `std::optional<std::optional<int>>` surprising comparison behaviorC++ Union Member Access And Undefined Behaviour

What are the advantages of this gold finger shape?

Cases with long math equation

What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?

What kind of liquid can be seen 'leaking' from the upper surface of the wing of a Boeing 737-800?

Why won't the Republicans use a superdelegate system like the DNC in their nomination process?

Installing Windows to flash UEFI/ BIOS, then reinstalling Ubuntu

How does the Athlete Feat affect the Ravnica Centaur playable race?

Lípínguapua dopo Pêpê

Graphs for which a calculus student can reasonably compute the arclength

Finding the shaded region

Word for an event that will likely never happen again

Are there really no countries that protect Freedom of Speech as the United States does?

Do you "gain" 1st level?

Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?

Global BGP Routing only by only importing supernet prefixes

Cycle of actions and voice signals on a multipitch climb

How can God warn people of the upcoming rapture without disrupting society?

Did DOS zero out the BSS area when it loaded a program?

Is this n-speak?

Why is the second S silent in "Sens dessus dessous"?

Do I have to cite common CS algorithms?

Good textbook for queueing theory and performance modeling

How can I communicate my issues with a potential date's pushy behavior?

Why is there a dummy union member in some implemetations of std::optional?



Why is there a dummy union member in some implemetations of std::optional?


Why is “using namespace std;” considered bad practice?Purpose of Unions in C and C++easy way to access union membersC++ equivalent of Rust's Result<T, E> type?memcpy/memmove to a union member, does this set the 'active' member?Static vector internal data layout - `union` vs `std::aligned_storage_t` - huge performance differenceClang LLVM C++ `std::optional<std::optional<int>>` surprising comparison behaviorC++ Union Member Access And Undefined Behaviour






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








18















Both libstdc++ (GNU) and libc++ (LLVM) implement std::optional value storage using a union and both of them include a dummy member.



GNU implementation:



using _Stored_type = remove_const_t<_Tp>;
struct _Empty_byte ;
union
_Empty_byte _M_empty;
_Stored_type _M_payload;
;


LLVM implementation:



union

char __null_state_;
value_type __val_;
;


My question is: Why do we need these _M_empty/__null_state_ members? Is there something wrong with a single-member union?










share|improve this question






























    18















    Both libstdc++ (GNU) and libc++ (LLVM) implement std::optional value storage using a union and both of them include a dummy member.



    GNU implementation:



    using _Stored_type = remove_const_t<_Tp>;
    struct _Empty_byte ;
    union
    _Empty_byte _M_empty;
    _Stored_type _M_payload;
    ;


    LLVM implementation:



    union

    char __null_state_;
    value_type __val_;
    ;


    My question is: Why do we need these _M_empty/__null_state_ members? Is there something wrong with a single-member union?










    share|improve this question


























      18












      18








      18


      1






      Both libstdc++ (GNU) and libc++ (LLVM) implement std::optional value storage using a union and both of them include a dummy member.



      GNU implementation:



      using _Stored_type = remove_const_t<_Tp>;
      struct _Empty_byte ;
      union
      _Empty_byte _M_empty;
      _Stored_type _M_payload;
      ;


      LLVM implementation:



      union

      char __null_state_;
      value_type __val_;
      ;


      My question is: Why do we need these _M_empty/__null_state_ members? Is there something wrong with a single-member union?










      share|improve this question














      Both libstdc++ (GNU) and libc++ (LLVM) implement std::optional value storage using a union and both of them include a dummy member.



      GNU implementation:



      using _Stored_type = remove_const_t<_Tp>;
      struct _Empty_byte ;
      union
      _Empty_byte _M_empty;
      _Stored_type _M_payload;
      ;


      LLVM implementation:



      union

      char __null_state_;
      value_type __val_;
      ;


      My question is: Why do we need these _M_empty/__null_state_ members? Is there something wrong with a single-member union?







      c++ c++17 optional unions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 11 hours ago









      r3mus n0xr3mus n0x

      4,2151 gold badge4 silver badges27 bronze badges




      4,2151 gold badge4 silver badges27 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          20














          This is because when using a non-trivial type in a union, suddently the default constructor is deleted.



          This creates all sorts of problem when trying to be constexpr compatible.



          Consider this code:



          struct nontrivial 
          constexpr nontrivial(int o) : uo
          int u;
          ;

          union storage
          nontrivial nt;
          ;

          struct optional
          storage s;
          ;

          constexpr auto run() -> int
          optional o;
          return o.s.nt.u;


          int main()
          constexpr int t = run();



          This is ill formed because optional has a deleted constructor.



          Then a simple fix would be to add a constructor that initialize no union member:



          union storage 
          constexpr storage() // standard says no
          nontrivial nt;
          ;


          But it won't work. Constexpr unions must have at least one active member. It cannot be an empty union. To workaround this limitation, a dummy member is added. This makes std::optional useable in constexpr context.






          share|improve this answer






















          • 6





            For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

            – Barry
            10 hours ago











          • @TedLyngmo, which version? Tried on 7.4.0 - got an error.

            – r3mus n0x
            9 hours ago











          • @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

            – Ted Lyngmo
            9 hours ago












          • @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

            – r3mus n0x
            9 hours ago











          • @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

            – Ted Lyngmo
            9 hours ago











          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%2f57496628%2fwhy-is-there-a-dummy-union-member-in-some-implemetations-of-stdoptional%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









          20














          This is because when using a non-trivial type in a union, suddently the default constructor is deleted.



          This creates all sorts of problem when trying to be constexpr compatible.



          Consider this code:



          struct nontrivial 
          constexpr nontrivial(int o) : uo
          int u;
          ;

          union storage
          nontrivial nt;
          ;

          struct optional
          storage s;
          ;

          constexpr auto run() -> int
          optional o;
          return o.s.nt.u;


          int main()
          constexpr int t = run();



          This is ill formed because optional has a deleted constructor.



          Then a simple fix would be to add a constructor that initialize no union member:



          union storage 
          constexpr storage() // standard says no
          nontrivial nt;
          ;


          But it won't work. Constexpr unions must have at least one active member. It cannot be an empty union. To workaround this limitation, a dummy member is added. This makes std::optional useable in constexpr context.






          share|improve this answer






















          • 6





            For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

            – Barry
            10 hours ago











          • @TedLyngmo, which version? Tried on 7.4.0 - got an error.

            – r3mus n0x
            9 hours ago











          • @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

            – Ted Lyngmo
            9 hours ago












          • @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

            – r3mus n0x
            9 hours ago











          • @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

            – Ted Lyngmo
            9 hours ago
















          20














          This is because when using a non-trivial type in a union, suddently the default constructor is deleted.



          This creates all sorts of problem when trying to be constexpr compatible.



          Consider this code:



          struct nontrivial 
          constexpr nontrivial(int o) : uo
          int u;
          ;

          union storage
          nontrivial nt;
          ;

          struct optional
          storage s;
          ;

          constexpr auto run() -> int
          optional o;
          return o.s.nt.u;


          int main()
          constexpr int t = run();



          This is ill formed because optional has a deleted constructor.



          Then a simple fix would be to add a constructor that initialize no union member:



          union storage 
          constexpr storage() // standard says no
          nontrivial nt;
          ;


          But it won't work. Constexpr unions must have at least one active member. It cannot be an empty union. To workaround this limitation, a dummy member is added. This makes std::optional useable in constexpr context.






          share|improve this answer






















          • 6





            For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

            – Barry
            10 hours ago











          • @TedLyngmo, which version? Tried on 7.4.0 - got an error.

            – r3mus n0x
            9 hours ago











          • @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

            – Ted Lyngmo
            9 hours ago












          • @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

            – r3mus n0x
            9 hours ago











          • @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

            – Ted Lyngmo
            9 hours ago














          20












          20








          20







          This is because when using a non-trivial type in a union, suddently the default constructor is deleted.



          This creates all sorts of problem when trying to be constexpr compatible.



          Consider this code:



          struct nontrivial 
          constexpr nontrivial(int o) : uo
          int u;
          ;

          union storage
          nontrivial nt;
          ;

          struct optional
          storage s;
          ;

          constexpr auto run() -> int
          optional o;
          return o.s.nt.u;


          int main()
          constexpr int t = run();



          This is ill formed because optional has a deleted constructor.



          Then a simple fix would be to add a constructor that initialize no union member:



          union storage 
          constexpr storage() // standard says no
          nontrivial nt;
          ;


          But it won't work. Constexpr unions must have at least one active member. It cannot be an empty union. To workaround this limitation, a dummy member is added. This makes std::optional useable in constexpr context.






          share|improve this answer















          This is because when using a non-trivial type in a union, suddently the default constructor is deleted.



          This creates all sorts of problem when trying to be constexpr compatible.



          Consider this code:



          struct nontrivial 
          constexpr nontrivial(int o) : uo
          int u;
          ;

          union storage
          nontrivial nt;
          ;

          struct optional
          storage s;
          ;

          constexpr auto run() -> int
          optional o;
          return o.s.nt.u;


          int main()
          constexpr int t = run();



          This is ill formed because optional has a deleted constructor.



          Then a simple fix would be to add a constructor that initialize no union member:



          union storage 
          constexpr storage() // standard says no
          nontrivial nt;
          ;


          But it won't work. Constexpr unions must have at least one active member. It cannot be an empty union. To workaround this limitation, a dummy member is added. This makes std::optional useable in constexpr context.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 10 hours ago

























          answered 10 hours ago









          Guillaume RacicotGuillaume Racicot

          19.7k5 gold badges41 silver badges79 bronze badges




          19.7k5 gold badges41 silver badges79 bronze badges










          • 6





            For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

            – Barry
            10 hours ago











          • @TedLyngmo, which version? Tried on 7.4.0 - got an error.

            – r3mus n0x
            9 hours ago











          • @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

            – Ted Lyngmo
            9 hours ago












          • @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

            – r3mus n0x
            9 hours ago











          • @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

            – Ted Lyngmo
            9 hours ago













          • 6





            For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

            – Barry
            10 hours ago











          • @TedLyngmo, which version? Tried on 7.4.0 - got an error.

            – r3mus n0x
            9 hours ago











          • @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

            – Ted Lyngmo
            9 hours ago












          • @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

            – r3mus n0x
            9 hours ago











          • @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

            – Ted Lyngmo
            9 hours ago








          6




          6





          For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

          – Barry
          10 hours ago





          For those hunting for the standard quote: eel.is/c++draft/dcl.dcl#dcl.constexpr-4.1

          – Barry
          10 hours ago













          @TedLyngmo, which version? Tried on 7.4.0 - got an error.

          – r3mus n0x
          9 hours ago





          @TedLyngmo, which version? Tried on 7.4.0 - got an error.

          – r3mus n0x
          9 hours ago













          @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

          – Ted Lyngmo
          9 hours ago






          @r3musn0x 9.1.1. I don't think 7.4.0 implements LWG 2900.

          – Ted Lyngmo
          9 hours ago














          @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

          – r3mus n0x
          9 hours ago





          @TedLyngmo, it looks to me that LWG 2900 is only related to the library implementation of optional, so I don't really see the connection... Anyway look here - an error! :)

          – r3mus n0x
          9 hours ago













          @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

          – Ted Lyngmo
          9 hours ago






          @r3musn0x You are probably correct. Here's a comparison between g++ and clang++ and also a bugreport that seems to be connected to this: bugzilla #886581 It seems to hit my example when templates are involved, but in the bugreport they've managed to trigger the bug without templates.

          – Ted Lyngmo
          9 hours ago









          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f57496628%2fwhy-is-there-a-dummy-union-member-in-some-implemetations-of-stdoptional%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

          ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

          Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

          19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу