gcc-extensions

Why doesn't GCC's ternary extension support assignment?

不打扰是莪最后的温柔 提交于 2019-12-22 07:39:30
问题 GCC has an awesome ternary expression extension to C which allows us to create a statement like this: int x = some_var ?: 10; // expands to some_var ? some_var : 10 Which is really nice, and while it's not particularly intuitive, it does work. Most binary operators in the C language have an additional operator associated with them, which allows for assignment: x = x + 2; // instead, we can say x += 2; Since this is the case, and the norm for most binary C operators ( + , - , * , / , % , | , &

What is “…” in switch-case in C code

情到浓时终转凉″ 提交于 2019-12-17 17:59:46
问题 Here is a piece of code in /usr/src/linux-3.10.10-1-ARCH/include/linux/printk.h : static inline int printk_get_level(const char *buffer) { if (buffer[0] == KERN_SOH_ASCII && buffer[1]) { switch (buffer[1]) { case '0' ... '7': case 'd': /* KERN_DEFAULT */ return buffer[1]; } } } Is it a kind of operator? Why does "The C Programming Language" not mention it? 回答1: This is a gcc extension called case ranges, this is how it is explained in the document: You can specify a range of consecutive

Rewrite GCC cleanup macro with nested function for Clang?

不想你离开。 提交于 2019-12-04 23:56:11
问题 I'm trying to work through an issue on a third party library. The issue is the library uses GCC's nested functions buried in a macro, and Clang does not support nested functions and has no plans to do so (cf., Clang Bug 6378 - error: illegal storage class on function). Here's the macro that's the pain point for me and Clang: #define RAII_VAR(vartype, varname, initval, dtor) \ /* Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774 */ \ auto void _dtor_ ## varname (vartype

Rewrite GCC cleanup macro with nested function for Clang?

自作多情 提交于 2019-12-03 15:54:12
I'm trying to work through an issue on a third party library. The issue is the library uses GCC's nested functions buried in a macro, and Clang does not support nested functions and has no plans to do so (cf., Clang Bug 6378 - error: illegal storage class on function ). Here's the macro that's the pain point for me and Clang: #define RAII_VAR(vartype, varname, initval, dtor) \ /* Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774 */ \ auto void _dtor_ ## varname (vartype * v); \ void _dtor_ ## varname (vartype * v) { dtor(*v); } \ vartype varname __attribute__((cleanup(

How to pass a VLA to a function template?

戏子无情 提交于 2019-11-28 14:24:02
I have the following code which could not be complied. using namespace std; void f(int); template<typename T1, size_t N> void array_ini_1d(T1 (&x)[N]) { for (int i = 0; i < N; i++) { x[i] = 0; } } What is the proper way to pass the array if the main is something like below. int main() { int a; cin >> a; int n = a / 4; f(n); return 0; } void f(int n) { int arr[n]; array_ini_1d(arr); } error: no matching function to call to array_ini_1d.............. I don't think the compiler can deduce the size of a variable-length array in a template. Also, don't forget to forward declare f before you use it.

What's this C++ syntax that puts a brace-surrounded block where an expression is expected?

╄→гoц情女王★ 提交于 2019-11-27 18:39:02
I came across this weird C++ program. #include <iostream> using namespace std; int main() { int a = ({int x; cin >> x; x;}); cout << a; } Can anyone explain what is going on? What is this construct called? It assigns user input value to a and prints it out. it is done by using a Statement Expression . Statement Expressions are gnu gcc compiler extension are not supported by the C/C++ standards. Hence any code which uses statement expression is non standard conforming and non portable. The IBM IBM XL C/C++ v7.0 also support Statement Expressions & it's doccumentation explains them aptly:

How to pass a VLA to a function template?

雨燕双飞 提交于 2019-11-27 08:25:03
问题 I have the following code which could not be complied. using namespace std; void f(int); template<typename T1, size_t N> void array_ini_1d(T1 (&x)[N]) { for (int i = 0; i < N; i++) { x[i] = 0; } } What is the proper way to pass the array if the main is something like below. int main() { int a; cin >> a; int n = a / 4; f(n); return 0; } void f(int n) { int arr[n]; array_ini_1d(arr); } error: no matching function to call to array_ini_1d.............. 回答1: I don't think the compiler can deduce

What's this C++ syntax that puts a brace-surrounded block where an expression is expected?

江枫思渺然 提交于 2019-11-26 22:42:32
问题 I came across this weird C++ program. #include <iostream> using namespace std; int main() { int a = ({int x; cin >> x; x;}); cout << a; } Can anyone explain what is going on? What is this construct called? 回答1: It assigns user input value to a and prints it out. it is done by using a Statement Expression . Statement Expressions are gnu gcc compiler extension are not supported by the C/C++ standards. Hence any code which uses statement expression is non standard conforming and non portable.

Are variable length arrays there in c++?

和自甴很熟 提交于 2019-11-26 18:00:47
I had always thought that variable length arrays were not allowed in c++(Refer : Why aren't variable-length arrays part of the C++ standard? ) .But than why does this code compile and work? #include <iostream> using namespace std; int main () { int n; cin >> n; int a[n]; for (int i=0; i<n; i++) { a[i] = i; } for (int i=0; i<n; i++) { cout << a[i] << endl; } } David Heffernan The current C++ standard does not require that compilers VLAs. However, compiler vendors are permitted to support VLAs as an extension. It was originally proposed that VLAs would appear in C++14, however the proposal did

Are variable length arrays there in c++?

一个人想着一个人 提交于 2019-11-26 08:54:21
问题 I had always thought that variable length arrays were not allowed in c++(Refer :Why aren't variable-length arrays part of the C++ standard?) .But than why does this code compile and work? #include <iostream> using namespace std; int main () { int n; cin >> n; int a[n]; for (int i=0; i<n; i++) { a[i] = i; } for (int i=0; i<n; i++) { cout << a[i] << endl; } } 回答1: The current C++ standard does not require that compilers VLAs. However, compiler vendors are permitted to support VLAs as an