0

I need to create a data structure that has a LOT of members. Example:

struct MyStruct {
    int varSomething;
    int helloNumber;
    int thisIsSomething;
    ...
    int varNumber50;//50th element
}

The problem with this struct is that when I want to instantiate it, I need to do the following:

my_vector[i] = MyStruct{10, 4, 90, ...}

as you can see, I need to put lots of values and I end up confusing which one is which. Ideally, I'd like to do something like this:

my_vector[i] = MyStruct{varSomething = 10, helloNumber = 4, thisIsSomething = 90, ...}

Since each variable has a name, I know what I'm doing, and can't confuse.

I know that I can do this:

MyStruct myStruct;
MyStruct.varSomething = 10;
myStruct.helloNumber = 4;
...
my_vector[i] = myStruct

but I want to create an anonymous struct, it's not nice to create a named struct just to put in the vector.

What should be a great way to solve this problem? Is there something similar to a struct that can achieve what I want??

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • related/dupe: https://stackoverflow.com/questions/18731707/why-does-c11-not-support-designated-initializer-lists-as-c99 – NathanOliver Aug 05 '19 at 17:55
  • 2
    You can use `std::map` as your member variable if the names are important but then you'll have to pay a small price in performance when you want to access a specific member from the map. Whether the performance penalty is acceptable depends entirely on your application – R Sahu Aug 05 '19 at 17:58
  • 6
    If you have a lot of named member variables, your design is almost certainly wrong. –  Aug 05 '19 at 17:58
  • It's best to not abuse a struct like this. Structs are not databases. Use something more appropriate. – Nikos C. Aug 05 '19 at 18:10

1 Answers1

1

With c++20 designated initializers you can do the following

struct MyStruct {
    int  a,b,c;
};

int main(){
    MyStruct my_value { 
        a : 2, 
        b : 3, 
        c : 95 
    };
}

In this case I would recommend against using this as it is a definite code smell. You need to rethink your design and maybe looking at design patterns like factory, builder or prototype to give you a better idea on how to create your objects.

See the following links for more information:
https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers

https://en.wikipedia.org/wiki/Creational_pattern

Alecto Irene Perez
  • 10,321
  • 23
  • 46
Bryan
  • 441
  • 2
  • 8
  • Are there any compilers (or development branches of compilers) that currently support this? Also, why the `name : value` syntax instead of the `.name = value` syntax`? – Alecto Irene Perez Aug 05 '19 at 19:34
  • Yes it is supported currently by gcc and msvc with partial support in clang https://en.cppreference.com/w/cpp/compiler_support Example of it compiling: https://godbolt.org/z/Ab7anQ – Bryan Aug 05 '19 at 19:41
  • Huh. I didn't realize both syntaxes were supported. – Alecto Irene Perez Aug 05 '19 at 19:43
  • The other syntax is not supported in C++ due to the C++ requirement for in order initialization. I believe this is the reason for the different syntax, to highlight that they operate differently. See the paper for more info: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0329r4.pdf – Bryan Aug 05 '19 at 19:46