【问题标题】:Why this compound statement as a sequence of statements enclosed by braces and inside parentheses does not appear to be a valid statement expression为什么这个复合语句作为由大括号和括号括起来的语句序列似乎不是有效的语句表达式
【发布时间】:2020-09-17 09:57:22
【问题描述】:

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

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

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

}

编译器输出:

 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 function 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)'
In file included from /usr/include/c++/4.9/ios:44:0,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 2:
/usr/include/c++/4.9/bits/basic_ios.h:66:11: note: 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' is implicitly deleted because the default definition would be ill-formed:
     class basic_ios : public ios_base
           ^
In file included from /usr/include/c++/4.9/ios:42:0,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 2:
/usr/include/c++/4.9/bits/ios_base.h:786:5: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
     ios_base(const ios_base&);
     ^
In file included from /usr/include/c++/4.9/ios:44:0,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 2:
/usr/include/c++/4.9/bits/basic_ios.h:66:11: error: within this context
     class basic_ios : public ios_base
           ^

我在What's this C++ syntax that puts a brace-surrounded block where an expression is expected?找到了一个关于“语句表达式”的好答案

【问题讨论】:

  • 旁白:我希望这不是实际上您的第二个 c++ 程序。在学习特定于编译器的扩展之前,您应该学习更多的语言。
  • @cigien:计算机科学专家通常可能会使用一种新语言并立即跳转到扩展。提出的问题是明智的,没有理由对提问者下结论。
  • 那条评论当然也可能是个玩笑……
  • @EricPostpischil 这很公平。我的意思是,如果您对语言有更多了解,可能更容易理解语言扩展正在做什么,但我认为无论如何尝试理解特定扩展并没有什么坏处。这绝对是一个有效且问得很好的问题。
  • @NateEldredge 好吧,我是认真的,但我想语气可能不那么带有判断力。以后我会尽量记住这一点。

标签: c++ expression braces gcc-extensions


【解决方案1】:

来自reference

复合语句中的最后一件事应该是一个表达式,后跟一个分号;此子表达式的值用作整个构造的值。

在G++中,语句表达式的结果值会经过数组和函数指针的衰减,并按值返回给封闭的表达式。

这意味着在表达式中:

({cout << "I'm a C++ program";}); 

对象std::cout返回。这会调用 std::basic_ostream 的复制构造函数,该构造函数已被删除,因此出现错误。

【讨论】:

  • 所以,({void(cout&lt;&lt;"I'm a C++ program");});
  • @jthill 或者只是({cout &lt;&lt; "I'm a C++ program"; ; });。但我认为这主要是一个学术问题,OP 实际上并不是在寻找解决方案。
  • 问题出现是因为我在 GitHub 上阅读了 gnu prolog 的 c++ 包装器,并在其中找到了“语句表达式”。这是我过去很少使用的功能,我开始对其进行一些测试。巧合的是,我遇到了一个意外的错误消息。解释有点超出我的能力,但基本上我明白并非一切都非常简单。对我来说,坚持更简单的结构就足够了。无论如何都要遵循的好风格。非常感谢!
  • 别担心,乐于助人:)
猜你喜欢
  • 2022-01-20
  • 2016-12-04
  • 2019-07-11
  • 2013-02-23
  • 1970-01-01
  • 2012-05-03
  • 2013-04-23
  • 1970-01-01
相关资源
最近更新 更多