Using POD instead of boost::value_initialized

Hi,

Is the following a good idea to replace boost::value_initialized? I would like to avoid having to depend on boost but I also want to ensure that all my variables are initialized before use.

This trick basically takes advantage of initialization of POD structures.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Test
{
public:
  Test()
  {
    std::cout << "Test" << std::endl;
  }

  ~Test()
  {
    std::cout << "~Test" << std::endl;
  }

  int a;

  struct TestPod
  {
    int b;
  } pod;
};

int main(int argc, char *argv[])
{
  Test test;

  std::tr1::shared_ptr<Test> sTest(new Test());

  std::cout << test.a << std::endl;
  std::cout << sTest->a << std::endl;

  std::cout << test.pod.b << std::endl;
  std::cout << sTest->pod.b << std::endl;

  return 0;
}


The output from this should be something like

Test
Test
234235
123123
0
0
~Test
~Test

Would anyone recommend this or should I be using a primitive container or such?
Also would this work for C++03 (C++98 and older)?

Thanks :)
Last edited on
Topic archived. No new replies allowed.