6.8.5 Initializers and POD

Moderators: david ward, misra cpp

Post Reply
martin.dowie
Posts: 1
Joined: Wed Jun 11, 2008 11:48 am
Company: Selex Galileo

6.8.5 Initializers and POD

Post by martin.dowie » Wed Sep 04, 2013 8:36 am

Rule 8-5-1 (Required) "All variables shall have a defined value before they are used."

My reading of this rule leads me to the conclusion that "POD" classes are effectively not allowed, since any user-defined constructor provided to give initial values to non-static member data results in the class no longer being POD. Example:

Code: Select all

// Standard headers
#include <iostream>
#include <type_traits>  // std::is_pod<T>

// User-defined structs...how many are POD?
struct Trivial_Ctor            { int i;                                      };
struct With_User_Defined_Ctor  { int i; With_User_Defined_Ctor()         { } };
struct With_Initialised_Member { int i; With_Initialised_Member() : i(0) { } };

int main()
{
	std::cout <<         std::is_pod<int>::value
	          << "\n" << std::is_pod<Trivial_Ctor>::value
	          << "\n" << std::is_pod<With_User_Defined_Ctor>::value
             << "\n" << std::is_pod<With_Initialised_Member>::value << std::endl;

	return 0;
}
Results in:

1
1
0
0

Can someone please tell me if/where I'm going wrong?...

Thanks
-- Martin

misra cpp
Posts: 151
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: 6.8.5 Initializers and POD

Post by misra cpp » Tue Oct 11, 2016 10:57 am

The rule does not require that all classes have constructors, but if a constructor is provided, it must initialise all non-static members
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.8 Declarators (C++)”