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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:43:18+00:00 2026-05-17T19:43:18+00:00

Suppose I have a C++ struct that has both POD and non-POD member variables:

  • 0

Suppose I have a C++ struct that has both POD and non-POD member variables:

struct Struct {
    std::string String;
    int Int;
};

and in order for my program to produce reproduceable behavior I want to have all member variables initialized at construction. I can use an initializer list for that:

 Struct::Struct() : Int() {}

the problem is as soon as I need to change my struct and add a new POD member variable(say bool Bool) I risk forgetting to add it to the initializer list. Then the new member variable will not be value-initialized during struct construction.

Also I can’t use the memset() trick:

Struct::Struct()
{
   memset( this, 0, sizeof( *this ) ); //can break non-POD member variables
}

because calling memset() to overwrite already constructed non-POD member variables can break those.

Is there a way to enforce value-initialization of all POD member variables without explicitly adding their initialization in this case?

  • 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-17T19:43:19+00:00Added an answer on May 17, 2026 at 7:43 pm

    The cleanest way would be to write the auto-initialzed template class initialized<T>:

    EDIT: I realize now it can be made even more flexible by allowing you to declare initialized<Struct>. This means that you can declare initialization without modifying the original Struct. The default initialization ‘T()’ was inspired on Prasoons answer.

    template<class T>  
    struct initialized 
    { 
    public: 
    
         initialized() 
            { value = T(); }
    
        initialized(T t) 
            { value = t; }
    
        initialized(const initialized<T>& x) 
            { value = x.value; }
    
        T* operator &() { return &value; } 
    
         operator T&() { return value; }     
    
    private: 
         T value; 
    };
    
    
    struct PodStruct 
    {            
        std::string String;      
        int Int; 
    };  
    
    
    struct GlorifiedPodStruct 
    {            
        std::string String;      
        initialized<int> Int; 
    };  
    
    void Test()
    {
        GlorifiedPodStruct s;
        s.Int = 1;
        int b = s.Int;
        int * pointer = &s.Int;
    
        initialized<PodStruct> s2;
    }
    

    This compiles, but may need more conversion operators, handling of keywords like volatile, etc. But you get the idea.

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

Sidebar

Related Questions

Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a struct called foo_boolean that contains some boolean values: struct foo_boolean
Suppose we have a struct in C++: struct foobar { int age; bool hot;
Suppose I have my node structure as: struct Employee { int age; int salary;
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Suppose I have: struct Vec3 { double x; double y; double z; } ;
Suppose I have a game written in C/C++. For simplicity, I have: struct masterStruct

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.