2

我的意图是创建一个大小为 n 的函数参数列表,这样我就可以将它传递给一个帮助器,该帮助器使用折叠表达式将这些值递归地相乘。

我对如何使参数列表传递给助手有点困惑。有没有办法在没有包表达式的情况下创建函数参数列表?也许通过创建一个数组或元组?

到目前为止,这是我想出的。

template<typename T, typename N>
T SmoothStart(const T& t, const N& n) {
    static_assert(std::is_integral_v<N>, "templatized SmoothStart requires type of N to be integral.");
    static_assert(n >= 0, "templatized SmoothStart requires value of N to be non-negative.");

    if constexpr (n == 0) {
        return 1;
    }
    if constexpr (n == 1) {
        return t;
    }
    return SmoothStart_helper((t, ...)); //<-- obviously this doesn't work but it would be awesome to have!
}

template<typename T, typename... Args>
T SmoothStart_helper(Args&&... args) {
    return (args * ...);
}
4

1 回答 1

3

首先,n如果要使用折叠表达式,必须在编译时知道。如果将其移至模板参数,则获取大小参数包的最简单方法N是使用std::make_index_sequence

// The helper has to be first so that the compiler can find SmoothStart_helper().
template<typename T, std::size_t... Is>
T SmoothStart_helper(const T& t, std::index_sequence<Is...>) {
    // You were taking t by value here; I think you might want to still
    // take it by reference

    // Use the comma operator to simply discard the current index and instead
    // yield t. The cast to void is to silence a compiler warning about
    // Is being unused
    return (((void) Is, t) * ...);
}

template<std::size_t N, typename T>
T SmoothStart(const T& t) {
    // std::size_t is unsigned, so no need to check for N >= 0.
    // We also don't need to special case when N == 1. The fold
    // expression handles that case and just returns t
    return SmoothStart_helper(t, std::make_index_sequence<N>{});
}

然后你可以像这样使用它:SmoothStart<N>(myThing);.

神螺栓

于 2018-02-21T20:06:52.987 回答