Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:45:47+00:00 2026-06-17T11:45:47+00:00

I’m dealing with an union in C++, and I would like have a function

  • 0

I’m dealing with an union in C++, and I would like have a function template which access to the active union member depending on a template parameter.

Code is something like (doSomething is just an example):

union Union {
  int16_t i16;
  int32_t i32;
};

enum class ActiveMember {
    I16 
  , I32
}

template <ActiveMember M>
void doSomething(Union a, const Union b) {
  selectMemeber(a, M) = selectMember(b, M);
  // this would be exactly (not equivalent) the same
  // that a.X = b.X depending on T.
}

To accomplish this I only found bad hacks like specialization, or a not homogeneous way to access and assign.

I’m missing something, and such things should be do it with other approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:45:48+00:00Added an answer on June 17, 2026 at 11:45 am

    Possibility 1

    instead of using an enum, you can use simple structs to pick the member:

    typedef short int16_t;
    typedef long int32_t;
    
    union Union {
        int16_t i16;
        int32_t i32;
    };
    
    struct ActiveMemberI16 {};
    struct ActiveMemberI32 {};
    
    template <typename M>
    void doSomething(Union& a, Union b) {
        selectMember(a, M()) = selectMember(b, M());
    
        // this would be exactly (not equivalent) the same
        // that a.X = b.X depending on T.
    }
    
    int16_t& selectMember(Union& u, ActiveMemberI16)
    {
        return u.i16;
    }
    
    int32_t& selectMember(Union& u, ActiveMemberI32)
    {
        return u.i32;
    }
    
    int main(int argc, char* argv[])
    {
        Union a,b;
        a.i16 = 0;
        b.i16 = 1;
        doSomething<ActiveMemberI16>(a,b);
        std::cout << a.i16 << std::endl;
    
        b.i32 = 3;
        doSomething<ActiveMemberI32>(a,b);
        std::cout << a.i32 << std::endl;
        return 0;
    }
    

    This requires to define a struct and a selectMember method for every member in the union, but at least you can use selectMember across many other functions.

    Note that I turned the arguments to references, you might adjust this if not appropriate.

    Possibility 2

    By casting the union pointer to the desired type pointer, you can go with a single selectMember function.

    typedef short int16_t;
    typedef long int32_t;
    
    union Union {
        int16_t i16;
        int32_t i32;
    };
    template <typename T>
    T& selectMember(Union& u)
    {
        return *((T*)&u);
    }
    
    template <typename M>
    void doSomething(Union& a, Union b) {
        selectMember<M>(a) = selectMember<M>(b);
    
        // this would be exactly (not equivalent) the same
        // that a.X = b.X depending on T.
    }
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Union a,b;
        a.i16 = 0;
        b.i16 = 1;
    
        doSomething<int16_t>(a,b);
        std::cout << a.i16 << std::endl;
    
        b.i32 = 100000;
        doSomething<int32_t>(a,b);
        std::cout << a.i32 << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.