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.

57 questions
1
vote
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…
1
vote
3 answers

How to make sure your object is zero-initialized?

Update: I'm looking to see if there's a way to zero-initialize the entire class at once, because technically, one can forget adding a '= 0' or '{}' after each member. One of the comments mentions that an explicitly defaulted no-arg c-tor will enable…
haelix
  • 3,405
  • 3
  • 26
  • 44
1
vote
1 answer

How to value-initialize aggregate types with list-initialization

How can one value-initialize aggregate types in C++14 with the list-intialization syntax? Aggregate_t {}; This is seen as aggregate initialization, which produces errors or warnings for uninitialized members of Aggregate_t. Is this possible at…
1
vote
2 answers

VS2013 list initialization

Consider the code #include "stdafx.h" #include #include struct B { public: void f() { for (auto &v : member) { std::cout << v << std::endl; } } private: int member[100]; }; int main() { B b{}; b.f(); } I…
user3701522
  • 297
  • 3
  • 10
0
votes
1 answer

How do I stop this templated function value-initializing a new constructed object?

If I can a parameter pack for the constructor arguments when I create a new object and I don't provide any constructor arguments then the result will be: new T(); which will value-initialize the object if it doesn't have a user-provided constructor.…
Zebrafish
  • 9,170
  • 2
  • 28
  • 78
0
votes
2 answers

C++ value initialize items of a custom container

Lets take custom vector implementation as an example: template class myVector { public: explicit myVector(int size = 0) : _size{ size }, _capasity{ size + SPARE_CAPACITY } { _buff = new…
user2376997
  • 443
  • 5
  • 13
0
votes
1 answer

Safe to rely on a initialised value in an unordered_map (hashmap)

Let's say I wanted to construct an std::unordered_map to map the frequency of characters in a string. I would do something like char* myString; std::unordered_map hashmap; for(char* pch = myString; *pch !=0 ; pch++) { …
0
votes
2 answers

Value initialization of nested classes

By the rules of value initialization. Value initialization occurs: 1,5) when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces (since C++11); 2,6) when an object with dynamic storage…
Darlyn
  • 4,152
  • 11
  • 30
  • 73
0
votes
3 answers

How many times are primitive data types allocated inside loops?

Here's an example: while (i < 10) { int j = 1; string k = "hello."; } j is a primitive data type and k is an object. According to Do built-in types have default constructors?, So non-class types (including fundamental…
0
votes
0 answers

How to always initialize properly?

With C++11 the Unified Initialization has been introduced and I got the impression that {} is now a quite safe initialization for all cases (when allowed). But I just noticed, that I mistaken. What is a safe form of initialization for all types?…
towi
  • 20,210
  • 25
  • 94
  • 167
0
votes
2 answers

Value initialization on explicit constructor call in C++?

Possible Duplicate: What do the following phrases mean in C++: zero-, default- and value-initialization? There are multiple places where people have said that an explicit call to the class constructor results in value initialization [when no…
asheeshr
  • 3,918
  • 5
  • 30
  • 49
-4
votes
3 answers

How to create a temporary value initialized T * in Standard-C++

How to create a temporary value-initialized T* in standard C++? void foo( int ); void bar( int * ); int main() { foo( int() ); // works. a temporary int - value initialized. bar( ??? ); // how to create a temporary int *? } Just out of…
Swordfish
  • 1
  • 3
  • 17
  • 42
1 2 3
4