以这种方式比较字符串是一种好风格吗?
is it a good style to compare strings this way?
我想在 .cpp
文件中使用:
namespace
{
bool operator==(char const* const a, char const* const b) noexcept
{
return !::std::strcmp(a, b);
}
}
这个款式好看吗?
编辑:
我认为完成同样事情的有品味的 c++1z 方法是使用新的 std::string_view
class 进行比较。
你不能 overload operator 不接受 class
或 enum
作为它的操作数,这意味着你不能改变他们使用内置类型的行为.
When an operator appears in an expression, and at least one of its operands has a class type or an enumeration type, then overload resolution is used to determine the user-defined function to be called among all the functions whose signatures match the following:
我建议你在必要时使用 std::string intead of char*
, which provide operator==. Then you can avoid using of std::strcmp()
, and such kind of c-style string functions at all. If you do need a c-style string, you can use std::basic_string::c_str() 将其转换回来。
我想在 .cpp
文件中使用:
namespace
{
bool operator==(char const* const a, char const* const b) noexcept
{
return !::std::strcmp(a, b);
}
}
这个款式好看吗?
编辑:
我认为完成同样事情的有品味的 c++1z 方法是使用新的 std::string_view
class 进行比较。
你不能 overload operator 不接受 class
或 enum
作为它的操作数,这意味着你不能改变他们使用内置类型的行为.
When an operator appears in an expression, and at least one of its operands has a class type or an enumeration type, then overload resolution is used to determine the user-defined function to be called among all the functions whose signatures match the following:
我建议你在必要时使用 std::string intead of char*
, which provide operator==. Then you can avoid using of std::strcmp()
, and such kind of c-style string functions at all. If you do need a c-style string, you can use std::basic_string::c_str() 将其转换回来。