【发布时间】: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