c++11 - C style use of a modern C++ class -


i have class so:

snippet 1:

struct b {     int member;     // more complex members e.g. arrays of structs etc }; 

this class used in code assuming c style struct (e.g. used in memcpy, memset etc)

as part of programming principles, contemplating modifying b so:

snippet 2

struct b  {     b() {}     int member;     // more complex members e.g. arrays of structs etc }; 

here understanding. please correct if got wrong a. snippet 1 defines b pod whereas snippet 2 defines b not pod, , b. in snippet 2, b can still legitimately used c style uses such memset , memcpy

  1. std::is_pod : false
  2. std::is_trivial : false
  3. std::is_trivially_copyable : true
  4. std::is_trivial : false

extending, follow rule of big 3 (or perhaps big 5), add copy assignment copy constructor.

snippet 3:

struct b {     b() {}     ~b(){}     b& operator=(b &){ return *this; }     b(b const &r){}     int member;      // more complex members e.g. arrays of structs etc }; 

here understanding. please correct if got wrong a. in snippet 3, b can no longer legitimately used c style uses such memset , memcpy

  1. std::is_pod : false
  2. std::is_trivial : false
  3. std::is_trivially_copyable : false
  4. std::is_trivial : false

i need understanding on effect of adding various members b in snippet 2 , snippet 3 on c style uses of b.

is c style use of class based on is_trivially_copyable or on is_trivial (which more restrictive)?

reading $3.9/2 indicates me in c++11 "is_trivially_copyable" determining criteria. old c++ books (c++ in nutshell e.g.) indicate criteria pod vs non pod , understand c++11 rules have changed since.

petes response seems indicate trivial-ness necessary criteria

if struct contains pod data members, , not responsible managing resources, such dynamically allocated memory, there's absolutely no reason define destructor, copy constructor , assignment operators. @ i'd add constructor initialize data members. , c++11, can done trivially using non-static data member initializers.

struct b {     int member = 0; // initialize data member 0 }; 

you add constructor takes int , allows initialize member desired value. in case

struct b {     int member;     explicit b(int member = 0) : member(member) {} }; 

both of these changes leave class trivially copyable (9/6) , standard layout (9/7), means you'll able use them c-style functions.


from discussion in comments, seems you're not interested in being able initialize data members, still want provide default constructor.

struct b  {     b() {}     int member;     // more complex members e.g. arrays of structs etc }; 

this bad idea because doesn't add useful class, , makes non-trivial because has non-trivial default constructor. if want define default constructor, should explicitly default instead.

struct b  {     b() = default;     int member;     // more complex members e.g. arrays of structs etc }; 

this leaves class trivial, , standard layout, allowing use c style functions.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -