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"});
}
  • NekkoDroid
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

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

    • Renderwahn@lemmy.worldOP
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      It is possible if they are added as regular function parameters before the struct parameter but somehow I find that a bit ugly…

    • cschreib
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      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.