小编Yak*_*ont的帖子

std::set<std::future> 不可能存在吗

我正在使用 C++11 来做一些线程程序。
现在我遇到这样的情况:

我有一个std::set<std::future<std::string>> results存储线程的一些结果,当然所有这些线程都会返回一个字符串。

但是,当我尝试获取字符串时,出现以下错误:

将 xxx 作为 xxx 的“this”参数传递会丢弃限定符

根据此链接,我认为这是因为我试图调用属于 元素的非常量函数set。换句话说, the 的元素setstd::future<std::string>并且std::future<std::string>::get()是非常量。这就是为什么我收到这样的错误。

如果我是对的,这是否意味着我永远不能声明 astd::set<std::future>因为它get总是不可用?

这是我的代码:

set<future<string>> results;
results.insert(...); // insert some future
for(auto it = results.begin(); it != results.end();)
{
    if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
    {
        string tmp = it->get();  // ERROR!!!
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading future set c++11

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

为什么移动ctor比复制ctor慢?

我有以下代码来测试复制ctor并移动std::string类的ctor,结果让我感到惊讶,移动ctor 比复制ctor慢约1.4倍.

据我所知,移动构造不需要分配内存,因为在这种std::string情况下,移动构造对象中可能有一个内部指针直接设置为移动对象的内部指针,它应该比为缓冲区分配内存更快然后在复制构造时复制对象中的内容.

这是代码:

#include <string>
#include <iostream>

void CopyContruct(const std::string &s) {
  auto copy = std::string(s);
}

void MoveContruct(std::string &&s) {
  auto copy = std::move(s);
  //auto copy = std::string(std::move(s));
}

int main(int argc, const char *argv[]) {
  for (int i = 0; i < 50000000; ++i) {
    CopyContruct("hello world");
    //MoveContruct("hello world");
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

从这两个函数的汇编中,我可以看到,因为MoveConstruct有一个std::remove_reference类模板的实例化,我认为这应该是罪魁祸首,但我不熟悉汇编,任何人都可以详细说明吗?

以下代码在https://godbolt.org/上使用x86-64 gcc7.2进行了反编译:

CopyContruct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
  push rbp
  mov …
Run Code Online (Sandbox Code Playgroud)

c++ performance move copy-constructor c++11

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

封装任务管理器的成员函数指针的好方法

我想在模板类中存储成员函数指针和相关的对象指针task.然后我想创建一个数组并在scheduler类中调用这些成员函数.

但是,如果我将这些tasks 保存在数组中,则它们都需要具有相同的模板类型.这个障碍使得保存任务类中的对象指针毫无用处.

现在,我想知道是否有一种聪明的方法来保护相关调度程序类中的这类对的数组.如果我使用a std::tuple,我不能在运行时通过容器进行迭代,或者至少不能以非常易读的方式进行迭代.有没有解决方案,我不需要调度程序类的模板?

template <class Obj>
struct task {    
    typedef void (Obj::*task_fn_t)();
    Obj*        obj_ptr;  // object referring
    task_fn_t   function; // member function
};

template <class Obj>
class scheduler {
    task <Obj> *_tasks; // problem :(
};
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

无法移动std :: any

以下代码

using vptr = std::vector<std::unique_ptr<int>>;
auto m = std::unordered_map<int, std::any>{};
m.try_emplace(0, move(vptr{}));
Run Code Online (Sandbox Code Playgroud)

无法编译,抱怨使用的已删除副本构造函数unique_ptr。在模板参数中替换std::any为之后,vptr此代码将编译,因此问题显然与any

如何强制std::any移动而不是复制?

c++ move move-semantics c++17 stdany

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

适用于iPhone的FTP直播

我想准备一个iPhone应用程序.现场直播将用于移动相机(假设为便携式网络摄像头),我需要使用FTP将该实时视频流式传输到iPhone(假设我们在该区域没有互联网).

任何形式的帮助表示赞赏.

c++ iphone objective-c ios c++11

-3
推荐指数
1
解决办法
397
查看次数

C++ 11是否引入了任何新的方法来打破嵌套的for循环?

考虑的情况下进行的其他内部

int f( ... )
{
  for (int i = start_a; i < end_a; i++)
  {
    for (int j = start_b; j < end_b; j++)
    {
      // make some computation
      if( i_must_exit == true)
      {
        // exit from all for
      }
    }
  }

  // I want arrive here
}
Run Code Online (Sandbox Code Playgroud)

我们想要摆脱两个for循环.在没有分解内部函数,抛出异常等的情况下,这在C++ 03中并不容易.我想知道C++ 11是否引入了一种机制来执行此操作.

c++ idioms c++11

-3
推荐指数
1
解决办法
623
查看次数