Is there a way to make a C++ struct value-initialize all POD member variables?

后端 未结 3 1326
陌清茗
陌清茗 2020-12-01 20:01

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

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

and in

相关标签:
3条回答
  • 2020-12-01 20:17

    Linked Question here

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

    I am not sure whether something like that is possible [directly] or not but the following works

    prasoon@prasoon-desktop ~ $ cat check.cpp && clang++ check.cpp && ./a.out
    #include <iostream>
    struct Struct {
        std::string String;
        int Int;
        bool k;
        // add add add
    };
    
    struct InStruct:Struct
    {
       InStruct():Struct(){}
    };
    
    int main()
    {
       InStruct i;
       std::cout<< i.k << "  " << i.Int << std::endl; 
    }
    0  0
    prasoon@prasoon-desktop ~ $ 
    
    0 讨论(0)
  • 2020-12-01 20:25

    You can add a base struct:

    struct PODStruct
    {
      PODStruct(unsinged int count) { memset( this, 0, count);}
    };
    

    And then your struct derived from this base struct, first place if you have more than one base structs,

    struct Struct : PODStruct
    {
      Struct();
      std::string Str;
      int Int;
    }
    
    Struc::Struct() : PODStruct(sizeof(Struct))
    {
    }
    
    0 讨论(0)
  • 2020-12-01 20:28

    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 讨论(0)
提交回复
热议问题