this post was submitted on 04 Aug 2023
6 points (100.0% liked)

C++

1791 readers
2 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
 

Recently I end up using structs everywhere as functions parameters to basically get named function parameters and better default arguments. Are there any downsides to this? So far the only annoying thing is to have to define those structs.

struct FunParams{
    int i = 5;
    float f = 3.14f;
    std::string s = "hello";
};

void Fun(const FunParams& params){}

int main(){
    Fun({.s = "hi there"});
}
you are viewing a single comment's thread
view the rest of the comments
[โ€“] NekkoDroid 3 points 1 year ago (2 children)

Well, you can't exactly have required parameters that way. At keast not to my knowledge

[โ€“] cschreib 1 points 1 year ago* (last edited 1 year ago)

I'm sure you can come up with some utility class required (templated with T, Lemmy won't let me) that isn't default constructible but can be implicitly constructed from a T, then use this instead of type T in the struct definition.

load more comments (1 replies)