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

  • Home
  • SEARCH
  • 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 800887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:21:31+00:00 2026-05-14T23:21:31+00:00

Unions aren’t something I’ve used that often and after looking at a few other

  • 0

Unions aren’t something I’ve used that often and after looking at a few other questions on them here it seems like there is almost always some kind of caveat where they might not work. Eg. structs possibly having unexpected padding or endian differences.

Came across this in a math library I’m using though and I wondered if it is a totally safe usage. I assume that multidimensional arrays don’t have any extra padding and since the type is the same for both definitions they are guaranteed to take up exactly the same amount of memory?

template<typename T> class Matrix44T
{
    ...

    union
    {
        T M[16];
        T m[4][4];
    } m;
};

Are there any downsides to this setup? Would the order of definition make any difference to how this works?

  • 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-05-14T23:21:31+00:00Added an answer on May 14, 2026 at 11:21 pm

    Although I do exactly the same in my Matrix-class I think this is implementation dependent, reading the standard to the letter:

    Standard 9.5.1:

    In a union, at most one of the data
    members can be active at any time,
    that is, the value of at most one of
    the data members can be stored in a
    union at any time. [Note: one special
    guarantee is made in order to simplify
    the use of unions: If a POD-union
    contains several POD-structs that
    share a common initial sequence (9.2),
    and if an object of this POD-union
    type contains one of the POD-structs,
    it is permitted to inspect the common
    initial sequence of any of POD-struct
    members; see 9.2. ]

    The question then is do m and M share a common initial sequence, to answer this we look at 9.2/15:

    Two POD-union (clause 9) types are
    layout-compatible if they have the
    same number of nonstatic data members,
    and corresponding nonstatic data
    members (in any order) have
    layout-compatible types (3.9).

    After reading this the answer seems to be, no m and M are not layout-compatible in the strict sense of the word.

    In practice I think this will work fine on all compilers though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables (removed columns that aren't used for my examples): CREATE
The standard defines that Unions cannot be used as Base class, but is there
I have used unions earlier comfortably; today I was alarmed when I read this
I read about unions the other day( today ) and tried the sample functions
I currently have a web-application that implements a rudimentary form of ACL using UNIONs
I have queries that use unions and I want to combine the queries into
I have a SQL query that's fairly complicated and involves multiple subqueries and unions.
Is there a way to use functions in Discriminated Unions? I am looking to
I have found out that unions are classes in c++. If you declare a
Misra says to ban all unions. I also know that deviations are allowed as

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.