Should I compare a std::string to “string” or “string”s?

前端 未结 3 1269
忘了有多久
忘了有多久 2021-01-31 07:41

Consider this code snippet:

bool foo(const std::string& s) {
    return s == \"hello\"; // comparing against a const char* literal
}

bool bar(const std::str         


        
3条回答
  •  轮回少年
    2021-01-31 08:48

    No, compare() does not require construction of a std::string for const char* operands.

    You're using overload #4 here.

    The comparison to string literal is the "free" version you're looking for. Instantiating a std::string here is completely unnecessary.

提交回复
热议问题