【发布时间】:2021-06-22 05:55:27
【问题描述】:
我有一个连接函数调用的模板函数:
template<typename Callable, typename... Callables>
decltype(auto) concat_callable(Callable callable, Callables... callables)
{
if constexpr (sizeof...(callables) > 0) {
return [=]
<typename... Args>
(Args&&... args) {
return callable(concat_callable(callables...)(std::forward<Args>(args)...));
};
}
else {
return callable;
}
}
我想调用以下函数:
std::string& f_1(std::string& str)
{
return str;
}
std::string& f_2(std::string& str)
{
return str;
}
std::string& f_3(std::string& str)
{
return str;
}
我想这样称呼它:
std::string str{ "sample text" };
concat_callable(f_1, f_2, f_3)(str);
相当于:
f_1(f_2(f_3(str)));
但目前我收到一个错误'std::string &(std::string &)': cannot convert argument 1 from 'std::string' to 'std::string &'。
由于某种原因,当我只使用两个函数时它编译得很好,例如:
concat_callable(f_1, f_2)(str);
请问有什么解决办法吗?
【问题讨论】:
标签: c++ lambda variadic-templates c++20