case 语句中的省略号是标准 C/C++
我正在浏览 Linux 内核中的一些代码,遇到了类似 case '0' ... '9':
的语句
。为了尝试这个,我创建了下面的测试程序。
#include <iostream>
int main()
{
const int k = 15;
switch (k)
{
case 0 ... 10:
std::cout << "k is less than 10" << std::endl;
break;
case 11 ... 100:
std::cout << "k is between 11 and 100" << std::endl;
break;
default:
std::cout << "k greater than 100" << std::endl;
break;
}
}
上面的程序确实可以编译,尽管我以前从未在 case 语句构造中遇到过省略号。这是标准的 C 和 C++ 还是 GNU 对该语言的特定扩展?
I was browsing some code in the linux kernel and I came across the statements like case '0' ... '9':
To try this out I created the test program below.
#include <iostream>
int main()
{
const int k = 15;
switch (k)
{
case 0 ... 10:
std::cout << "k is less than 10" << std::endl;
break;
case 11 ... 100:
std::cout << "k is between 11 and 100" << std::endl;
break;
default:
std::cout << "k greater than 100" << std::endl;
break;
}
}
The program above does compile although I have never come across the elipses in case statement construct before. Is this standard C and C++ or is this a GNU specific extension to the language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 GNU C 编译器的 case range 扩展,它不是标准 C 或 C++。
That is the case range extension of the GNU C compiler, it is not standard C or C++.
那是一个扩展。使用
-pedantic
编译程序会给出:clang
给出更好的警告:That's an extension. Compiling your program with
-pedantic
gives:clang
gives even better warnings:这是 GCC 对 C 的扩展,在 这个答案中提到 基本上是一个重复的问题,并确认在 GCC 文档中。
This is a GCC extension to C, mentioned in this answer to what is basically a duplicate question, and confirmed in the GCC documentation.