c++ - 我怎样才能 Eloquent 地写 "if not greater than or equal to some value"?

标签 c++ c if-statement operators

<分区>

我正在阅读编程:使用 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/

相关文章:

c++ - 分配 vector

c++ - 在带有 mingw 的 Windows 上使用 openmp。找不到 -lpthread

c++ - 索引大型 txt 文件

c - C中的字符串问题

c - C语言中如何统计char数组中输入的字符数

c - 为什么我的代码没有进入else语句?

r - 根据R中数据框中第二列的值填充第三列

c++ - 复制构造函数不被继承

c - 什么是 C 中的指定初始化程序?

javascript - ES6如何使用if语句声明一个新变量? JavaScript