std::index_sequence 三元运算符
Ternary operator with std::index_sequence
在下面的代码中:
#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});
}
如果 set.find(Is) == set.end()
,我希望 f((set.find(Is) == set.end() ? Is : 2*Is)...);
使用 Is
,否则什么都不用(而不是 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)
,我想传递 NOTHING,所以我传递 NullObject{}
然后以某种方式尝试从 f
的参数中删除所有 NullObjects 以获得传递给 [=19 的真实参数=].
您不能为此使用三元运算符 - 这需要两个具有共同类型的表达式。这里没有骰子。我知道无法根据 runtime 比较有条件地返回不同的 type。
您必须有条件地将类型转发给另一个函数,方法是引入另一个构建 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<>{});
}
我只是想分享我所寻求的一般问题的解决方案。 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)
}
在下面的代码中:
#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});
}
如果 set.find(Is) == set.end()
,我希望 f((set.find(Is) == set.end() ? Is : 2*Is)...);
使用 Is
,否则什么都不用(而不是 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)
,我想传递 NOTHING,所以我传递 NullObject{}
然后以某种方式尝试从 f
的参数中删除所有 NullObjects 以获得传递给 [=19 的真实参数=].
您不能为此使用三元运算符 - 这需要两个具有共同类型的表达式。这里没有骰子。我知道无法根据 runtime 比较有条件地返回不同的 type。
您必须有条件地将类型转发给另一个函数,方法是引入另一个构建 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<>{});
}
我只是想分享我所寻求的一般问题的解决方案。 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)
}