Weird C ++ code snippet

I have this snippet:

template<class T>
class VECTOR_2D 
{
public:
    T x,y;

    VECTOR_2D() 
        :x(T()),y(T())
    {}
}

What initializes x and y in the constructor?

+4
source share
4 answers

xand yinitialized with a copy to a Tvalue-initialized value.

From the C ++ 03 standard, §8.5 / 7:

An object whose initializer is an empty set of brackets, i.e. (), must be initialized with a value.

And from § 8.5 / 5:

To initialize the value of an object of type Tmeans:

  • If it Tis a class type with a constructor declared by the user, then the default constructor for is called T(and initialization is poorly formed if it Tdoes not have an accessible default constructor);
  • T - , , T ;
  • T - , ;

T :

  • T , 0 (), T;
  • T - , ;
  • T - , , ) ;
  • T - , ;
  • T , .

x(T()),y(T()) x(),y(), value-initialize x y. ( , T ), , , , .

+6

() T, .

+3

- , :

VECTOR_2D() : x(), y() {};

, , - :

X x = X();

, .

+2

T() rvalue T, value - .

T , , T , ( Java: integer, float, double, char, ), 0.

+1

All Articles