带有decltype return的模板推导/替换错误

时间:2018-12-19 19:32:16

标签: c++ c++11 c++14 variadic-templates decltype

我在这里有此代码段,其中包含一个函数指针元组和一个值元组,然后将值绑定到函数指针,并返回一个具有绑定函数的新元组。

当我直接调用bind_tuple_impl时,我没有任何错误,但这并不是调用此例程的实际方法。我还需要在绑定后延迟执行,因此仅不能使用给定参数调用函数。

我把这一切都弄错了吗?我是否缺少明显的东西

template <class Fs, class Ts, std::size_t... Is>
auto bind_tuple_impl(std::tuple<Fs> f, std::tuple<Ts> t, std::index_sequence<Is...>)
   -> decltype(std::make_tuple(std::bind(std::get<Is>(f),std::get<Is>(t))...))
{
   return std::make_tuple(std::bind(std::get<Is>(f),std::get<Is>(t))...);
}

template <class... Fs, class... Ts>
auto bind_tuple(std::tuple<Fs...> f, std::tuple<Ts...> t)
   -> decltype(bind_tuple_impl(f, t, std::index_sequence_for<Fs...>))
{
   return bind_tuple_impl(f, t, std::index_sequence_for<Fs...>);
}

int main()
{
    std::tuple<std::function<void(double)>> f;
    std::tuple<double> t;

    auto f_bound = bind_tuple(f,t);
}

错误:

main.cpp:91:68: error: expected primary-expression before ')' token    
    -> decltype(bind_tuple_impl(f, t, std::index_sequence_for<Fs...>))    
                                                                    ^    
main.cpp: In function 'decltype (bind_tuple_impl(f, t, <expression error>)) bind_tuple(std::tuple<_Tps ...>, std::tuple<_Elements ...>)':

main.cpp:94:63: error: expected primary-expression before ')' token    
    return bind_tuple_impl(f, t, std::index_sequence_for<Fs...>);    
                                                               ^    
main.cpp: In function 'int main()':    
main.cpp:102:34: error: no matching function for call to 'bind_tuple(std::tuple<std::function<void(double)> >&, std::tuple<double>&)'    
     auto f_bound = bind_tuple(f,t);    
                                  ^    
main.cpp:89:6: note: candidate: 'template<class ... Fs, class ... Ts> decltype (bind_tuple_impl(f, t, <expression error>)) bind_tuple(std::tuple<_Tps ...>, std::tuple<_Elements ...>)'    
 auto bind_tuple(std::tuple<Fs...> f, std::tuple<Ts...> t)    
      ^~~~~~~~~~    
main.cpp:89:6: note:   template argument deduction/substitution failed:

1 个答案:

答案 0 :(得分:1)

这里:

return bind_tuple_impl(f, t, std::index_sequence_for<Fs...>);

std::index_sequence_for<Fs...>只是一种类型。您需要实例化它:

return bind_tuple_impl(f, t, std::index_sequence_for<Fs...>{});

对您的decltype进行相同的更改(或摆脱尾随的返回类型,让auto来完成它的工作!)。