Post by BartCPost by Thiago AdamsPost by BartCPost by Thiago Adamsstruct X
{
int x = 1;
int y = 2;
};
struct X x; //initialization not used
You define a struct with default values for elements, but then allow a
struct instance to be created without initialisation. That is, with
undefined values. That would make the definition a little misleading.
I think the best way to initialize structs is using a macro "init".
struct Point { int x; int y; };
#define POINT_INIT { 0, 0 }
struct Line
{
struct Point start;
struct Point end;
};
#define LINE_INIT { POINT_INIT, POINT_INIT }
With this feature I would remove these macros.
typedef struct {int x,y;} Point;
Point point_init = {0,0};
Point origin = point_init;
typedef struct {int x = 0, y = 0;} Point;
Point origin = {};
But with the problems that have been suggested, where a Point instance
might be thought to be initialised when it isn't. Also that {} doesn't
really suggest a default; a keyword is better, but then that's not far
off using 'point_init'.
The syntax could be = {default}, = default
Post by BartCAlso, with 'point_init', this allows the defaults, or selected parts of
the defaults, to picked up anywhere.
When it comes to a Line struct, that runs into problems with the current
typedef struct {Point p,q;} Line;
Line line_init = {point_init, point_init};
because 'point_init' is not a compile-type expression. But wouldn't that
typedef struct {Point p = point_init, q = point_init;} Line;
As you said initialisers must be constant expressions?
The problem with point_init variables are many.
You cannot compose and you have to type more because you need a declaration and instantiation of the variable.
Because of these problems I use MACRO_INIT in my code.
More specifically, an initializer it not a constant expression.
It should be another initializer.
Sample:
struct Point
{
int x =1;
int y =2;
};
struct Line
{
struct Point start;
struct Point end = {.x = 3 , .y = 4};
};
Some options for syntax
//visible initialization
struct Line line = {};
struct Line line = {...};
struct Line line = {auto};
struct Line line = {default};
struct Line line = default;
struct Line line = {{default}, {default}};
struct Line line = {default, default};
//disable the end initialization
struct Line line = {{.x = 1, .y = 2}, {void}};
struct Line line = {void};
struct Line line = void;
//hidden initialization
struct Line line;
But, I'm not sure if dynamic initialization should be allowed.
I think one simple rule can be created.
The rule is : The compiler does a copy paste from the struct variable initializer into the point of instantiation.
Empty initializer will use zero.
struct Point
{
int x =1;
int y =2;
};
struct Line
{
struct Point start;
struct Point end = {.x = 3 , .y = 4};
};
struct Line line = {default};
What it does? Just copy and paste the initializers
struct Line line = {{.x = 1, .y = 2}, .x = 3, .y = 4};
This idea already have a hidden initialization
See "start"
struct Line
{
struct Point start;
struct Point end = {.x = 3 , .y = 4};
};
So, one option could be
struct Line
{
struct Point start = {default};
struct Point end = {.x = 3 , .y = 4};
};
The another option could be just accept hidden initializers.
struct Line
{
struct Point start;
struct Point end = {.x = 3 , .y = 4};
};
//both line.start and line are using hidden initialization
struct Line line;
The thirty option could be allow hidden only inside the struct.