小编K.K*_*zen的帖子

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

为什么这个复合语句作为由大括号括起来(在 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
查看次数

为什么可以在 unique_ptr 的向量中放置两次指针?

不应该std::unique_ptr防止出现这种错误的可能性吗?

#include <iostream>
#include <vector>
#include <memory>

struct B {
    int b;
};

int main()
{
    std::vector<std::unique_ptr<B>> v;  // unique_ptr can be stored in a container
    B* p = new B;
    v.emplace_back(p);
    std::cout << "p:" <<p <<"\n";
    std::cout << "v[0]:"<<v[0].get() << "\n";
    v.emplace_back(p);
    std::cout << "p:" <<p <<"\n";
    std::cout << "v[1]:"<<v[1].get() << "\n";

}
Run Code Online (Sandbox Code Playgroud)

检测到双重释放时的错误信息:

*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001094c20 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3da200b7e5]
...
======= Memory map: ========
00400000-00402000 r-xp 00000000 …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++17

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

标签 统计

c++ ×2

braces ×1

c++17 ×1

expression ×1

gcc-extensions ×1

unique-ptr ×1