标签: gcc-extensions

为什么这个复合语句作为由大括号和括号括起来的语句序列似乎不是有效的语句表达式

为什么这个复合语句作为由大括号括起来(在 GNU C++ 中)和括号内的语句序列似乎不是有效的 Statement 表达式。

// my second program in C++
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World! ";
  ({cout << "I'm a C++ program";}); 

}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

 In function 'int main()':
8:32: error: use of deleted function 'std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)'
In file included from /usr/include/c++/4.9/iostream:39:0,
                 from 2:
/usr/include/c++/4.9/ostream:58:11: note: 'std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)' is implicitly deleted because the default definition would be ill-formed:
     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
           ^
/usr/include/c++/4.9/ostream:58:11: error: use of deleted …
Run Code Online (Sandbox Code Playgroud)

c++ expression braces gcc-extensions

4
推荐指数
1
解决办法
190
查看次数

GCC类型的扩展

我不明白为什么会这样:

/* gcc range extension */
__extension__ static int fn(int n)
{
    switch (n) {
        case 0: return 0;
        case 1 ... 1000: return 1;
        default: return -1;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这不是:

/* gcc typeof extension */
__extension__ static void fn(int n)
{
    typeof(n) a = n;

    printf("%d\n", a);
}
Run Code Online (Sandbox Code Playgroud)

gcc返回:

demo.c:14: warning: implicit declaration of function ‘typeof’
demo.c:14: warning: nested extern declaration of ‘typeof’
demo.c:14: error: expected ‘;’ before ‘a’
demo.c:16: error: ‘a’ undeclared (first use in this function)
demo.c:16: …
Run Code Online (Sandbox Code Playgroud)

c gcc typeof gcc-extensions

3
推荐指数
1
解决办法
6653
查看次数

如何在 C 中实现 Go 的 defer() 以便允许声明变量?

在这个现代 C视频中,有一个技巧可以推迟代码的执行,直到块/作用域退出。它的使用方法如下:

\n
int main()\n{\n    int foo=0, bar;\n    const char *etc = "Some code before defer";\n\n    defer(profile_begin(), profile_end())\n    {\n        /* Some code, which will be automatically \n         * preceded by call to profile_begin() and\n         * followed by run of profile_end().*/\n         foo++;\n         bar = 1;\n    }\n\n    etc = "Some code after defer";\n    foo = bar + 1;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

视频中的实现:

\n
#define macro_var_line(name) concat(name, __LINE__)\n#define defer(start,end) for( \\\n        int macro_var_line(done) = (start,0); \\\n        !macro_var_line(done); \\\n        (macro_var_line(done) += 1), end)\n
Run Code Online (Sandbox Code Playgroud)\n …

c gcc c-preprocessor c11 gcc-extensions

3
推荐指数
1
解决办法
2382
查看次数

将__builtin_expect委托给内联函数是否安全?

我正在研究一些定义的C ++代码

#define LIKELY(x)   (__builtin_expect((x), 1))
Run Code Online (Sandbox Code Playgroud)

我想知道-为什么不使用内联函数?即为什么不

template <typename T> inline T likely(T x) { return __builtin_expect((x), 1); }
Run Code Online (Sandbox Code Playgroud)

(或许

inline int likely(int x) { return __builtin_expect((x), 1); }
Run Code Online (Sandbox Code Playgroud)

因为x应该是某些条件检查的结果)

宏和函数应该基本相同,对吗?但是后来我想知道:也许是因为__builtin_expect...在内联辅助函数中工作的不同吗?

c++ built-in gcc-extensions

2
推荐指数
1
解决办法
450
查看次数

C/C++中的括号内的代码块是否合法,MSCL可以编译它吗?

我有以下代码:

int x;
x = ({ 1; 2; 3; });
printf("%d\n", x); // should be 3
Run Code Online (Sandbox Code Playgroud)

(如果你很好奇为什么我会编写那样令人厌恶的代码.答案是我不是.我正在编写一个输出C代码的生成器,并且这样的语句会使事情变得容易多了.)

该代码编译并在Apple LLVM版本7.0.2上运行(当然会对未使用的代码发出警告)但在MSCL 10.0和14.0中失败(错误C2059:语法错误:'{').

我的问题是:1)是否有这种代码的名称(-abuse)?2)在任何C/C++标准中是否合法?3)有没有办法让MSCL接受它?

c c++ gcc-extensions cl

2
推荐指数
1
解决办法
167
查看次数

为什么在 C++ 中没有定义 n 的 sizeof (array A[n]) 是固定的?

当我尝试找到 sizeof(A) 其中 A 的类型为 int 且大小为 'n' 时,n 是一个未定义的 int。我得到 496 的输出,当我给 n 一个值然后检查它时,sizeof(A) 给我的值与 496 相同。我知道 Array 是一种静态数据类型,因此无论“n”如何,它都会有内存谁能解释一下 496 的值是从哪里来的?

int main()
{
    int n;
    int A[n];
    
    cout<<sizeof(A)<<"\n";
    
    cin>>n;
    
    cout<<sizeof(A);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ sizeof variable-length-array gcc-extensions

2
推荐指数
1
解决办法
139
查看次数

gcc __attribute__ 放置之间的区别

在为函数使用 gcc __attribute__ 时,我注意到代码生成存在差异,具体取决于我放置属性的位置。在下面的示例中,我希望编译器不要优化我对 use() 的调用。

编译器:x86-64 gcc(trunk)

选项:-O3 -Wall

void  __attribute__((noinline, optimize("O0"))) use() {} 
int main () 
{
    use();
}
use:
        push    rbp
        mov     rbp, rsp
        nop
        pop     rbp
        ret
main:
        xor     eax, eax
        call    use
        xor     eax, eax
        ret
Run Code Online (Sandbox Code Playgroud)

但是,如果我更改属性的位置,则会生成不同的代码。

void   use() {} __attribute__((noinline, optimize("O0")))
int main () 
{
    use();
}
main:
        push    rbp
        mov     rbp, rsp
        mov     eax, 0
        pop     rbp
        ret
use:
        ret
Run Code Online (Sandbox Code Playgroud)

如果我不放任何属性,我会得到这个:

void   use() {} 
int main () 
{
    use();
}
use:
        ret …
Run Code Online (Sandbox Code Playgroud)

c gcc gcc-extensions

0
推荐指数
1
解决办法
369
查看次数

GCC - 修改函数返回后继续执行的位置

是否可以在GCC中做这样的事情?

void foo() {
    if (something()) returnSomewhereElse;
    else return;
}

void bar() {
    foo();
    return; // something failed, no point of continuing
    somewhereElse:
    // execution resumes here if something succeeds
    // ...
}
Run Code Online (Sandbox Code Playgroud)
  • 这可以通过C和GCC扩展以便携方式实现,而无需使用平台特定组件吗?
  • 堆栈状态在正常返回点和更改的返回点之间不会改变,那么是否可以重用恢复堆栈的代码并从常规返回中注册状态?
  • 考虑到函数可能会或可能不会内联,如果调用它必须改变返回地址,如果内联它只能改变代码路径而不是当前函数返回地址,因为这会破坏代码
  • 备用返回点不需要是标签,但我希望GCC的标签扩展地址在这种情况下可以派上用场

只是为了澄清意图 - 它是关于错误处理.这个例子是一个最小的例子,只是为了说明事情.我打算在更深层次的上下文中使用它,以便在发生错误时停止执行.我还假设状态没有改变,我可能错了,因为在两个返回点之间没有添加额外的局部变量,所以我希望编译器生成的代码在foo的返回时可以重用为此节省了使用longjmp,设置和传递跳转缓冲区的开销.

这个例子"确实有意义",因为它的目的是展示我想要实现的目标,而不是为什么以及如何在实际代码中有意义.

为什么你的想法更简单,只需从foo()返回一个值并让bar()返回或执行somewhereElse:conditionally?

它并不简单,你所建议的不适用于实践,只是在一个简单的例子的背景下,但它更好,因为:

1 - 它不涉及额外返回值

2 - 它不涉及额外检查值

3 - 它不涉及额外的跳跃

我可能错误地认为目标应该在这一点上清楚,并且在所有澄清和解释之后.我们的想法是从深度调用链中提供"转义代码路径",而无需任何额外开销.通过重用编译器生成的代码来恢复前一个调用帧的状态,并简单地修改函数返回后执行恢复的指令.成功跳过"转义代码路径",发生的第一个错误进入它.

if (failure) return; // right into the escape code path
else {
    doMagickHere(); // to skip the escape code path
    return; // skip over the …
Run Code Online (Sandbox Code Playgroud)

c gcc return memory-address gcc-extensions

-25
推荐指数
1
解决办法
1134
查看次数