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
2
votes
2 answers

int i=int(); what happens in D?

Consider following program (See live demo here) import std.stdio; void main() { int i=int(); writefln("i is %d",i); } In language like C++ the statement int i=int(); is known as value initialization. For the type int, value initialization…
Destructor
  • 14,123
  • 11
  • 61
  • 126
2
votes
1 answer

Direct Initialization vs. Value Initialization

I am a C programmer trying to learn C++11, and I've run into something I don't understand. From what I can tell, the following issue is a difference between value initialization and direct initialization. The following code snippet does not compile…
jefftime
  • 442
  • 5
  • 14
2
votes
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
  • 4,245
  • 4
  • 34
  • 56
2
votes
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…
2
votes
4 answers

Value-initialization of an input iterator

I am reading chapter 8 of the "Accelerated C++" book. Section 8.3 is about input and output iterators: vector v; // read ints from the standard input and append them to v copy(istream_iterator(cin), istream_iterator(),…
usual me
  • 8,338
  • 10
  • 52
  • 95
2
votes
3 answers

Confused on how empty user defined constructor will initialize non-static non-POD member variables

I know that default initialization for non-POD types will also default initialize non-static non-POD member variables by calling their default constructor. But I'm not sure exactly how this happens. Here is an example of what I mean: #include…
1
vote
1 answer

value initialization for automatic variables

Possible Duplicate: non-copyable objects and value initialization: g++ vs msvc Value-initializing an automatic object? Consider the following statement: It's not really possible to value-initialize an automatic object. Is this statement true? …
T.J.
  • 1,466
  • 3
  • 19
  • 35
1
vote
3 answers

Initialise std::shared_ptr using value initialisation

I was experimenting with braced initialisation and specifically using value initialisation. To my understanding int x {} uses value initialisation to set the value of x to 0. So I then tried this with a std::shared_ptr and a trivial inner class…
1
vote
1 answer

Value-initialization of class types

Per cppreference, the syntax for value initialization is: [..] T object {}; (since C++11) [..] It's already known that value-initialization is performed when an object is constructed with an empty initializer. Per, [dcl.init]/8 (emphasis…
mada
  • 1,646
  • 1
  • 15
1
vote
1 answer

Are struct scalar members zero-initialized when using value-initialization on a struct with a default non-trivial-constructor

If I have the following struct struct test { char *x; std::string y; }; And I initialize with test *t = new test(); That should value-initialize the object and do the following based on the standard: if T is a (possibly cv-qualified)…
1
vote
2 answers

Difference between = and {} syntaxes for initializing a variable in C++

I have read quite a few C++ codes, and I have come across two methods of initialising a variable. Method 1: int score = 0; Method 2: int score {}; I know that int score {}; will initialise the score to 0, and so will int score = 0; What is the…
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
  • 307
  • 3
  • 12
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
  • 11,682
  • 3
  • 43
  • 119
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
  • 501
  • 6
  • 22
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++) { …