1

Consider this code:

template <typename T>
void f()
{T x = T();}

When T = int, is x equal to 0 or to an arbitrary value?

Bonus question: and consequently, are arrays (both T[N] and std::array<T, N>) the only types where such a syntax may leave contents with arbitrary values.

Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 2
    0. However, `int x;` is undefined. – Rapptz Aug 18 '13 at 04:12
  • 1
    Regarding `std::array`, you can value-initialize them using universal initialization. I.e. `std::array data{};`, But you are correct that, like fixed arrays, they are indeterminate without *some* sort of initial value (this way, or direct). – WhozCraig Aug 18 '13 at 04:23
  • Possibly related: http://stackoverflow.com/questions/8106016/c-default-initialization-and-value-initialization-which-is-which-which-is-ca – Violet Giraffe Aug 18 '13 at 05:42

2 Answers2

3

T() gives value initialization, which gives zero-initialization for a type other than a class, union or array. (§8.5/7 bullet 3): "otherwise, the object is zero-initialized." For an array, each element of the array is value initialized.

For an array, the content will be of an arbitrary value if it's auto storage class, but zero initialized if it's static storage class -- i.e., global (assuming, of course, that you don't specify any initialization).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • And does `T x{};` instead of `T x = T();` would solve the problem for all types (no arbitrary value whatever `T` is) ? – Vincent Aug 18 '13 at 04:32
  • 1
    @Vincent: Yes, `T x{};` should always give a value initialized object (§8.5.1/7): "If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from an empty initializer list". – Jerry Coffin Aug 18 '13 at 04:37
  • @Vincent also, `x()` or `x{}` in an constructor initializer list would value-initialize an array. – juanchopanza Aug 18 '13 at 06:29
2

Regarding your first question, it is called value-initialization, and is well-covered in the standard (C++11 § 8.5 covers initialization in detail, with the specifics on () initialization and how it eventually leads to zero-initialization, starting with 8.5/16 (covers ()), which leads to 8.5/7 (value-initialization), and finally 8.5/5 (zero-initialization).

Regarding std::array<T,N>, if T is a class-type the constructors will fire for each element (user-provided or default if none are provided by the user). If default construction happens default-initialization will fire (which isn't very exciting). By the standard (8.5/6), each element is default-initialized. For T that is not a class type this is effectively the same as T ar[N]; which as you have pointed out, is indeterminate as well (because it is default initialized, which by the standard "no initialization is performed.".

Finally, if static storage is declared for your fixed array of non-class-type, it resides in zero-filled memory on inception. For automatic storage, its back to "no initialization is performed." as your end-game.

I hope i didn't miss something (and I know i'll hear it if I did). There are lots of interesting questions on SO that cover areas like this. If I get a chance I'll link a few.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141