structure initialization question

Hi guys. Imagine you have the following structure: ...

1
2
3
4
struct product {
  int weight;
  double price;
} ;


what's the difference between the 2 following statements? : ...

statement 1:

 
product apple;


statement 2:

 
product apple = {};


(there must be some important difference since it's making a program's execution I'm dealing with produce different behaviors)

thanks in advance!

(More info: go to

https://docs.microsoft.com/en-us/windows/win32/learnwin32/creating-a-window?view=vs-2019

and check the ...

 
WNDCLASS wc = { };


... statement. The window won't draw if you use:...

 
WNDCLASS wc;



... instead.)
Using braces in the initializer gives the members a value of zero.
Using an empty initializer does not initialize the members, leaving them with a garbage value that cannot be used in a correct C++ program.

More formally, product is an aggregate type, and is subject to aggregate initialization when list-initialized (i.e., when braces are used):
https://en.cppreference.com/w/cpp/language/aggregate_initialization

When no initializer is present, the rules of default-initialization apply:
https://en.cppreference.com/w/cpp/language/default_initialization
Last edited on
Thanks a lot.

The thing is the syntax of the default initialization you refer on cppreference.com DOES NOT use the '=' operator.

Notice the '=' operator:

product apple = {};

WNDCLASS wc = { };

according to cppreference.com you mustn't use the '=' operator, so what does that '=' operator mean?. Where is it referred within the C++ official specification (for the case of default initialization)?

Check this out:

https://docs.microsoft.com/en-us/cpp/cpp/initializers?view=vs-2019#default_initialization

Even Microsoft won't introduce the '=' operator for default initialization (and so the '='-free default initialization should work, but it doesn't)! It's really annoying.

EDIT: --------------------------------

The following piece of code works right on Visual Studio but NOT on Code::Blocks (notice the circle structure uses a constructor) ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
#include "iostream"



// std::cout <<

int variar;


struct Circle {
public:
	double radius;
	Circle() { radius = 5.7; }
};

int main() {
     // Circle mycirc (10.0);    functional form
    // Circle mycirc = 20.0;    assignment init.
    //  Circle mycirc {30.0};   uniform init.
    
	Circle mycirc = {}; // POD-like

	std::cout << mycirc.radius << '\n';
	std::cin >> variar;
	return 0;
}
Last edited on
The thing is the syntax of the default initialization you refer on cppreference.com DOES NOT use the '=' operator.

Because the class is an aggregate, it is aggregate-initialized and not default-initialized. The page about aggregate initialization discusses the relevant syntax.

Notice that the initializer is = {}, not merely the stuff inside the (empty) braces; an example of a declaration with no initializer would be product apple; and not product apple = {};.

Where is it referred within the C++ official specification

[dcl.init] discusses the formal grammar production that defines initializer.
http://eel.is/c++draft/dcl.init#1
[dcl.init.aggr] defines aggregate initialization and discusses the behavior of the initializer in context.
http://eel.is/c++draft/dcl.init#aggr
Last edited on
Topic archived. No new replies allowed.