C ++: automatic initialization

I find it sometimes annoying that I have to initialize all POD types manually. For instance.

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

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

I do not like this for several reasons:

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

To overcome this, I am trying to use my own types. That is, instead of using, int x,y;I use my own vector structure, which is also automatically initialized with 0. I also thought about just implementing some simple types of wrappers, for example:

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 ... */
};

, 0 ( ).

: boost::value_initialized.


, POD-:

- , , Num , . , (, float) .

Java :

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

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

, , - , init -, , int x = 42;.

, ++.

, init , :

#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; } \
}

( _LINENAME . MSVC .)

, . :

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

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

, ( ), :

  • PIVar ( PreInitVar), - . , .
  • .

? ?


, , , ++ 0x , Java. ? ++ 0x.


, :

  • ", Java " / ", ++ then"
  • ", - , , , - "
  • " ".

, , . . , , , . , , . , , . , , , .

+3
6

++ 0x, :

struct A {
    int x = 42;
};

, .

, - , , ( ).

++, , , , .

0

- , .

.

struct POD {
  int i;
  char ch;
};

POD uninitialized;

POD initialized = POD();

:

class myclass
  POD pod_;
  // ....
  myclass()
   : pod_() // pod_ members will be initialized
  {
  }

, .

:

void f(int&);

Num<int> i;
f(i);

, , , .


? ?

, . , , : POD . :

struct ML_LieroX : MapLoad {        
    std::string       id;
    PIVar(int, 0)     type;
    std::string       themeName;
    PIVar(int, 0)     numObj;
    PIVar(bool,false) isCTF;

. ? ? ++?

, ? ?

bon mot: , , . , , , , . , , 10% , , - , .

+10

Boost value_initialized<T>, (POD ). .

; POD; -POD- , .

+7

POD :

struct POD
{
    int x;
    float y;
};

int main()
{
    POD a = {};            // initialized with zeroes
    POD b = { 1, 5.0f };   // x = 1, y = 5.0f

    return 0;
}
+2

++ , ?

struct AState
{
    int x;
    AState() : x(42) {}
};

class A : AState
{
    A() {}
    A(/*...*/) { /*...*/ }
    A(/*...*/) { /*...*/ }
};

x . , A::A.

0

++ 0x , , , , ( ). boost:: initialized_value, , ( , ++).

template<typename T> struct Default { T operator()() { return T(); } };
template<typename T, T (*F)()> struct Call { T operator()() { return F(); } };
template<int N> struct Integer { int operator()() { return N; } };

template< typename X, typename Value = Default<X> >
class initialized {
public:
  initialized() : x(Value()()) {}
  initialized(const X& x_) : x(x_) {}
  const X& get() const { return x; }
  operator const X&() const { return x; }
  operator X&() { return x; }
private:
  X x;
};

:

struct Pi { double operator()() { return 3.14; } }; //Exactly
const char* init_message() { return "initial message"; }
Point top_middle() { return Point(screen_width()/2, 0); }

struct X {
  initialized<int> a;
  initialized<int, Integer<42> > b;
  initialized<double> c;
  initialized<double, Pi> d;
  initialized<std::string> e;
  initialized<std::string, Call<const char*, init_message> > f;
  initialized<Point> g;
  initialized<Point, Call<Point,top_middle> > h;
  X() {}
};

, / ( ).

, typedef .

, ++ 0x/11/14/whatever.

0

Source: https://habr.com/ru/post/1766051/


All Articles