[英]Forwarding variadic function parameters to a std::function object
所以我試圖創建一個隊列來存儲稍后調用的函數。 為了做到這一點,我必須創建一個 std::function 對象,以便將任何傳遞的函數放入隊列中。 問題出現在我創建這個對象並且可以調用它的地方,但似乎傳遞的參數沒有正確存儲(我希望它們如何)
主要問題都在成員模板函數push(Ret (&)(...), &&...)
我嘗試在push(temp)
上方插入對臨時創建的函數的調用,一切都按預期工作。 但是當我嘗試從隊列中訪問此函數時,似乎我轉發的參數已被覆蓋。
class thread_queue {
public:
thread_queue() = default;
void push(std::function<void()> func) { thread_q_.push(func); }
template <typename Ret, typename ... Params>
void push(Ret (&func)(Params...), Params&&... params) {
std::function<void()> temp = [&]() { // TO DO : FIX HERE
func(std::forward<Params>(params)...);
};
push(temp);
}
void pop() { thread_q_.pop(); }
std::function<void()> front() { return thread_q_.front(); }
private:
std::queue<std::function<void()>> thread_q_;
};
void func1() {
std::cout << "Inside of func1" << std::endl;
}
void func2(int a) {
std::cout << "Inside of func2 " << a << std::endl;
}
int func3() {
std::cout << "Inside of func3" << std::endl;
return 5;
}
int func4(int a, int b) {
std::cout << "Inside of func4 " << a + b << std::endl;
return a + b;
}
int main() {
thread_queue test;
test.push(func1);
test.push(func2, 10);
test.push(func3);
test.push(func4, 1, 8);
test.front()();
test.pop();
test.front()();
test.pop();
test.front()();
test.pop();
test.front()();
test.pop();
return 0;
}
所以有了這個我想得到
Inside of func1
Inside of func2 10
Inside of func3
Inside of func2 10
Inside of func3
Inside of func4 9
Inside of func3
Inside of func4 9
但我得到的Inside of func1
Inside of func2 8
Inside of func3
Inside of func2 8
Inside of func3
Inside of func4 9
Inside of func3
Inside of func4 9
一些進一步的說明:我想嘗試轉發傳遞的參數,所以如果我決定傳遞一些大對象,那么浪費的時間會比復制它少。 我還考慮以某種方式在參數上使用 shared_ptr 或 unique_ptr,但尚未對此進行測試,因為我想盡可能避免這種情況。 謝謝你。
編輯:看來我的問題可能與傳遞的右值引用有關,因為當我嘗試將 10、1 和 8 轉換為左值(通過將它們設置為 main 中的變量)時,它按預期工作。 現在更多地研究這個
您的隊列保存對參數的引用,因此在調用函數時參數必須仍在范圍內。 例如
{
int value = 1;
test.push(func2, value);
}
test.front()(); //Invalid, value is out of scope
int value2 = 2;
test.push(func2, value2);
test.front()(); //Ok, value2 is still in scope
test.push(func2, 3);
test.front()(); //Invalid, the temporary that was holding 3 is out of scope
如果您希望函數始終有效,則需要按值存儲參數。 在 lambda 中按值捕獲參數包並不簡單,但是,我們可以使用 std::bind 代替 lambda。
#include <functional>
#include <queue>
#include <iostream>
class thread_queue {
public:
thread_queue() = default;
void push(std::function<void()> func) { thread_q_.push(func); }
template <typename Ret, typename ... Params>
void push(Ret (&func)(Params...), Params&&... params) {
std::function<void()> temp = std::bind(func, std::forward<Params>(params)...);
push(std::move(temp));
}
void pop() { thread_q_.pop(); }
std::function<void()> front() { return thread_q_.front(); } //could avoid a copy
//by returning a reference. Would be more consistent with other containers.
private:
std::queue<std::function<void()>> thread_q_;
};
void func1() {
std::cout << "Inside of func1" << std::endl;
}
void func2(int a) {
std::cout << "Inside of func2 " << a << std::endl;
}
int func3() {
std::cout << "Inside of func3" << std::endl;
return 5;
}
int func4(int a, int b) {
std::cout << "Inside of func4 " << a + b << std::endl;
return a + b;
}
int main() {
thread_queue test;
test.push(func1);
test.push(func2, 10);
test.push(func3);
test.push(func4, 1, 8);
test.front()();
test.pop();
test.front()();
test.pop();
test.front()();
test.pop();
test.front()();
test.pop();
return 0;
}
更新:如果您只移動參數 std::bind 將不起作用,因為它返回的對象可以被多次調用,因此無法移動存儲的參數。 僅移動參數的另一個問題是 std::function 要求傳遞給它的函數對象是可復制的。 解決這些問題的一個原因是在 std::function 中存儲一個 std::shared_ptr 例如
#include <functional>
#include <queue>
#include <iostream>
#include <tuple>
#include <memory>
class thread_queue {
template <typename Ret, typename... Params>
struct callable {
Ret (&func)(Params...);
std::tuple<Params...> params;
template<typename... Params2>
callable(Ret (&func1)(Params...), Params2&&... params) :
func(func1),
params{std::forward<Params2>(params)...}
{}
void operator()() {
std::apply(func, std::move(params));
}
};
public:
thread_queue() = default;
void push(std::function<void()> func) { thread_q_.push(std::move(func)); }
template <typename Ret, typename... Params>
void push(Ret (&func)(Params...), Params&&... params) {
auto data = std::make_shared<callable<Ret, Params...>>(func, std::forward<Params>(params)...);
thread_q_.push(std::function<void()>{
[data = std::move(data)]() {
(*data)();
}
});
}
void pop() { thread_q_.pop(); }
std::function<void()>& front() { return thread_q_.front(); }
private:
std::queue<std::function<void()>> thread_q_;
};
struct MoveOnly {
MoveOnly() {}
MoveOnly(MoveOnly&&) {}
};
void func5(MoveOnly m) {
std::cout << "func5\n";
}
int main() {
thread_queue test;
test.push(func5, MoveOnly{});
test.front()();
test.pop();
return 0;
}
另一個可能更快的解決方案是編寫您自己的 std::function 版本。 下面是一個最小的例子,不包括小緩沖區優化。
#include <functional>
#include <queue>
#include <iostream>
#include <tuple>
#include <memory>
template<class T>
class move_only_function;
template<class R, class... Args>
class move_only_function<R(Args...)>
{
struct base_callable
{
virtual R operator()(Args... args) = 0;
virtual ~base_callable() = default;
};
template<class F>
struct callable : public base_callable
{
F func;
callable(const F& f) : func(f) {}
callable(F&& f) : func(std::move(f)) {}
virtual R operator()(Args... args) override
{
return static_cast<R>(func(args...));
}
};
std::unique_ptr<base_callable> func;
public:
move_only_function(move_only_function&& other) : func(std::move(other.func)) {}
template<class F>
move_only_function(F&& f) : func(std::make_unique<callable<F>>(std::forward<F>(f))) {}
template<class... Args2>
R operator()(Args2&&... args)
{
return (*func)(std::forward<Args2>(args)...);
}
};
class thread_queue {
public:
thread_queue() = default;
void push(move_only_function<void()> func) { thread_q_.push(std::move(func)); }
template <typename Ret, typename ... Params>
void push(Ret (&func)(Params...), Params&&... params) {
thread_q_.push(move_only_function<void()>{
[func, tup=std::make_tuple(std::forward<Params>(params)...)]() mutable {
return std::apply(func, std::move(tup));
}});
}
void pop() { thread_q_.pop(); }
move_only_function<void()>& front() { return thread_q_.front(); }
private:
std::queue<move_only_function<void()>> thread_q_;
};
struct MoveOnly {
MoveOnly() {}
MoveOnly(MoveOnly&&) {}
};
void func5(MoveOnly m) {
std::cout << "func5\n";
}
int main() {
thread_queue test;
test.push(func5, MoveOnly{});
test.front()();
test.pop();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.