6
submitted 1 year ago* (last edited 1 year ago) by Renderwahn@lemmy.world to c/cpp@programming.dev

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
[-] o11c@programming.dev 3 points 1 year ago

The problem is that C++ compilers still haven't fixed a trivial several-decades-old limitation: you still have to pass the named arguments in order.

They use the excuse of "what's the evaluation order", but ordinary constructors have the exact same problem and they deal with that fine.

[-] Renderwahn@lemmy.world 1 points 1 year ago

It's a bit annoying but why is it a problem? You still can skip arguments where you just want the default value. Compared to function arguments you also get defined evaluation order.

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

C++

1718 readers
38 users here now

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

Rules

founded 1 year ago
MODERATORS