Are Elipses, if approved, standard C / C ++

I was looking at some code in the linux kernel, and I came across expressions like case '0' ... '9':

To try this, I created a 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 described above compiles, although I had never encountered elipses in the case constructor before. Is this standard C and C ++ or is it a GNU extension for the language?

+13
c ++ c
May 7, '11 at 23:28
source share
3 answers

This is a case case extension of the GNU C compiler, it is not a C or C ++ standard.

+22
May 7 '11 at 23:31
source share

This is an extension. Compiling your program with -pedantic yields:

 example.cpp: In function 'int main()': example.cpp:9: error: range expressions in switch statements are non-standard example.cpp:12: error: range expressions in switch statements are non-standard 

clang gives even better warnings:

 example.cpp:9:12: warning: use of GNU case range extension [-Wgnu] case 0 ... 10: ^ example.cpp:12:13: warning: use of GNU case range extension [-Wgnu] case 11 ... 100: ^ 
+6
May 7 '11 at 23:31
source share

This is the GCC extension for C, mentioned in this answer , to what is basically a duplicate question, and is confirmed in the GCC documentation .

+1
May 7 '11 at 23:34
source share



All Articles