Invalid explicitly-specified argument for template

2019-03-01 11:25发布

问题:

I have a static_loop construct like this

template <std::size_t n, typename F> void static_loop(F&& f) {
    static_assert(n <= 8 && "static loop size should <= 8");
    if constexpr (n >= 8)
        f(std::integral_constant<size_t, n - 8>());
    if constexpr (n >= 7)
        f(std::integral_constant<size_t, n - 7>());
    if constexpr (n >= 6)
        f(std::integral_constant<size_t, n - 6>());
    if constexpr (n >= 5)
        f(std::integral_constant<size_t, n - 5>());
    if constexpr (n >= 4)
        f(std::integral_constant<size_t, n - 4>());
    if constexpr (n >= 3)
        f(std::integral_constant<size_t, n - 3>());
    if constexpr (n >= 2)
        f(std::integral_constant<size_t, n - 2>());
    if constexpr (n >= 1)
        f(std::integral_constant<size_t, n - 1>());
}


template <typename T> constexpr size_t tupleSize(T) { return tuple_size_v<T>; }
struct A {
    int a;
    int b;
    void run() {
        auto ab = std::make_tuple(std::ref(a), std::ref(b));
        static_loop<tupleSize(ab)>([&](auto i) { std::get<i>(ab) = i; });
        std::cout << a << " " << b << std::endl;
    }
};

However, it fails to iterate over a tuple as listed above.

live godbolt example

回答1:

Suggestion: try with

// .........VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
static_loop<std::tuple_size_v<decltype(ab)>>([&](auto i) { std::get<i>(ab) = i; });

I mean... you can't use ab (as value), in a constant expression, because ab isn't defined constexpr.

And you can't define it constexpr because it's initialized using std::ref() that isn't constexpr.

But you're not interested in ab as value to get the size of its type; you're interested only in ab type; so you can pass through decltype(ab).

-- EDIT --

Off Topic suggestion.

Instead of static_loop(), you can use the classic way based on std::index_sequence (and template folding, available starting from C++17).

I mean... if you define a run_1() function (with run_1_helper() helper) as follows

template <typename F, typename ... Ts, std::size_t ... Is>
void run_1_helper (F const & f, std::tuple<Ts...> & t, std::index_sequence<Is...> const)
 { (f(std::get<Is>(t), Is), ...); }

template <typename F, typename ... Ts>
void run_1 (F const & f, std::tuple<Ts...> & t)
 { run_1_helper(f, t, std::index_sequence_for<Ts...>{}); }

you can write A as follows

struct A {
    int a;
    int b;
    void run() {
        auto ab = std::make_tuple(std::ref(a), std::ref(b));
        run_1([](auto & v, auto i){ v = i; }, ab);
        std::cout << a << " " << b << std::endl;
    }
};

Or, maybe better, simply using std::apply(), as follows

struct A {
    int a;
    int b;
    void run() {
        auto ab = std::make_tuple(std::ref(a), std::ref(b));
        int i { -1 };
        std::apply([&](auto & ... vs){ ((vs = ++i), ...); }, ab);
        std::cout << a << " " << b << std::endl;
    }
};


回答2:

Change

template <typename T>
constexpr size_t tupleSize(T) { return tuple_size_v<T>; }

to:

template <typename T>
constexpr size_t tupleSize(T const&) { return tuple_size_v<T>; }

That is, take the argument by reference to const, not by value. As-is, you're trying to copy a non-constexpr tuple in a constant expression - that can't work. By reference is fine since you're not actually reading the tuple.