编译器支持GNU语句表达式

时间:2011-06-22 12:42:53

标签: c visual-c++ compiler-construction c99

哪些现代编译器支持Gnu语句表达式(C和C ++语言)。我应该使用哪个版本的语句表达式?

语句表达式与({ code; code; retval })

类似
int b=56;
int c= ({int a; a=sin(b); a})

我已经知道一些这样的编译器:

这个编译器似乎不支持这个(我不确定):

  • MS Visual C ++

PS。列出了here的一些C / C ++编译器,但我只对成熟编译器感兴趣,这些编译器被广泛使用(例如,不是tcc或turbo c)

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

英特尔C ++编译器不支持语句表达式,即使是我所知的最新版本,版本13.0。

答案 2 :(得分:0)

正如我之前的回答评论所述,英特尔编译器确实支持语句表达式。但是,在C ++中,英特尔对GNU扩展的仿真并不完整。以下代码取自CGAL-4.0(http://www.cgal.org/):

#include <cassert>

struct A {
  int* p;

  A(int i) : p(new int(i)) {}
  ~A() { delete p; }
  int value() const { return *p;}
};

int main()
{
  int i = __extension__ ({ int j = 2; j+j; });
  assert(i == 4);

  // The Intel Compiler complains with the following error:
  // "error: destructible entities are not allowed inside of a statement
  // expression"
  // See http://software.intel.com/en-us/articles/cdiag1487/
  i = __extension__ ({ A a(2); A b(3); a.value() + b.value(); });

  assert(i == 5);
  return 0;
}

代码中的注释甚至会给出英特尔编译器返回的错误,并使用版本11,12或13进行测试。

http://software.intel.com/en-us/articles/cdiag1487/

相关问题