小编Kis*_*Yoo的帖子

C++ 11:使用std :: move()的'decltype类实例声明'不会调用'move constructor'.为什么?

我最近开始使用c ++,我选择学习c ++ 11的功能.但是c ++代码的运行方式有时并不那么明显.

下面是我的代码.在那部分,decltype(std::move(sample)) sample2 = std::move(sample);我不知道为什么这一行不会调用移动构造函数.你能解释一下原因吗?

#include <iostream>

class AAA
{
   public:
      AAA() { std::cout << "default constructor" << std::endl; }
      AAA(const AAA& cs) { std::cout << "copy constructor" << std::endl; }
      AAA(AAA&& cs) { std::cout << "move constructor" << std::endl; }
      ~AAA() { std::cout << "destructor" << std::endl; }
};

int main(int argc, char* args[])
{
    AAA sample;
// ((right here))
    decltype(std::move(sample)) sample2 = std::move(sample); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它是在[ubuntu 16.04 …

c++ decltype move-semantics c++11 stdmove

5
推荐指数
2
解决办法
149
查看次数

任何人都可以解释((a == 1 && a == 2 && a == 3)== true)?

根据输出信息,任何人都可以解释下面的代码?

怎么可能都是(a==1 && a==2 && a==3)真的?

#include <iostream>
#include <thread>

int a = 0;

int main()
{
  std::thread runThread([]() { while (true) { a = 1; a = 2; a = 3; }});
  while (true)
  {
    if (a == 1 && a == 2 && a == 3)
    {
      std::cout << "Hell World!" << std::endl;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Hell World!
Hell World!
Hell World!
Hell World!
Hell World!
Hell World!
Hell World!
Hell World!
Hell World!
Hell …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

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

与 C 代码相比,为什么 C++ 代码不需要“#define _POSIX_C_SOURCE 200809L”?

当我使用“POSIX 间隔计时器”或进行信号处理时,我必须插入

#define _POSIX_C_SOURCE 200809L
Run Code Online (Sandbox Code Playgroud)

在我的任何文件中的第 1 行。但我发现只有 C 代码需要它,而不是 C++ 代码。

在这个问题上g++gcc编译器与编译器的工作方式有何不同?


下面是我的系统环境(gcc:相同版本)

 user@~ $ g++ --version
 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
 Copyright (C) 2015 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.
 There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

我构建了在 CMakeLists.txt 中添加这一行的项目

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++14 -Wall")
Run Code Online (Sandbox Code Playgroud)

c c++ preprocessor

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

无法在 std::set&lt;&gt; 中重置 std::shared_ptr 对象......为什么?

在下面的 C++ 调用 reset() 列表、向量、映射中,既没有错误也没有警告。

但是,当我尝试在 set 中执行此操作时,出现错误。

错误消息是 [ 没有匹配的成员函数调用 'reset' ]

为什么会这样???有人可以与社区分享您的知识吗?

std::shared_ptr<int> sp;
sp.reset(new int(11));
sp.reset();

std::map<int, std::shared_ptr<int>> my_map;
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
  it->second.reset();
  (*it).second.reset();
}

std::list<std::shared_ptr<int>> my_list;
for (auto& x : my_list) {
  x.reset();
}

std::vector<std::shared_ptr<int>> my_vec;
for (auto it = my_vec.begin(); it != my_vec.end(); ++it) {
it->reset();
  (*it).reset();
}

std::set<std::shared_ptr<int>> my_set;
for (auto& x : my_set) {
  x.reset();      // ERROR!!!
}

for (auto it = my_set.begin(); it != my_set.end(); …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers stdset c++11

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