Questions tagged [value-initialization]

Preparing an object or variable for use and assigning starting data to it.

Initialization is the process of allocating space for an object or primitive data type on the stack or heap.

Value-initialization is a specialization of the initialization process in which the allocated space is assigned starting data.

65 questions
7
votes
4 answers

How to make a user-defined type initialize *exactly* like a built-in type?

I would like to make a type that wraps a numeric type (and provides additional functionality). Furthermore, I need the number and the wrapper to be both implicitly convertible to each other. So far I have: template struct Wrapper { T…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
2 answers

In C++ are elements of an array of pointer type by default guaranteed to be initialized to nullptr?

If I have code like so class Node { public: Node *subnodes[10]; }; Node x = Node(); is it guaranteed after that code runs that x->subnodes[0] == nullptr? I'm no expert at C++, and I find C++ specs daunting. This works out in practice, but is it…
mhd
  • 115
  • 4
6
votes
1 answer

How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

Ben Voigt has pointed out here that: Zero initialization is one of the steps of static initialization. But you're right that you can't blindly substitute the latter (tag), since zero initialization is also performed for value initialization.…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
1 answer

Value initialization for classes with exclusively inherited constructors

According to cppreference non-union class types without any user-provided constructors will be zero-initialized before being constructed: If T is an non-union class type without any user-provided constructors, then the object is zero-initialized…
Tom De Caluwé
  • 926
  • 5
  • 17
5
votes
2 answers

Why does defining an empty copy ctor beside a deleted default ctor make a value initialization with empty list fail?

In short, why the code below behaves like described in the comments? struct A { A() = delete; //A(const A&) {} // uncommenting this... }; int main() { A a{}; // ... breaks this //A(); // this fails in either case because of `A() =…
Enlico
  • 23,259
  • 6
  • 48
  • 102
5
votes
1 answer

c++11 value-initialization prior to aggregate-initialization

I try to understand the first accepted answer of @bolov to the question Deleted default constructor. Objects can still be created... sometimes [1] It seems like I found a error there and so it messes up the whole explanation. @bolov explains why…
JenyaKh
  • 2,040
  • 17
  • 25
5
votes
1 answer

Why is value initialization so named?

It's really unclear to me why anyone would name a particular form of initialization "value initialization". It sounds as though it's initializing the object by giving it a value... but that's what initialization does in general, and the name doesn't…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
4
votes
3 answers

C++ object initialization with copy-list-initializer

// Example program #include #include class T{ public: int x, y; T(){ std::cout << "T() constr called..." << std::endl; }; T(int x, int y):x(x),y(y){ std::cout << "T(x,y) constr called..." <<…
Khamyl
  • 398
  • 2
  • 12
4
votes
0 answers

Is there a reason for 8 bytes of size overhead in libstdc++ std::(multi)set/map?

Lately I've been analyzing red-black tree implementations in different standard library implementations. What struck me as odd, is that libstdc++ seems to have unnecessary size overhead. Simplifying a little, the classes are composed like that: //…
Kaznov
  • 1,035
  • 10
  • 17
4
votes
1 answer

Get a default-initialized (NOT value/zero-initialized) POD as an rvalue

#include struct A { int x; }; void foo(A a) { std::cout << a.x << std::endl; } int main() { A a; foo(a); // -7159156; a was default-initialized foo(A()); // 0; a was value-initialized } Is it possible to pass an…
4
votes
4 answers

non-copyable objects and value initialization: g++ vs msvc

I'm seeing some different behavior between g++ and msvc around value initializing non-copyable objects. Consider a class that is non-copyable: class noncopyable_base { public: noncopyable_base() {} private: noncopyable_base(const…
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
4
votes
1 answer

C++ proper new usage?

int* array = new int[ 10 ]( ); Is this the proper usage of the new operator? To my knowledge the previous code will initialize each element in the array to 0. int* array = new int[ 10 ]; Does the second line of code just initialize the array, but…
cj1094
  • 81
  • 6
4
votes
1 answer

How to invoke clang++ or g++ to exactly replicate the requirements in two different standard versions

I'm trying to nail down the differences between N3337 §8.5p7 (C++11) and N3797 §8.5p8 (post C++11) that deal with value-initialization. N3337 §8.5p7: To value-initialize an object of type T means: if T is a (possibly cv-qualified) class type…
4
votes
1 answer

User-defined constructors and implicit default constructors

I have been reading this page to understand the concept of value-initialization http://en.cppreference.com/w/cpp/language/value_initialization The effects of value initialization are: If T is a class type with at least one user-provided…
usual me
  • 8,338
  • 10
  • 52
  • 95
3
votes
2 answers

3 types of Initializations

Possible Duplicate: What do the following phrases mean in C++: zero-, default- and value-initialization? Today I came to know about 3 types of initialization in C++: Zero Initialization Default Initialization Value Initialization I have…
T.J.
  • 1,466
  • 3
  • 19
  • 35