Questions tagged [designated-initializer]

In Cocoa programming, a designated initializer is the method through which all of the instance's initial parameters can be set. A designated initializer is typically executed implicitly when not called explicitly. In C programming, designated initializers refer to a certain way of struct/union/array initialization, which allows the programmer to to initialize an element by using its name or index.

In Cocoa programming, a designated initializer is a method that accepts all of the instance's initial parameters and initializes the instance. This method is typically something other than - (id)init, and is usually documented as the designated initializer in the header file. A designated initializer is typically executed implicitly when not called explicitly. As an example, the designated initializer of UIViewController is - (id)initWithNibName:bundle:.


In C programming, designated initializers refer to a certain way of struct/union/array initialization, which allows the programmer to to initialize an element by using its name or index. This feature was introduced with the C99 standard.

Designated initializers have two forms (ISO 9899:2011 6.7.9):

If a designator has the form
[ constant-expression ]
then the current object (defined below) shall have array type and the expression shall be an integer constant expression. If the array is of unknown size, any nonnegative value is valid.

If a designator has the form
.identifier
then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

Example of designated initializer usage (9899:2011 6.7.9/35):

struct { int a[3], b; } w[] =
       { [0].a = {1}, [1].a[0] = 2 };

In the example, both the above mentioned forms of designated initializers are combined.

135 questions
1
vote
1 answer

std::make_unique and designated initializers

Consider this structure: struct MyStruct { int my_number; std::string my_string; }; Is it possible to create a std::unique_ptr using Designated initializers? Like this: // like this auto my_struct = std::make_unique{.my_number = 4,…
uni
  • 539
  • 1
  • 9
  • 21
1
vote
0 answers

Why does MSVC not auto-generate code for an explicitly-defaulted, noexcept default constructor used to copy construct a designated initializer?

See this godbolt: struct Foo { Foo() noexcept = default; int t{}; }; struct Bar { Foo baz{}; }; int main() { return Bar { .baz = Foo{} }.baz.t; } When deleting noexcept, code for the constructor is auto-generated…
1
vote
3 answers

Is there an alternative to Range Initialization of arrays in C?

I believe it's a simple question: Is there a way to initialize an array with values that are nonzero without using loops? One way I know is to use Range Initialization to initialize the array with the values I want. For example: int main() { /*…
1
vote
1 answer

Undefined behaviour of Designated initializers in C++

The following C++20 program is accepted without any warning in all compiles I have tried: struct A { const int & x = z; int y = x; int z; }; int main() { return A{.z=3}.y; } https://gcc.godbolt.org/z/nqb95zb7c But every program returns some…
user5545734
1
vote
2 answers

What is this ```typedef``` declaration?

I was researching about state machines using C, and I ran into this code from this site. There is some typedef declaration that I had never seen before: typedef eSystemState (*const afEventHandler[last_State][last_Event])(void); and it is used…
1
vote
1 answer

Overload struct default values with Designated Initializers

Is it possible to achieve (at least something similar to) this? I need Designated Initializers for "named arguments" and/or possibility to skip setting of some params (not shown here). And still get this "cascade" default values. Ideally I need set…
1
vote
1 answer

designated Initialization of a Node with a pointer (C++)

When creating a new node in a linked list, is it legal to use designated initializers to initialize the members of the node as mentioned below? Is there any repercussion in doing so and what would be a better way to achieve the same result? (lang :…
1
vote
1 answer

Static struct initialization without designated initializers?

The following designated initializer example is valid in Visual Studio 2019 with /std:c++latest, but I'm wondering how to accomplish the same thing without designated initializers in Visual Studio 2017. I am using C++ and I realize there is an…
Stradigos
  • 814
  • 1
  • 13
  • 29
1
vote
3 answers

Iterable Designated Initializer Alternative in C++

In C99, a struct array can be initialized static struct {char* tag; char* msg;} help_info[] = {[0]={"tag0","msg0"}, [1]={"tag1", "msg1"}}; This is not valid in C++. What is a possible alternative to replicate this behavior for an iterable object…
TVB22
  • 47
  • 3
1
vote
1 answer

Aggregate / designated initialization of c++ struct: Refer directly to another field

When using aggregate / designated initialization of a struct it is possible to refer to another field like this: #include int main() { struct { int a; int b; } s = { .a = 3, .b = s.a + 1, }; return…
1
vote
2 answers

Is it possible to leave fields unitialized using designated intiailizers?

Consider the following struct and function to create the struct: #define MAX_ELEMS 1000 struct stuff { double magic; bool is_valid[MAX_ELEMS]; double values[MAX_ELEMS]; }; struct stuff make_stuff(double magic) { return (struct stuff){ …
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
1
vote
2 answers

Can I force a user to provide a value for a member when using designated initializers?

I have created an options struct, intended to be used via designated initializer: struct FooOptions { bool enableReticulation; }; void Foo(FooOptions&& opts); Foo(FooOptions{.enableReticulation = true}); Unfortunately, because bool has a…
Drew
  • 12,578
  • 11
  • 58
  • 98
1
vote
1 answer

Embedded C++ static initialization of struct arrays

While migrating to C++ I require a certain function that seems to have been deprecated. sorry, unimplemented: non-trivial designated initializers not supported What is the correct way to implement the following data storage system in C++ for memory…
Jeroen3
  • 919
  • 5
  • 20
1
vote
0 answers

Can I initialize a Codable Model from a different model in a Swift extension?

I have a codable model public final class MyCodableModel: Codable { public let id: Int } I also have another model that happens to have the same variables inside it. public final class MyOtherModel { public let id: Int } Now, I want to…
teradyl
  • 2,584
  • 1
  • 25
  • 34
1
vote
1 answer

Designated initializer warnings when calling another init from initWithCoder:

I am working on an inherited codebase and trying to resolve the following warnings: Designated initializer should only invoke a designated initializer on 'super' Designated initializer missing a 'super' call to a designated initializer of the super…
jcad
  • 63
  • 9
1 2 3
8 9