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 6084659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:32:53+00:00 2026-05-23T11:32:53+00:00

Possible Duplicate: Why compiler doesn't allow std::string inside union ? I knew that I

  • 0

Possible Duplicate:
Why compiler doesn't allow std::string inside union ?

I knew that I had this problem when I started with C++: The compiler wouldn’t allow me to put a variable of the type std::string into unions.

That was years ago, but actually I still don’t know the exact answer. I read something related to a copy function with the string that the union didn’t like, but that’s pretty much all.

  • Why are C++ STL strings incompatible with unions?
  • 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-23T11:32:53+00:00Added an answer on May 23, 2026 at 11:32 am

    From Wikipedia:

    C++ does not allow for a data member to be any type that has a full fledged constructor/destructor
    and/or copy constructor, or a non-trivial copy assignment operator. In particular, it is impossible to
    have the standard C++ string as a member of a union.

    Think about it this way: If you have a union of a class type like std::string and a primitive type (let’s say a long), how would the compiler know when you are using the class type (in which case the constructor/destructor will need to be called) and when you are using the simple type? That’s why full-fledged class types are not allowed as members of a union.

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

Sidebar

Related Questions

Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
Possible Duplicate: How was the first compiler written? This question has always been bothering
Possible Duplicate: 'Delegate 'System.Action' does not take 0 arguments.' Is this a C# compiler
Possible Duplicate: Custom Compiler Warnings Duplicate: Custom Compiler Warnings I'm new to writing my
Possible Duplicate: Plugging in to Java compilers Edit - this appears to be a
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: In C++ why can’t I write a for() loop like this: for(
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: Nullable types and the ternary operator. Why won’t this work? This is
Possible Duplicate: Java - why no return type based method overloading? The compiler does

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.