使用std :: index_sequence

时间:2015-09-28 12:05:22

标签: c++ templates c++11 variadic

在以下代码中:

#include <iostream>
#include <utility>
#include <set>

template <typename... Args>
void f(Args... args) {
    std::cout << sizeof...(Args) << " elements.\n";
}

template <std::size_t... Is>
void g (std::index_sequence<Is...>, const std::set<int>& set) {
    f((set.find(Is) == set.end() ? Is : 2*Is)...);
}

int main() {
    g (std::make_index_sequence<10>{}, {1,3,7,8});
}

我希望f((set.find(Is) == set.end() ? Is : 2*Is)...);使用Is set.find(Is) == set.end(),但不要使用2*Is(而不是#include <iostream> #include <utility> #include <tuple> template <typename... Args> void f(Args... args) { std::cout << sizeof...(Args) << " elements.\n"; } struct NullObject {}; template <typename Tuple, std::size_t... Is> void g (std::index_sequence<Is...>, const Tuple& tuple) { f ((std::get<Is>(tuple) != std::get<Is+1>(tuple) ? std::get<Is>(tuple) : NullObject{})...); } int main() { g (std::make_index_sequence<8>{}, std::make_tuple(2,1.5,'a','a',true,5,5,false)); } )。因此,传递的参数数量不固定。如何实现?

修改 对不起,但我太过简化了问题。下面的代码反映了更多真正的问题:

std::get<Is>(tuple) != std::get<Is+1>(tuple)

由于三元运算符传递的混合类型,上面的代码无法编译,但我认为你可以在这里看到我的想法。我希望在条件NullObject{}时传递NOTHING,所以我传递f然后以某种方式尝试从f的参数中删除所有NullObjects以获得传递给{{1的真实参数}}

2 个答案:

答案 0 :(得分:4)

您不能使用三元运算符 - 这需要两个具有公共类型的表达式。这里没有骰子。我知道无法根据运行时比较有条件地返回不同的类型

您必须通过引入构建Args...和简单if语句的另一个辅助函数来有条件地将类型转发到其他函数:

template <size_t... Js>
void fhelper (std::index_sequence<>, const std::set<int>& , std::index_sequence<Js...>) {
    f(Js...);
}

template <std::size_t I, size_t... Is, size_t... Js>
void fhelper (std::index_sequence<I, Is...>, const std::set<int>& set, std::index_sequence<Js...>) {
    if (set.find(I) == set.end()) {
        fhelper(std::index_sequence<Is...>{}, set, std::index_sequence<Js..., I>{});
    }
    else {
        fhelper(std::index_sequence<Is...>{}, set, std::index_sequence<Js...>{});
    }
}

int main() {
    fhelper (std::make_index_sequence<10>{}, {1,3,7,8}, std::index_sequence<>{});
}

答案 1 :(得分:0)

我只想分享我所寻求的一般问题的解决方案。由Barry解决上述具体问题的方法成为可能:

#include <iostream>
#include <utility>
#include <tuple>

template <template <std::size_t, typename> class Check, typename F, size_t... Js, typename Tuple>
void screenArguments (std::index_sequence<>, std::index_sequence<Js...>, const Tuple& tuple) {
    F()(std::get<Js>(tuple)...);
}

template <template <std::size_t, typename> class Check, typename F, std::size_t I, size_t... Is, size_t... Js, typename Tuple>
void screenArguments (std::index_sequence<I, Is...>, std::index_sequence<Js...>, const Tuple& tuple) {
    if (Check<I, Tuple>::execute(tuple))
        screenArguments<Check, F>(std::index_sequence<Is...>{}, std::index_sequence<Js..., I>{}, tuple);
    else
        screenArguments<Check, F>(std::index_sequence<Is...>{}, std::index_sequence<Js...>{}, tuple);
}

template <template <std::size_t, typename> class Check, typename F, typename Tuple, std::size_t N = std::tuple_size<Tuple>::value>
void passCertainArguments (const Tuple& tuple) {
    screenArguments<Check, F> (std::make_index_sequence<N>{}, std::index_sequence<>{}, tuple);
}

// Testing
#include <typeinfo>

template <typename... Args>
void foo (Args&&...) {
    std::cout << sizeof...(Args) << " elements passed into foo.\n";
}

struct Foo {
    template <typename... Args>
    void operator()(Args&&... args) {
        foo(std::forward<Args>(args)...);
    }
};

template <typename... Args>
void bar (Args&&...) {
    std::cout << sizeof...(Args) << " elements passed into bar.\n";
}

struct Bar {
    template <typename... Args>
    void operator()(Args&&... args) {
        bar(std::forward<Args>(args)...);
    }
};

template <std::size_t N, typename Tuple>
struct CheckArguments {
    static bool execute (const Tuple& tuple) {
        return std::get<N>(tuple) != std::get<N+1>(tuple);
    }
};

struct NullObject {};

template <std::size_t N, typename Tuple>
struct CheckNotNullObject {
    static bool execute (const Tuple& tuple) {
        return typeid(std::get<N>(tuple)) != typeid(NullObject);
    }
};

template <typename F, typename... Args>
void executeWithoutNullObject (Args&&... args) {
    passCertainArguments<CheckNotNullObject, F> (std::forward_as_tuple(args...));
}

template <typename F, typename... Args>
void executeRemoveConsecutiveRepeatElements (Args&&... args) {
    const auto tuple = std::forward_as_tuple(args...);
    passCertainArguments<CheckArguments, F, decltype(tuple), sizeof...(Args) - 1> (tuple);
}

int main() {
    executeWithoutNullObject<Foo> (3, 5, 'a', true, NullObject{}, 'b', 5.8, NullObject{}, NullObject{}, '!', 2);
    // 8 elements passed into foo.  (the 3 NullObjects are removed)
    executeRemoveConsecutiveRepeatElements<Bar> (2, 1.5, 'a', 'a', true, 5, 5, false);
    // 5 elements passed into bar.  (the first 'a', the first 5, and 'false' removed)
}