73 questions
0
votes
0
answers
132
views
Confusion on value initialization [duplicate]
I have the below code with the below output:
#include <iostream>
#include <string>
using namespace std;
class P {
public:
P() {
cout << "P() ctor" << ...
0
votes
1
answer
149
views
C++17 Are non-static POD class members initialized with curly braces {} set to 0?
Got code review comment that my non-static class POD members be set to an invalid value. I agree that the initial value should be invalid. For my cases, 0 is invalid. Here's an example:
class A
{
...
-1
votes
1
answer
107
views
why default constructor is not used in value initialisation in c++ [duplicate]
c++ primer says :
The default constructor is used automatically whenever an object is default or
value initialized.
Default initialization happens
• When we define nonstatic variables (§ 2.2.1, p. 43)...
3
votes
0
answers
121
views
C++: Rules for this return {} initialization
I am trying to understand the rules governing the following initialization:
struct A {
const char* s_ptr;
std::string str;
};
A Foo() {
return {};
}
A a = Foo();
As per my understanding, a....
1
vote
1
answer
535
views
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 ...
1
vote
1
answer
588
views
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 ...
4
votes
3
answers
201
views
C++ object initialization with copy-list-initializer
// Example program
#include <iostream>
#include <string>
class T{
public:
int x, y;
T(){
std::cout << "T() constr called..." << std::endl;
};
...
0
votes
1
answer
36
views
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 ...
5
votes
2
answers
212
views
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
//...
6
votes
2
answers
474
views
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++ ...
3
votes
2
answers
403
views
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 <array>
#include <vector>
int main()
{
std::array<int, 2>{42, 42}; // ...
3
votes
2
answers
139
views
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 ...
8
votes
1
answer
1k
views
Does value initialization work for atomic objects?
By work here, I take to mean that std::atomic<T> a{} effectively zero initializes a. I have always been thinking so and have been practically using it until this. Before explaining my ...
0
votes
2
answers
244
views
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 ...
8
votes
2
answers
2k
views
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....
5
votes
1
answer
648
views
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 ...
7
votes
1
answer
245
views
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 ...
2
votes
1
answer
117
views
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 ...
3
votes
1
answer
3k
views
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 ...
2
votes
1
answer
735
views
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 ...
2
votes
3
answers
552
views
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 ...
-4
votes
3
answers
297
views
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( ??? ); /...
14
votes
1
answer
3k
views
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 ...
6
votes
1
answer
189
views
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 ...
10
votes
3
answers
5k
views
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 '...