如何在可变参数模板之前推导模板

How to deduce a template before a varidic template

我目前遇到以下问题,我的函数模板必须在可变参数模板之前声明,而编译器无法推导它。

template<class F, class... Ts>
void update(F f){
    for(auto t: get_range<Ts...>()){
        apply(std::forward<F>(f), t);
    }
}
..
cg.update<decltype(ftest),int,float>(ftest);
..

这个问题有好的解决方法吗?我想这样称呼它

cg.update<int,float>(ftest);

我相信在 C++17 中我可以写

template<class... Ts>
void update(auto f){
    for(auto t: get_range<Ts...>()){
        apply(f, t);
    }
}

但是 clang 似乎还不支持它。

只需将 class F 参数放在可变 class... Ts 参数之后。

template<class... Ts>
void get_range(){ }

auto x = [](auto){};

template<class... Ts, class F>
void update(F f)
{        
    // The following `static_assert` assumes the function is being
    // instantiated with `<int,float>`. It's just here to prove
    // that `F` is not part of `Ts...`.

    // Make sure that `F` is not being caught in `Ts...`:
    static_assert(sizeof...(Ts) == 2, "");

    // Make sure that `F` is actually `decltype(x)`:
    static_assert(std::is_same<decltype(f), decltype(x)>{}, "");

    // Call your function:
    get_range<Ts...>();
}

int main() 
{       
    update<int,float>(x);
    return 0;
}

ideone example