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
161
votes
8 answers

How do I mock an autowired @Value field in Spring with Mockito?

I'm using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value("#{myProps['default.url']}") private String defaultUrl; @Value("#{myProps['default.password']}") private String defaultrPassword; // ... From my JUnit test,…
Dave
  • 17,420
  • 96
  • 300
  • 582
46
votes
3 answers

Do built-in types have default constructors?

After reading this article I made a point that int () yields 0 because the temporary int is value initialized and not because int() calls the default constructor for int. (The article is flawed according to my understanding.) I also said that…
Prasoon Saurav
  • 85,400
  • 43
  • 231
  • 337
36
votes
2 answers

The behavior of value-initializing an enum

First, I want to say, according to cppreference.com, it is somewhat impossible to value-initialize an enum. According to http://en.cppreference.com/w/cpp/language/value_initialization, value-initializing an enum actually performs…
Lingxi
  • 13,248
  • 1
  • 32
  • 74
20
votes
1 answer

What does 'value initializing' something mean?

Possible Duplicate: What do the following phrases mean in C++: zero-, default- and value-initialization? If I have a class for example: class Info { int x; int y; }; which I used to created an object, Info *p = new Info(); Does the…
user1086635
  • 1,426
  • 1
  • 13
  • 20
17
votes
2 answers

Value initialization and Non POD types

An hour ago I posted an answer here which according to me was correct. However my answer was downvoted by Martin B. He said You're just lucky and are getting zeros because the memory that i was placed in happened to be zero-initialized. This is not…
Prasoon Saurav
  • 85,400
  • 43
  • 231
  • 337
15
votes
1 answer

Why does Foo({}) invoke Foo(0) instead of Foo()?

Executables produced by clang 3.5.0 and gcc 4.9.1 from the code #include struct Foo { Foo() { std::cout << "Foo()" << std::endl; } Foo(int x) { std::cout << "Foo(int = " << x << ")" << std::endl; } Foo(int x, int y) { std::cout…
12
votes
3 answers

Why is this simple assignment undefined behaviour?

I was refreshing my understanding of value-initialisation versus default-initialisation, and came across this: struct C { int x; int y; C () { } }; int main () { C c = C (); } Apparently this is UB because In the case of C(),…
spraff
  • 29,265
  • 19
  • 105
  • 197
12
votes
3 answers

Add member to existing struct without breaking legacy code

There is the following definition in some legacy code that I am working with. struct VlanData { uint16_t mEtherType; uint16_t mVlanId; }; I want to add a new member to this struct: struct VlanData { uint16_t mEtherType; uint16_t…
LeopardSkinPillBoxHat
  • 26,928
  • 14
  • 70
  • 108
12
votes
1 answer

Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard?

Cheers and hth. - Alf made a comment in this answer that value initialization is arguably a new feature of C++03 compared to C++98. I wonder what he meant. Is value initialization part of C++98? Is it present in concept but not in name? Why was it…
Praxeolitic
  • 17,768
  • 12
  • 57
  • 109
9
votes
1 answer

Value initialization of POD struct is a constexpr?

Consider the struct: struct mystruct { }; Is it true that this is always valid: constexpr mystruct mystructInstance = mystruct(); i.e. that value initialization of POD is a constexpr? Similarly how about if the struct is defined to be: struct…
Andrew Parker
  • 1,265
  • 1
  • 14
  • 23
9
votes
3 answers

VS2013 default initialization vs value initialization

Consider the code below struct B { B() : member{}{}; int member[10]; }; int main() { B b; } VS2013 compiler gives the following warning: warning C4351: new behavior: elements of array 'B::member' will be default initialized 1> …
8
votes
1 answer

Does value initialization work for atomic objects?

By work here, I take to mean that std::atomic a{} effectively zero initializes a. I have always been thinking so and have been practically using it until this. Before explaining my understanding of this, I want to show that, at the very least,…
Lingxi
  • 13,248
  • 1
  • 32
  • 74
8
votes
2 answers

float initialization from double with braces

Why does the compiler (clang,gcc) not warn about narrowing conversions when doing this float a{3.1231231241234123512354123512341235123541235}; float a = {double(3.1231231241234123512354123512341235123541235)} I expected a warning because I do…
Gabriel
  • 7,477
  • 5
  • 47
  • 87
8
votes
4 answers

Explicit Type Conversion and Multiple Simple Type Specifiers

To value initialize an object of type T, one would do something along the lines of one of the following: T x = T(); T x((T())); My question concerns types specified by a combination of simple type specifiers, e.g., unsigned int: unsigned int x =…
James McNellis
  • 327,682
  • 71
  • 882
  • 954
7
votes
1 answer

Ambiguity between default-initialization and value-initialization

I found many articles explaining the difference between "default-initialization and value-initialization" but in fact I didn't understand clearly. Here's an example: class A{ public: int x; }; int main(){ A a;// default initialization…
1
2 3 4