gpt4 book ai didi

c++ - 从可调用参数创建的线程对象参数移动构建 C++11 线程

转载 作者:太空狗 更新时间:2023-10-29 20:33:07 25 4
gpt4 key购买 nike

在 C++11 初始化列表中,我试图从在别处创建并传递给构造函数的线程对象移动构造一个线程。我还尝试使用传入的可调用参数创建线程对象。我运气不好。

以下(无效)代码有望表明我正在尝试执行的操作:

a.cpp

#include <map>
#include <thread>

class Thing {
int i;
};

class Impl {
class Entry {
Thing thing;
std::thread thread;

public:
template<class F>
Entry(Thing& thing, F&& func)
: thing(thing)
, thread(func) {
}
};

class ThingMap {
std::map<Thing, Entry> map;

public:
template<class F>
void add(Thing& thing, F&& func) {
map.emplace({thing, {thing, func}});
}
};

ThingMap things;

void execute() {
}

public:
void activate(Thing& thing) {
things.add(thing, [this]{execute();});
}
};

这是我的 g++ 4.8.5 编译器的错误:

$ g++ -std=c++11 -c a.cpp
a.cpp: In instantiation of ‘void Impl::ThingMap::add(Thing&, F&&) [with F = Impl::activate(Thing&)::__lambda0]’:
a.cpp:38:45: required from here
a.cpp:27:13: error: no matching function for call to ‘std::map<Thing, Impl::Entry>::emplace(<brace-enclosed initializer list>)’
map.emplace({thing, {thing, func}});
^
a.cpp:27:13: note: candidate is:
In file included from /usr/include/c++/4.8.2/map:61:0,
from a.cpp:1:
/usr/include/c++/4.8.2/bits/stl_map.h:540:2: note: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::emplace(_Args&& ...) [with _Args = {}; _Key = Thing; _Tp = Impl::Entry; _Compare = std::less<Thing>; _Alloc = std::allocator<std::pair<const Thing, Impl::Entry> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const Thing, Impl::Entry> >]
emplace(_Args&&... __args)
^
/usr/include/c++/4.8.2/bits/stl_map.h:540:2: note: candidate expects 0 arguments, 1 provided
$

我们将不胜感激有关如何修复代码的任何和所有建议。请不要管这个结构,但是,它存在是有原因的。

最佳答案

std::map::emplace 是一个模板函数,接受任意对象来构造键/值对。您不能使用列表初始化,因为编译器将无法推断类型。

改为指定类型:

template<class F>
void add(Thing& thing, F&& func) {
map.emplace(thing, Entry{thing, std::forward<F>(func)});
}

关于c++ - 从可调用参数创建的线程对象参数移动构建 C++11 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com