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
3
votes
1 answer

Can I choose between default-initalisation and value-initialisaion or all or part of an array?

Since C++2003 we have value-initialisation as well as default-initialisation. Meaning that: struct Foo { int i; std :: string s; }; Foo f1; // f1.s is default-constructed, f1.i is uninitialised Foo f2 = Foo (); // f1.s is…
spraff
  • 32,570
  • 22
  • 121
  • 229
3
votes
3 answers

Does resize on a std::vector set the new elements to zero?

Consider #include int main() { std::vector foo; foo.resize(10); // are the elements of foo zero? } Are the elements of foo all zero? I think they are from C++11 onwards. But would like to know for sure.
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
3
votes
0 answers

default initialization, value initialization and in-class-initialization of object not getting properly?

You might feel code is long and I write lot of things but believe me it is quite simple. I referred these two answers on stack-overflow for understanding this. post1 & post2 Code #include class A { public: int…
3
votes
2 answers

why is list initialization not invoked when initialize this class?

according to value initialization described in this page https://en.cppreference.com/w/cpp/language/value_initialization If T is a class type that has no default constructor but has a constructor taking std::initializer_list, list-initialization is…
3
votes
2 answers

Value initialize aggregate

Tried C++ standard, but couldn't figure it out. Are these equivalent? double x[2] = {0.0, 0.0}; and double x[2] = {}; How about these? struct A { double x[2]; }; A a = {0.0, 0.0}; and A a = {}; Thank you!
pic11
  • 14,267
  • 21
  • 83
  • 119
3
votes
2 answers

Are there any difference in empty parentheses (“T()”) and empty braces (“T{}”) when used as initializers?

Generally speaking, parentheses and braces are very different. For minimal reproducible example: #include #include int main() { std::array{42, 42}; // OK std::array(42, 42); // ill-formed …
3
votes
2 answers

Value initialization from signed char to integer, premature promotion?

In this piece of code: signed char v = -64; int n = 4; int x = v - '0' * (signed char)n; std::cout << x << std::endl; Should x be -5 or -261? In my understanding, the initializer expression has signed char type, and the type conversion should…
ABu
  • 10,423
  • 6
  • 52
  • 103
3
votes
1 answer

Elegant avoiding of value-initialization of vector elements in future C++?

When elements are default-inserted into an instance of std::vector, they are value-initialized by default. I often work with multi-threaded high-performance codes, where such value-initialization might for large arrays represent an unacceptable…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
3
votes
1 answer

How to force compiler to set a non-zero value to uninitialized variables?

I am playing with C++ value initialization. Therefore, I am printing uninitialized values in order to highlight (un)initialization depending on C++ standard version. But uninitialized values convey often the zero value :-( How to highlight if a…
oHo
  • 51,447
  • 27
  • 165
  • 200
3
votes
1 answer

Braces (without constructor) initialization of a derived class

struct base { }; //struct derived { // <--- this one works struct derived : public base { // <--- but this one doesn't double x; }; main () { derived d{0.5}; return 0; } What is the way to initialize derived using braces, meaning without…
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
2
votes
1 answer

Why `default` ctor zero-initializes class members?

Given two classes with different constructors: #include struct A { int x; A() {}; }; struct B { int x; B() = default; }; int main() { int x = 5; x = 7; printf("before: %d\n", x); new(&x) A(); …
vladon
  • 8,158
  • 2
  • 47
  • 91
2
votes
1 answer

Object literal alternative in C++

I'm coming from Javascript like language where I can make functions like this. function tween(obj, tweenParams, time, tweenOptions); This is used like: tween(mySprite, {x: 10, y: 10}, 1, {startDelay: 1, loops: 5}); If it's not clear, this lerps…
2
votes
3 answers

Do Ideone and Codepad really not support C++03?

My question is related to Prasoon's question about non POD types and value initialization. I tried the following code on online compilers like Ideone and Codepad but the executables gave runtime error on both the sites. #include #include…
2
votes
0 answers

Initialization of class with deleted default constructor in different c++11 standard versions

In the duplicate there is no answer to the points 1 and 4 of my question at least. And they are the most important. I can delete the other points but I ask not to close the whole question. 1. In code below obj1 is created normally. But if I try to…
JenyaKh
  • 2,040
  • 17
  • 25
2
votes
1 answer

Do we need an accessible copy constructor for value initialization in C++98/03?

This question refers only to pre C++11. Consider the following seemingly broken code: struct X { X(){} // default user-provided constructor private: X(const X&){} }; int main() { X x = X(); } Live on Coliru According to…
vsoftco
  • 55,410
  • 12
  • 139
  • 252