[英]variadic template and std::function
在下面的代码中:
#include <functional>
#include <iostream>
#include <tuple>
template <typename... t>
class a {
public:
explicit a(std::function<std::tuple<t...>()>&& p_d,
std::function<bool(t...)>&& p_f)
: m_d(std::move(p_d)), m_f(std::move(p_f)) {}
bool operator()() { return m_f(m_d()); }
private:
std::function<std::tuple<t...>()> m_d;
std::function<bool(t...)> m_f;
};
class d {
std::tuple<int, float, std::string&&> operator()() {
return std::tuple<int, float, std::string&&>(-9, 3.14, "olá!");
}
};
class f {
bool operator()(int p_i, float p_f, std::string&& p_s) {
std::cout << "i = " << p_i << ", f = " << p_f << ", s = " << p_s
<< std::endl;
return true;
}
};
int main() {
d _d;
f _f;
typedef a<int, float, std::string&&> a_t;
a_t _a(std::move(_d), std::move(_f));
_a();
return 0;
}
我收到编译器错误:
../untitled014/main.cpp: In function ‘int main()’:
../untitled014/main.cpp:38:38: error: no matching function for call to ‘a<int, float, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>::a(std::remove_reference<d&>::type, std::remove_reference<f&>::type)’
a_t _a(std::move(_d), std::move(_f));
^
../untitled014/main.cpp:8:12: note: candidate: a<t>::a(std::function<std::tuple<_Elements ...>()>&&, std::function<bool(t ...)>&&) [with t = {int, float, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}]
explicit a(std::function<std::tuple<t...>()>&& p_d,
^
../untitled014/main.cpp:8:12: note: no known conversion for argument 1 from ‘std::remove_reference<d&>::type {aka d}’ to ‘std::function<std::tuple<int, float, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>()>&&’
我不明白为什么编译器找不到合适的转换,因为它在错误的最后一行中报告。
我正在Xubuntu框上使用带有标志'-std = c ++ 14'的g ++,并且'g ++ --version'报告'g ++(Ubuntu 5.4.0-6ubuntu1〜16.04.10)5.4.0 20160609'
谁能告诉我我在做什么错?
谢谢!!
我在您的代码中看到至少三个错误。
没有特别的顺序...
(1)如果您想要功能, operator()
必须是public
; 你让他们都private
class d { // default for a class is private, so operator() is private
std::tuple<int, float, std::string&&> operator()() {
return std::tuple<int, float, std::string&&>(-9, 3.14, "olá!");
}
};
class f { // default per a class is private, so operator() is private
bool operator()(int p_i, float p_f, std::string&& p_s) {
std::cout << "i = " << p_i << ", f = " << p_f << ", s = " << p_s
<< std::endl;
return true;
}
};
您可以解决此问题,使operator()
public
,或者使d
和f
struct
更简单。
(2) "olá!"
不是初始化std::string &&
的有效值
std::tuple<int, float, std::string&&>(-9, 3.14, "olá!");
你可以用
std::tuple<int, float, std::string&&>(-9, 3.14, std::string{"olá!"});
但这是一个错误的主意,因为用这种方式,您用一个对象的引用初始化了一个对象,该对象在此之后立即被销毁,并且在元组中获得了一个悬空的引用。
我不清楚,为什么要在元组中使用std::string &&
,但我怀疑您可以使用std::string
,而不是对其的引用。 在所有代码中,不仅限于此功能。
在这种情况下,以下代码
std::tuple<int, float, std::string>(-9, 3.14, "olá!");
作品。
如果您确实要在元组中引用std::string
,则必须将引用传递给对象,该对象的生存期应足以覆盖元组的生存期。
(3) d
的operator()
返回std::tuple<int, float, std::string&&>
,其中f
的operator()
接受三个参数: int
, float
和std::string&&
。
因此,您不能简单地将第一个返回的值传递给第二个,而不用像打开a::operator()
那样解压std::tuple
bool operator()() { return m_f(m_d()); }
// ........................^^^^^^^^^^ Wrong!
您正在使用C ++ 14,因此无法使用std::apply()
(可从C ++ 17开始使用)
bool operator()() { return std::apply(m_f, m_d()); }
// ........................^^^^^^^^^^^^^^^^^^^^^^ Starting from C++17
因此您必须以某种方式进行仿真( std::make_index_sequence
, std::index_sequence
, std::get()
等)。
举个例子
bool operator() ()
{ return call(std::make_index_sequence<sizeof...(t)>{}); }
template <std::size_t ... Is>
auto call (std::index_sequence<Is...> const &)
{
auto tmp { m_d() }; // so m_d() is called only one time
return m_f(static_cast<t>(std::get<Is>(tmp))...);
}
其中call()
可以是private
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.