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

  • Home
  • SEARCH
  • 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 1096799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:20:01+00:00 2026-05-17T00:20:01+00:00

I find it sometimes annoying that I have to initialise all POD-types manually. E.g.

  • 0

I find it sometimes annoying that I have to initialise all POD-types manually. E.g.

struct A {
    int x;
    /* other stuff ... */

    A() : x(0) /*...*/ {}
    A(/*..*/) : x(0) /*...*/ {}
};

I don’t like this for several reasons:

  • I have to redo this in every constructor.
  • The initial value is at a different place than the variable declaration.
  • Sometimes the only reason I have to implement a constructor is because of this.

To overcome this, I try to use my own types instead. I.e. instead of using int x,y;, I use my own vector struct which also initialize automatically with 0. I also thought about just implementing some simple wrapper types, like:

template<typename T>
struct Num {
    T num;
    Num() : num(0) {}
    operator T&() { return num; }
    operator const T&() const { return num; }
    T& operator=(T _n) { num = _n; return num; }
    /* and all the other operators ... */
};

This basically solves this so far for all cases where I want to init with 0 (that are by far the most often cases for me).

Thanks to James McNellis for the hint: This can also be solved via the boost::value_initialized.


Now, not limited to POD-types:

But sometimes I want to initialise with something different and there are the troubles again because that Num template struct cannot easily be extended to allow that. Basically because I cannot pass floating point numbers (e.g. float) as a template parameter.

In Java, I would just do:

class A {
    int x = 42;
    /*...*/

    public A() {}
    public A(/*...*/) { /*...*/ }
    public A(/*...*/) { /*...*/ }
    /*...*/
}

I find it quite important that in such cases where you want to init a member variable always in the same way in all possible constructors, that you are able to write the init value directly next to the member variable, like in int x = 42;.

So the thing I was trying to solve is to do the same thing in C++.

To overcome the problem that I cannot pass the init-value via a template parameter, I hacked together an ugly macro:

#define _LINENAME_CAT( name, line ) name##line
#define _LINENAME( name, line ) _LINENAME_CAT( name, line )

/* HACK: use _LINENAME, workaround for a buggy MSVC compiler (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=360628)*/
#define PIVar(T, def) \
struct _LINENAME(__predef, __LINE__) { \
 typedef T type; \
 template<typename _T> \
 struct Data { \
  _T var; \
  Data() : var(def) {} \
 }; \
 Data<T> data; \
 T& operator=(const T& d) { return data.var = d; } \
 operator const T&() const { return data.var; } \
 operator T&() { return data.var; } \
}

(For other compilers, I can just omit that _LINENAME name for the struct and just leave it unnamed. But MSVC doesn’t like that.)

This now works more or less like I want it. Now it would look like:

struct A {
    PIVar(int,42) x;
    /*...*/

    A() {}
    A(/*...*/) { /*...*/ }
    A(/*...*/) { /*...*/ }
    /*...*/
};

While it does what I want (mostly), I still am not fully happy with it:

  • I don’t like the name PIVar (which stands for PreInitVar) but I really couldn’t come up with something better. At the same time, I want to have it short.
  • I don’t like that macro hack.

How have you solved this? Any better solution?


There was an answer which was deleted again which said that C++0x allows basically the same syntax as in Java. Is that true? So then I would just have to wait for C++0x.


Please don’t give any comments like:

  • “then just use Java instead” / “don’t use C++ then” or
  • “if you need something like this, you are probably doing something wrong” or
  • “just don’t do it this way”.

Also, please don’t tell me not to use it. I know about all the drawbacks of my current solution. Please only make comments about non-obvious drawbacks if you are really sure that I am not aware of that. Please don’t just state that there are many drawbacks in my current solution. Please also don’t state that it is not worse to use it. I am just asking if you know about a better solution than the one I have presented here.

  • 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-17T00:20:02+00:00Added an answer on May 17, 2026 at 12:20 am

    There is a proposal for C++0x which allows this:

    struct A {
        int x = 42;
    };
    

    That is exactly what I want.

    If this proposal is not making it into the final version, the possibility of delegating constructors is another way of at least avoiding to recode the initialization in every single constructor (and at the same time avoiding a dummy helper function to do this).

    In current C++, there does not seem to be any better way to do it despite what I have already demonstrated.

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

Sidebar

Related Questions

It does not happen all the time but sometimes I find stuff like this
When using Mercurial I sometimes find that it is hard to understand the relationship
I find that local builds will sometimes take 5-10 minutes of an application we
Sometimes I find myself in the situation where I want to execute several sequential
jQuery selectors are wonderful, but I sometimes I find myself typing them over and
Sometimes when using eclipse it loses references to the JRE. i.e. It cannot find
I find that getting Unicode support in my cross-platform apps a real pain in
I find that the .NET event model is such that I'll often be raising
I have a situation in which we have two production databases that synchronize with
I sometimes need to see what SQL statement SubSonic generates. That works great with:

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.