Macro for static std::string object from literal

前端 未结 5 2163
萌比男神i
萌比男神i 2021-02-12 17:56

Suppose I need to call a function foo that takes a const std::string reference from a great number of places in my code:

int foo(const          


        
5条回答
  •  名媛妹妹
    2021-02-12 18:16

    This will work for simple strings - w/o whitespace:

    #define DECL_STR(s) const std::string str_##s (#s)
    

    Usage in header (parse once!):

    DECL_STR(Foo);
    DECL_STR(Bar);
    

    In code:

    func(str_Foo);
    func(str_Bar);
    

提交回复
热议问题