<分区>
我正在阅读编程:使用 C++ 的原理和实践,Bjarne 建议避免过度使用括号。是否有一种 Eloquent 方式来构造以下 if 语句?这是我的代码:
bool is_valid(string s)
{
if(!(s.size()>=8)) throw invalid_argument("Username must be 8 char or more");
return true;
}
<分区>
我正在阅读编程:使用 C++ 的原理和实践,Bjarne 建议避免过度使用括号。是否有一种 Eloquent 方式来构造以下 if 语句?这是我的代码:
bool is_valid(string s)
{
if(!(s.size()>=8)) throw invalid_argument("Username must be 8 char or more");
return true;
}
最佳答案
功能设计不好。它总是返回 true
并且不清楚它抛出异常的原因。
我应该是这样的
bool is_valid( const string &s )
{
return s.size() >= 8;
}
或
bool is_valid( const string &s )
{
const std::string::size_type acceptable_size = 8;
return s.size() >= acceptable_size;
}
关于c++ - 我怎样才能 Eloquent 地写 "if not greater than or equal to some value"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438829/