无法对可变模板参数使用std::bind
Unable to use std::bind with variadic template parameters
我正在为std::thread
实现包装器,这将允许我在线程完成执行后检索任意返回值。当我使用C++11
时,我使用的是不支持完全支持原子整型的较旧的ARM
体系结构,这意味着我不能使用std::future
, std::promise
, std::packaged_task
和许多stl线程功能(我至少得到std::thread
)。我正在测试gcc 4.8.4
。
在我的实现工作时,我遇到了这个错误,这使得我无法用lambda捕获可变的模板参数。遗憾的是,目前我无法将编译器升级到4.9。
我试图实现使用std::bind
的解决方案,但我遇到了相当多的问题。我不确定这些是编译器错误还是我的实现错误。来源:
#include <iostream>
#include <memory>
#include <thread>
#include <unistd.h>
#include <pthread.h>
class ConcurrentTaskBase
{
public:
ConcurrentTaskBase(int priority, const std::function<void()>& runTask)
: m_thread(),
m_active(true)
{
auto wrap = [this](int priority, const std::function<void()>& runTask)
{
//Unrelated pthread stuff that I commented out
// sched_param param{priority};
//
// int err = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
// if (err)
// cout << "failed to set new priority: " << err << endl;
runTask();
};
m_thread = std::thread(wrap, priority, runTask);
}
virtual ~ConcurrentTaskBase(void)
{
waitForCompletion();
}
void waitForCompletion(void)
{
if (m_active)
{
m_thread.join();
m_active = false;
}
}
private:
std::thread m_thread;
bool m_active;
};
template<class R, class... ArgTypes>
class ConcurrentTask;
template<class R, class... ArgTypes>
class ConcurrentTask<R(ArgTypes...)> : public ConcurrentTaskBase
{
public:
ConcurrentTask(int priority, const std::function<R(ArgTypes...)>& task, ArgTypes&&... args)
: ConcurrentTaskBase(priority, bindTask(task, std::forward<ArgTypes>(args)...))
{}
std::shared_ptr<R> getReturn(void) noexcept
{
waitForCompletion();
return m_storage;
};
private:
static std::function<void(void)> bindTask(const std::function<R(ArgTypes...)>& task, ArgTypes&&... args)
{
auto action = [task](ArgTypes&&... args) -> void
{
//Eventually m_storage = std::make_shared<R>(task(std::forward<ArgTypes>(args)...)); after bugs are fixed
task(std::forward<ArgTypes>(args)...);
return;
};
std::function<void(void)> bound = std::bind(action, std::forward<ArgTypes>(args)...);
return bound;
};
std::shared_ptr<R> m_storage;
};
int testFunction(int val)
{
std::cout << "Was given " << val << std::endl;
return val + 10;
}
int main()
{
ConcurrentTask<int(int)> task(20, testFunction, 5);
// shared_ptr<int> received = task.getReturn();
// testFunction(*received);
return 0;
}
下面是我的编译器输出:
16:31:00 **** Incremental Build of configuration Debug for project TestLinuxMint ****
make all
Building file: ../src/TestLinuxMint.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -pthread -c -fmessage-length=0 -MMD -MP -MF"src/TestLinuxMint.d" -MT"src/TestLinuxMint.o" -o "src/TestLinuxMint.o" "../src/TestLinuxMint.cpp"
../src/TestLinuxMint.cpp: In instantiation of ‘static std::function<void()> ConcurrentTask<R(ArgTypes ...)>::bindTask(const std::function<_Res(_ArgTypes ...)>&, ArgTypes&& ...) [with R = int; ArgTypes = {int}]’:
../src/TestLinuxMint.cpp:58:84: required from ‘ConcurrentTask<R(ArgTypes ...)>::ConcurrentTask(int, const std::function<_Res(_ArgTypes ...)>&, ArgTypes&& ...) [with R = int; ArgTypes = {int}]’
../src/TestLinuxMint.cpp:91:53: required from here
../src/TestLinuxMint.cpp:76:90: error: conversion from ‘std::_Bind_helper<false, ConcurrentTask<R(ArgTypes ...)>::bindTask(const std::function<_Res(_ArgTypes ...)>&, ArgTypes&& ...) [with R = int; ArgTypes = {int}]::__lambda1&, int>::type {aka std::_Bind<ConcurrentTask<R(ArgTypes ...)>::bindTask(const std::function<_Res(_ArgTypes ...)>&, ArgTypes&& ...) [with R = int; ArgTypes = {int}]::__lambda1(int)>}’ to non-scalar type ‘std::function<void()>’ requested
std::function<void(void)> bound = std::bind(action, std::forward<ArgTypes>(args)...);
^
make: *** [src/TestLinuxMint.o] Error 1
16:31:01 Build Finished (took 319ms)
这个问题似乎是在line 76
上,从std::bind(*) to std::function<void(void)>
转换失败。这个代码肯定还在开发中,但我需要通过这个问题向前推进。我看过很多关于SO的其他帖子,但是他们似乎都可以在可变模板参数上使用std::bind而不会出现问题。
这是我想出的最终解决方案(关于这个问题),感谢kzraq和这篇文章。
源:#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include <thread>
#include <type_traits>
#include <typeinfo>
#include <tuple>
#include <memory>
//------------------------------------------------------------------------------------------------------------
template <std::size_t... Ints>
struct idx_sequence
{
using type = idx_sequence;
using value_type = std::size_t;
static constexpr std::size_t size() noexcept { return sizeof...(Ints); }
};
//------------------------------------------------------------------------------------------------------------
template <class Sequence1, class Sequence2>
struct _merge_and_renumber;
template <std::size_t... I1, std::size_t... I2>
struct _merge_and_renumber<idx_sequence<I1...>, idx_sequence<I2...> >
: idx_sequence<I1..., (sizeof...(I1)+I2)...>
{
};
//------------------------------------------------------------------------------------------------------------
template <std::size_t N>
struct make_idx_sequence : _merge_and_renumber<make_idx_sequence<N/2>, make_idx_sequence<N - N/2> >
{
};
template<> struct make_idx_sequence<0> : idx_sequence<> { };
template<> struct make_idx_sequence<1> : idx_sequence<0> { };
//------------------------------------------------------------------------------------------------------------
template<typename Func, typename Tuple, std::size_t... Ints>
auto applyImpl(Func&& f, Tuple&& params, idx_sequence<Ints...>)
-> decltype(f(std::get<Ints>(std::forward<Tuple>(params))...))
{
return f(std::get<Ints>(std::forward<Tuple>(params))...);
};
template<typename Func, typename Tuple>
auto apply(Func&& f, Tuple&& params)
-> decltype(applyImpl(std::forward<Func>(f),
std::forward<Tuple>(params),
make_idx_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{}))
{
return applyImpl(std::forward<Func>(f),
std::forward<Tuple>(params),
make_idx_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{});
};
class ConcurrentTaskBase
{
public:
ConcurrentTaskBase(int priority, const std::function<void()>& task)
: m_thread(),
m_active(true)
{
auto wrap = [this](int priority, const std::function<void()>& task)
{
//Unrelated pthread stuff that I commented out
sched_param param{priority};
int err = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
if (err)
std::cout << "failed to set new priority: " << err << std::endl;
task();
};
m_thread = std::thread(wrap, priority, task);
}
virtual ~ConcurrentTaskBase(void)
{
waitForCompletion();
}
void waitForCompletion(void)
{
if (m_active)
{
m_thread.join();
m_active = false;
}
}
private:
std::thread m_thread;
bool m_active;
};
template<class R, class... ArgTypes>
class ConcurrentTask;
template<class R, class... ArgTypes>
class ConcurrentTask<R(ArgTypes...)> : public ConcurrentTaskBase
{
public:
ConcurrentTask(int priority, const std::function<R(ArgTypes...)>& task, ArgTypes&&... args)
: ConcurrentTaskBase(priority, bindTask(task, std::forward<ArgTypes>(args)...))
{}
std::shared_ptr<R> getReturn(void) noexcept
{
waitForCompletion();
return m_storage;
}
private:
std::function<void(void)> bindTask(const std::function<R(ArgTypes...)>& task, ArgTypes&&... args)
{
auto params = std::make_tuple(args...);
return [this, task, params](){m_storage = std::make_shared<R>(apply(task, params));};
};
std::shared_ptr<R> m_storage;
};
template<class... ArgTypes>
class ConcurrentTask<void(ArgTypes...)> : public ConcurrentTaskBase
{
public:
ConcurrentTask(int priority, const std::function<void(ArgTypes...)>& task, ArgTypes&&... args)
: ConcurrentTaskBase(priority, bindTask(task, std::forward<ArgTypes>(args)...))
{}
private:
std::function<void(void)> bindTask(const std::function<void(ArgTypes...)>& task, ArgTypes&&... args)
{
auto params = std::make_tuple(args...);
return [this, task, params](){apply(task, params);};
};
};
// Example stuff
struct MyStruct
{
int x;
int y;
};
int testFunction(MyStruct val)
{
std::cout << "X is " << val.x << " Y is " << val.y << std::endl;
return val.x + 10;
}
void printMe(int x)
{
std::cout << "Printing " << x << std::endl;
}
int main()
{
ConcurrentTask<int(MyStruct)> task(20, testFunction, {5, -21});
std::shared_ptr<int> received = task.getReturn();
std::cout << "Return value is " << *received << std::endl;
ConcurrentTask<void(int)> voidTask(25, printMe, -123);
return 0;
}
这或多或少就是Yakk写的。也许我不太理解你的想法,但对我来说,你似乎已经过度设计了,你过早地使用了std::function
。此外,ArgTypes&&
不会是转发/通用引用的列表,因为它们没有在bindTask
中推导出来。
在gcc 4.8.2上可以成功编译:
为c++ 11获取自己的integer_sequence
。由Xeo提供。
写apply
将元组参数应用于函数(也许这可以改进):
template<typename Func, typename Tuple, unsigned int... is>
auto apply_impl(Func&& f, Tuple&& params, seq<is...>)
// -> decltype(f(std::get<is>(std::forward<Tuple>(params))...)) // C++11 only
{
using std::get; // enable ADL-lookup for get in C++14
return f(get<is>(std::forward<Tuple>(params))...);
}
template<typename Func, typename Tuple>
auto apply(Func&& f, Tuple&& params)
// -> decltype(apply_impl(std::forward<Func>(f), std::forward<Tuple>(params),
// GenSeq<std::tuple_size<typename std::decay<Tuple>::type>::value>{}))
// C++11 only
{
return apply_impl(std::forward<Func>(f), std::forward<Tuple>(params),
GenSeq<std::tuple_size<typename std::decay<Tuple>::type>::value>{});
}
简化你的bindTask
(虽然在这一点上,我将保持它作为一个模板):
auto params = make_tuple(args...);
std::function<void(void)> bound = [task,params]{ apply(task, params); };
return bound;
在c++ 14中执行[task=std::move(task),params=std::move(params)]
以避免不必要的复制
作为猜测,bind假定它可以被重复调用(特别是在左值上下文中调用时!),因此不会将右值参数转换为右值参数,并将其绑定函数作为右值参数。这是你的代码要求的。这个lambda不是完美的转发!
您还通过lambdas中的引用捕获const&
std::function
s,这只会引起悬垂引用地狱。但这是一个运行时问题。作为一般规则,除非lambda和所有副本的生命周期在当前作用域结束,否则永远不会捕获&
;在原型制作过程中绝对不要这样做,即使"确定"不会有问题。
我会考虑编写std::apply
和index_sequence
s的弱版本,并将参数打包到tuple
中,然后执行apply
将其解包到目标可调用对象中。但这是一种偏见,不知道是否理想。
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- 为什么 std::function 可以作为 std::not2 的参数?
- 传递给std::function template的template参数究竟代表什么
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 在C++中,使用带有 std::optional 参数的函数<T>来表示可选参数是否有意义?
- 将函数参数"const char*"转换为"std::string_view"是
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 为什么 std::绑定错误参数可以成功?
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- std::vector 没有重载函数的实例与参数列表匹配
- std::span<const T> 作为函数模板中的参数
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 在构造函数中使用可变参数初始化 std::tuple
- 为什么我不能将引用作为 std::async 的函数参数传递
- 转发变量参数列表以模拟 std::thread
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 可变参数模板参数扩展 类型为 std::function 的类成员