Macro for static std::string object from literal

前端 未结 5 2273
萌比男神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:23

    If that function foo does not make a copy of the string then its interface is sub-optimal. It is better to change it to accept char const* or string_view, so that the caller is not required to construct std::string.

    Or add overloads:

    void foo(char const* str, size_t str_len); // Does real work.
    
    inline void foo(std::string const& s) { foo(s.data(), s.size()); }
    inline void foo(char const* s) { foo(s, strlen(s)); }
    

提交回复
热议问题