Named parameter string formatting in C++

后端 未结 6 1967
甜味超标
甜味超标 2021-02-07 08:55

I\'m wondering if there is a library like Boost Format, but which supports named parameters rather than positional ones. This is a common idiom in e.g. Python, where you have a

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 09:35

    The fmt library supports named arguments:

    print("You clicked {button} at {x},{y}.",
          arg("button", "b1"), arg("x", 50), arg("y", 30));
    

    And as a syntactic sugar you can even (ab)use user-defined literals to pass arguments:

    print("You clicked {button} at {x},{y}.",
          "button"_a="b1", "x"_a=50, "y"_a=30);
    

    For brevity the namespace fmt is omitted in the above examples.

    Disclaimer: I'm the author of this library.

提交回复
热议问题