Differentiate between two pack expansions - c++

Is there a way to explicitly specify which ... refers to which pack expansion? In my code I have two pack expansions that I want to apply at different levels:
template<typename T, int N>
struct MyArr
{
T e[N];
constexpr T& operator[](int i) { return e[i]; }
constexpr const T& operator[](int i) const { return e[i]; }
MyArr() : e{} {}
template<typename ...type_pack>
MyArr(const type_pack&... pack) : e{pack...}
{
static_assert(sizeof...(pack)==N,
"Argument count must match the size.");
}
};
template<typename type_lhs, typename type_rhs>
auto add(const type_lhs& lhs, const type_rhs& rhs)
{
return lhs + rhs;
}
template<int ...I, typename type_head, typename ...type_pack, int N, typename Function>
auto apply(Function&& op,
const MyArr<type_head,N>& head, const MyArr<type_pack,N>&... pack)
{
return MyArr<type_head,N>((op(head[I],(pack[I])...))...);
// expand pack[I]- ^ , ^ - expand I...
};
int main()
{
MyArr<int,3> a(1,2,3);
return apply<0,1,2>(add<int,int>, a, a);
}
Essentially, I want to get:
(op(head[0], get<0>(pack)[0], ..., get<M-1>(pack)[0]),
...,
op(head[N-1], get<0>(pack)[N-1], ..., get<M-1>(pack)[N-1]))
Thanks to OznOg's advice I got it to work through creating a function in the middle:
template<int ...I, typename type_head, typename ...type_pack, int N, typename Function>
auto apply(Function&& op,
const MyArr<type_head,N>& head, const MyArr<type_pack,N>&... pack)
{
auto op2 = [&](int i) { return op(head[i], pack[i]...);};
return MyArr<type_head,N>(op2(I)...);
};

In this particular case, the only way I see is the use of an helper function (getVal(), in the following example)
template <int I, typename type_head, typename ...type_pack, int N,
typename Function>
auto getVal (Function&& op, MyArr<type_head,N> const & head,
MyArr<type_pack,N> const & ... pack)
{ return op(head[I], pack[I]...); }
template <int ... Is, typename type_head, typename ...type_pack, int N,
typename Function>
auto apply (Function && op, MyArr<type_head,N> const & head,
MyArr<type_pack,N> const &... pack)
{ return MyArr<type_head,N>{ getVal<Is>(op, head, pack...)... }; }
The problem is that you have
(pack[I])...
so there is no way (as far I know) to say that the expansion is to be applied to pack and not to I.
With the intermediate function
//.......................VVV expand pack
getVal<Is>(op, head, pack...)...
//...........................^^^ expand Is
you can use parentheses to separate the levels.
But you have to separate pack and Is.

Related

Improving fold function

I have implemented a simple fold function in C++ that accepts a lambda, and can fold multiple vectors at the same time at compile time. I am wondering if it could be simplified in some manner (I have provided both a recursive version and an iteratively recursive version - I am unsure which should have better performance): https://godbolt.org/z/39pW81
Performance optimizations are also welcome - in that regard is any of the two approaches faster?
template<int I, typename type_identity, typename type_head, int N, typename ...type_tail, int ...N_tail, typename Function>
auto foldHelperR(Function&& func, const type_identity& id, const tvecn<type_head, N>& head, const tvecn<type_tail, N_tail>&... tail)
{
if constexpr (I>0)
{
return func(foldHelperR<I-1>(std::forward<Function>(func), id, head, tail...), head[I], tail[I]...);
}
else
{
return func(id, head[0], tail[0]...);
}
}
template<int I, typename type_identity, typename type_head, int N, typename ...type_tail, int ...N_tail, typename Function>
auto foldHelperI(Function&& func, const type_identity id, const tvecn<type_head, N>& head, const tvecn<type_tail, N_tail>&... tail)
{
if constexpr (I<N-1)
{
return foldHelperI<I+1>(std::forward<Function>(func), func(id, head[I], tail[I]...), head, tail...);
}
else
{
return func(id, head[N-1], tail[N-1]...);
}
}
template<typename type_identity, typename type_head, int N_head, typename ...type_tail, int ...N_tail, typename Function = void (const type_identity&, const type_head&, const type_tail&...)>
constexpr auto fold(Function&& func, const type_identity& id, const tvecn<type_head, N_head>& head, const tvecn<type_tail, N_tail>&... tail)
{
static_assert(std::is_invocable_v<Function, const type_identity&, const type_head&, const type_tail &...>,
"The function cannot be invoked with these zip arguments (possibly wrong argument count).");
static_assert(all_equal_v<N_head, N_tail...>, "Vector sizes must match.");
//return foldHelperR<N_head-1>(std::forward<Function>(func), id, head, tail...);
return foldHelperI<0>(std::forward<Function>(func), id, head, tail...);
}
int main()
{
tvecn<int,3> a(1,2,3);
return fold([](auto x, auto y, auto z) {return x+y+z;}, 0, a, a);
}
and can fold multiple vectors at the same time at compile time
Not exactly: if you want to operate compile-time
(1) you have to define constexpr the tvecn constructor and
(2) you have to define constexpr the foldhelper function and
(3) you have to declare constexpr a
// VVVVVVVVV
constexpr tvecn<int,3> a(1,2,3);
(4) you have to place the result of fold in a constexpr variable (or, more generally speaking, in a place where the value is required compile time, as the size field of a C-style array, or a template value parameter, or a static_assert() test)
constexpr auto f = fold([](auto x, auto y, auto z) {return x+y+z;},
0, a, a);
I am wondering if it could be simplified in some manner
Sure.
First of all: if you can, avoid to reinventing the weel: your tvecn is a simplified version of std::array.
Suggestion: use std::array (if you can obviously)
Second: you tagged C++17 so you can use folding
Suggestion: use it also for all_equal
template <auto V0, auto ... Vs>
struct all_equal : public std::bool_constant<((V0 == Vs) && ...)>
{ };
template<auto ...N_pack>
constexpr bool all_equal_v = all_equal<N_pack...>::value;
More in general: when you have to define a custom type traits that has to provide a number, inherit (if possible) from std::integral_constant (or std::bool_constant, or std::true_type, or std::false_type: all std::integral_constant specializations). So you automatically inherit all std::integral_constant facilities.
Third: almost all C++ standard uses std::size_t, not int, for sizes.
Suggestion: when you have to do with sizes, use std::size_t, not int. This way you can avoid a lot of annoying troubles.
Fourth: from main() you should return only EXIT_SUCCESS (usually zero) or EXIT_FAILURE (usually 1)
Suggestion: avoid things as
return fold([](auto x, auto y, auto z) {return x+y+z;}, 0, a, a);
Fifth: never underestimate the power of the comma operator.
Suggestion: avoid recursion at all and use template folding also for the helper function; by example
template <std::size_t ... Is, typename F, typename T, typename ... As>
constexpr auto foldHelperF (std::index_sequence<Is...>,
F const & f, T id, As const & ... arrs)
{ return ( ..., (id = [&](auto i){ return f(id, arrs[i]...); }(Is))); }
that you can call as follows from fold()
return foldHelperF(std::make_index_sequence<N_head>{},
std::forward<Function>(func),
id, head, tail...);
The following is a full compiling, and simplified, example
#include <array>
#include <utility>
#include <iostream>
#include <type_traits>
template <auto V0, auto ... Vs>
struct all_equal : public std::bool_constant<((V0 == Vs) && ...)>
{ };
template<auto ...N_pack>
constexpr bool all_equal_v = all_equal<N_pack...>::value;
template <std::size_t ... Is, typename F, typename T, typename ... As>
constexpr auto foldHelperF (std::index_sequence<Is...>,
F const & f, T id, As const & ... arrs)
{ return ( ..., (id = [&](auto i){ return f(id, arrs[i]...); }(Is))); }
template <typename type_identity, typename type_head, std::size_t N_head,
typename ...type_tail, std::size_t ...N_tail,
typename Function = void (type_identity const &,
type_head const &,
type_tail const & ...)>
constexpr auto fold (Function && func, type_identity const & id,
std::array<type_head, N_head> const & head,
std::array<type_tail, N_tail> const & ... tail)
{
static_assert( std::is_invocable_v<Function, const type_identity&,
const type_head&, const type_tail &...>,
"The function cannot be invoked with these zip arguments"
" (possibly wrong argument count).");
static_assert( all_equal_v<N_head, N_tail...>,
"Vector sizes must match.");
return foldHelperF(std::make_index_sequence<N_head>{},
std::forward<Function>(func),
id, head, tail...);
}
int main()
{
constexpr std::array<int, 3u> b{2, 5, 7};
constexpr auto f = fold([](auto x, auto y, auto z) {return x+y+z;},
0, b, b);
std::cout << f << std::endl;
}
With Fold expression, it might be:
template <typename F, typename Init, std::size_t... Is, typename... Arrays>
constexpr auto fold_impl(F&& f, Init init, std::index_sequence<Is...>, Arrays&&... arrays)
{
auto l = [&](Init init, std::size_t i){ return f(init, arrays[i]...); };
return ((init = l(init, Is)), ...);
}
template <typename F, typename Init, typename Array, typename ... Arrays>
constexpr auto fold(F&& f, Init init, Array&& array, Arrays&&... arrays)
{
static_assert(((arrays.size() == array.size()) && ...));
return fold_impl(f, init, std::make_index_sequence<array.size()>{}, array, arrays...);
}
Demo

Implementing mathematical complement logic on variadic template parameters

I have a class that defines a constexpr size_t dimensions. In this class, I implemented an EvaluateOver<Dimensions...>(Lambda F) which does something over the dimensions I specify. For example, say dimensions=4 and f is some lambda expression:
MyClass.EvaluateOver<0,2>(f);
will perform f with respect to 0 and 2 by doing the following expansion:
template<size_t... Dims, typename Lambda>
inline auto EvaluateOver(const Lambda& F) const
{
F(std::get<Dims>(_memberTupleDataContainer)...);
}
Now I want another member function that will evaluate over the unspecified dimensions. So EvaluateOverOthers<0,2>(f) will perform its operation on dimensions 1 and 3.
Ideally, I am thinking of the following:
template<size_t... Dims, typename Lambda>
inline auto EvaluateOverOthers(const Lambda& F) const
{
EvaluateOver<
// variadic parameter that does the mathematical complement of
// Dims... with a variadic expansion of dimensions
>(F);
}
Following might help:
namespace details
{
template <typename Seq1, typename Seq2, typename Res = std::index_sequence<>>
struct minus;
// Nothing more to remove
template <std::size_t ... Is1, std::size_t... IRes>
struct minus<std::index_sequence<Is1...>,
std::index_sequence<>,
std::index_sequence<IRes...>>
{
using type = std::index_sequence<IRes..., Is1...>;
};
// Remove front elements as they are equal.
template <std::size_t I, std::size_t ... Is1, std::size_t ... Is2, std::size_t... IRes>
struct minus<std::index_sequence<I, Is1...>,
std::index_sequence<I, Is2...>,
std::index_sequence<IRes...>>
{
using type = typename minus<std::index_sequence<Is1...>,
std::index_sequence<Is2...>,
std::index_sequence<IRes...>>::type;
};
// Add front element to result.
template <std::size_t I1, std::size_t I2,
std::size_t ... Is1, std::size_t ... Is2,
std::size_t... IRes>
struct minus<std::index_sequence<I1, Is1...>,
std::index_sequence<I2, Is2...>,
std::index_sequence<IRes...>>
{
using type = typename minus<std::index_sequence<Is1...>,
std::index_sequence<I2, Is2...>,
std::index_sequence<IRes..., I1>>::type;
};
}
template <std::size_t N, typename Seq>
using complement = details::minus<std::make_index_sequence<N>, Seq>;
template <std::size_t N, typename Seq>
using complement_t = typename complement<N, Seq>::type;
// Some test
static_assert(std::is_same<std::index_sequence<0, 3>,
complement_t<4, std::index_sequence<1, 2>>>::value, "!");
And then
template<size_t... Is, typename Lambda>
auto EvaluateOver(const Lambda& F, std::index_sequence<Is...>) const
{
return F(std::get<Is>(_memberTupleDataContainer)...);
}
template<size_t... Dims, typename Lambda>
auto EvaluateOver(const Lambda& F) const
{
return EvaluateOver(F, std::index_sequence<Is...>{});
}
template<size_t... Is, typename Lambda>
auto EvaluateOverOthers(const Lambda& F) const
{
return EvaluateOver(F, complement_t<_dimension, std::index_sequence<Is...>>{});
}
I played with a constexpr C++17 solution (online demo). I think the logic for complements could be factored out (if desired).
#include <iostream>
#include <iterator>
#include <utility>
#include "my_constexpr_array.hpp"
template<size_t Nd>
struct MyClass {
static constexpr auto dim_arr = Array(std::make_index_sequence<Nd>{});
template<size_t... excludes, class F>
auto eval_others(F f) const {
constexpr auto excl_arr = Array{excludes...};
constexpr auto incl_pred = [&] (size_t i) { return !excl_arr.contains(i); };
constexpr auto incl_excl_arr = dim_arr.partition(incl_pred);
constexpr auto incl_count = dim_arr.count_if(incl_pred);
return eval_helper(
f,
[&] { return incl_excl_arr; },// wrapped in lambda to preserve constexpr
std::make_index_sequence<incl_count>{}// indices for trimming incl_excl_arr
);
}
template<class F, class Dims, size_t... is>
auto eval_helper(F f, Dims dims, std::index_sequence<is...>) const {
return f(std::integral_constant<size_t, dims()[is]>{}...);
}
};
int main() {
MyClass<7> foo{};
foo.eval_others<2, 4>([&] (auto... is) { (std::cout << ... << is) << "\n"; });
return 0;
}
Where "my_constexpr_array.hpp" would behave like
template<class T, size_t size>
struct Array {
static_assert(size >= 1);
T data_[size];
constexpr Array() noexcept
: data_{} {}
template<class... Ts>
explicit constexpr Array(T v0, Ts... vs) noexcept
: data_{v0, vs...} {}
template<T... vs>
explicit constexpr Array(std::integer_sequence<T, vs...>) noexcept
: data_{vs...} {}
constexpr T* begin() { return data_; }
constexpr const T* begin() const { return data_; }
constexpr T* end() { return begin() + size; }
constexpr const T* end() const { return begin() + size; }
constexpr decltype(auto) operator[](size_t i) { return data_[i]; }
constexpr decltype(auto) operator[](size_t i) const { return data_[i]; }
constexpr bool contains(const T& v) const {
for(auto& x : *this) if(x == v) return true;
return false;
}
template<class Pred>
constexpr size_t count_if(Pred pred) const {
size_t result = 0;
for(auto& x : *this) result += size_t(pred(x) ? 1 : 0);
return result;
}
template<class Pred>
constexpr Array partition(Pred pred) const {
// return a sorted copy such that all `true`s precede all `false`s
Array result{};
T* true_false_dst[2] = {result.begin(), result.begin() + count_if(pred)};
// pair of output iterators; use first if predicate is true and vice versa
for(auto& x : *this) *true_false_dst[pred(x) ? 0 : 1]++ = x;
return result;
}
friend std::ostream& operator<<(std::ostream& os, const Array& self) {
for(auto& x : self) os << x << ", ";
return os;
}
};
template<class T, class... Ts>
Array(T, Ts...) -> Array<T, size_t(1) + sizeof...(Ts)>;
template<class T, T... vs>
Array(std::integer_sequence<T, vs...>) -> Array<T, sizeof...(vs)>;

How can I map a C++ parameter pack into a sequence of std::pair objects?

I have a variadic template function foo():
template <typename... Args>
void foo(Args &&... args);
This function is intended to be invoked with all arguments of size_t. I can enforce that using some metaprogramming. I need to take the resulting list of arguments two at a time and put them into a container of std::pair<size_t, size_t>. Conceptually, something like:
std::vector<std::pair<size_t, size_t> > = {
std::make_pair(args[0], args[1]),
std::make_pair(args[2], args[3]), ...
};
Is there a straightforward way to do this? I know that by pack expansion, I could put the arguments into a flat container, but is there a way to group them two by two into std::pair objects at the same time?
Indexing into packs isn't really doable (yet?), but indexing into tuples is. Just stick everything into a tuple first, and then pull everything back out as you go. Since everything's a size_t, we can just copy:
template <size_t... Is, class Tuple>
std::vector<std::pair<size_t, size_t>>
foo_impl(std::index_sequence<Is...>, Tuple tuple) {
return std::vector<std::pair<size_t, size_t> >{
std::make_pair(std::get<2*Is>(tuple), std::get<2*Is+1>(tuple))...
};
}
template <typename... Args>
void foo(Args... args)
{
auto vs = foo_impl(std::make_index_sequence<sizeof...(Args)/2>{},
std::make_tuple(args...));
// ...
}
Suppose you are allowed to refactor your logic into an internal helper function:
template <typename ...Args>
void foo(Args &&... args)
{
foo_impl(std::make_index_sequence<sizeof...(Args) / 2>(),
std::forward<Args>(args)...);
}
Now we can operate on the argument pack index by index:
template <std::size_t ...I, typename ...Args>
void foo_impl(std::index_sequence<I...>, Args &&... args)
{
std::vector<std::pair<std::size_t, std::size_t>> v =
{ GetPair(std::integral_constant<std::size_t, I>(), args...)... };
}
It remains to implement the pair extractor:
template <typename A, typename B, typename ...Tail>
std::pair<std::size_t, std::size_t> GetPair(std::integral_constant<std::size_t, 0>,
A a, B b, Tail ... tail)
{
return { a, b };
}
template <std::size_t I, typename A, typename B, typename ...Tail>
std::pair<std::size_t, std::size_t> GetPair(std::integral_constant<std::size_t, I>,
A a, B b, Tail ... tail)
{
return GetPair<I - 1>(tail...);
}
With range-v3, you may do
template <typename... Args>
void foo(Args&&... args)
{
std::initializer_list<std::size_t> nbs = {static_cast<std::size_t>(args)...};
const auto pair_view =
ranges::view::zip(nbs | ranges::view::stride(2),
nbs | ranges::view::drop(1) | ranges::view::stride(2));
// And possibly
std::vector<std::pair<std::size_t, std::size_t>> pairs = pair_view;
// ...
}
Demo
Someone (cough #Barry cough) said that indexing into packs isn't possible.
This is C++. Impossible means we just haven't written it yet.
template<std::size_t I> struct index_t:std::integral_constant<std::size_t, I> {
using std::integral_constant<std::size_t, I>::integral_constant;
template<std::size_t J>
constexpr index_t<I+J> operator+( index_t<J> ) const { return {}; }
template<std::size_t J>
constexpr index_t<I-J> operator-( index_t<J> ) const { return {}; }
template<std::size_t J>
constexpr index_t<I*J> operator*( index_t<J> ) const { return {}; }
template<std::size_t J>
constexpr index_t<I/J> operator/( index_t<J> ) const { return {}; }
};
template<std::size_t I>
constexpr index_t<I> index{};
template<std::size_t B>
constexpr index_t<1> exponent( index_t<B>, index_t<0> ) { return {}; }
template<std::size_t B, std::size_t E>
constexpr auto exponent( index_t<B>, index_t<E> ) {
return index<B> * exponent( index<B>, index<E-1> );
}
template<std::size_t N>
constexpr index_t<0> from_base(index_t<N>) { return {}; }
template<std::size_t N, std::size_t c>
constexpr index_t<c-'0'> from_base(index_t<N>, index_t<c>) { return {}; }
template<std::size_t N, std::size_t c0, std::size_t...cs>
constexpr auto from_base(index_t<N>, index_t<c0>, index_t<cs>...) {
return
from_base(index<N>, index<c0>) * exponent(index<N>, index<sizeof...(cs)>)
+ from_base(index<N>, index<cs>...)
;
}
template<char...cs>
constexpr auto operator""_idx(){
return from_base(index<10>, index<cs>...);
}
auto nth = [](auto index_in){
return [](auto&&...elems)->decltype(auto){
using std::get;
constexpr auto I= index<decltype(index_in){}>;
return get<I>(std::forward_as_tuple(decltype(elems)(elems)...));
};
};
Now we get:
using pair_vec = std::vector<std::pair<std::size_t, std::size_t>>;
template <typename... Args>
pair_vec foo(Args &&... args) {
return
index_over< sizeof...(args)/2 >()
([&](auto...Is)->pair_vec{
return {
{
nth( Is*2_idx )( decltype(args)(args)... ),
nth( Is*2_idx+1_idx )( decltype(args)(args)... )
}...
};
});
}
where we "directly" index into our parameter packs using compile time constant indexes.
live example.

Infinite recursive template instantiation when using Clang while GCC works fine?

the intention of the following c++ code is to wrap the ternary operator (?:) in a separate function, which later on will help with building a syntax tree.
Before looking at real c++ snippet let's take a quick look at what it does in pseudo code:
bool recursive(bool v) {
return v ? v : recursive(v);
}
int main() {
bool r = recursive(true)
}
Unfortunatly Clang has problems with terminating the recursion when the ternary operator (?:) is wrapped within a template function:
/****************** DECLARATIONS ******************/
template<typename T>
constexpr T
recursive(T t);
struct IfCase {
template<typename T>
constexpr T
operator()(T t) const;
};
struct ElseCase {
template<typename T>
constexpr T
operator()(T t) const;
};
#if defined(WORKS)
static constexpr bool
if_then_else_return(bool b, IfCase const& ic, ElseCase const& ec, bool x);
#else
template<typename T, typename IfFunctor, typename ElseFunctor>
static constexpr T
if_then_else_return(T b, IfFunctor const& ic, ElseFunctor const& ec, T x);
#endif
/****************** DEFINITIONS ******************/
template<typename T>
constexpr T
IfCase::operator()(T t) const {
return t;
}
template<typename T>
constexpr T
recursive(T t) {
return if_then_else_return(t, IfCase{}, ElseCase{}, t);
}
template<typename T>
constexpr T
ElseCase::operator()(T t) const {
return recursive(t);
}
#if defined(WORKS)
constexpr bool
if_then_else_return(bool b, IfCase const& ic, ElseCase const& ec, bool x) {
return b ? ic(x) : ec(x);
}
#else
template<typename T, typename IfFunctor, typename ElseFunctor>
constexpr T
if_then_else_return(T b, IfFunctor const& ic, ElseFunctor const& ec, T x) {
return b ? ic(x) : ec(x);
}
#endif
/****************** CALL ******************/
int main() {
constexpr auto r = recursive(true);
}
Build results:
g++ with reg. function (-DWORKS): OK
g++ with tmpl. function: OK
clang++ with reg. function (-DWORKS): OK (Find code & results also at Coliru)
clang++ with tmpl. function: FAIL (Find code & results also at Coliru)
GCC (4.9.2) compiles both variants without an error, but Clang (3.5 to 3.8) fails with the following error message:
main.cpp:56:14: fatal error: recursive template instantiation exceeded maximum depth of 256
return b ? ic(x) : ec(x);
^
/*** the next error messages for lines 64, 38 and 56 are repeated several times ***/
main.cpp:56:22: note: in instantiation of function template specialization 'ElseCase::operator()<bool>' requested here
return b ? ic(x) : ec(x);
^
main.cpp:38:9: note: in instantiation of function template specialization 'if_then_else_return<bool, IfCase, ElseCase>' requested here
return if_then_else_return(t, IfCase{}, ElseCase{}, t);
^
main.cpp:64:21: note: in instantiation of function template specialization 'recursive<bool>' requested here
constexpr auto r = recursive(true);
^
1 error generated.
But why? How can this code be rewritten so that Clang does not complain anymore?
Thank you very much in advance.
EDIT 1:
I've shorted the compiler message, hopefully increasing its readability. For a full backtrace, please take a look at those Coliru links provided above.
Removing the constexpr specifier will work around this Clang error. But this also reduces functionality and thus is not an option.
One workaround is to limit recursion yourself by adding a counter to the template arguments of the constructs participating in recursion, updating the counter on the recursive call, and using partial specialization to terminate recursion.
I made these changes to your program:
Changed IfCase and ElseCase (IfCase only for symmetry) to template classes instead of regular classes with a template member function. This allows partial specialization.
Added an integer counter template argument to ElseCase and recursive().
Incremented the counter when calling recursive() in ElseCase.
Created a partial specialization of ElseCase at an arbitrary recursion depth that does not make a recursive call. The maximum depth should be tuned to avoid the clang++ limit.
Here's the code:
#include <cassert>
/****************** DECLARATIONS ******************/
template<typename T, int N = 0>
constexpr T
recursive(T t);
template<typename T>
struct IfCase;
template<typename T, int N>
struct ElseCase;
#if defined(WORKS)
static constexpr bool
if_then_else_return(bool b, IfCase<bool> const& ic, ElseCase<bool> const& ec, bool x);
#else
template<typename T, typename IfFunctor, typename ElseFunctor>
static constexpr T
if_then_else_return(T b, IfFunctor const& ic, ElseFunctor const& ec, T x);
#endif
/****************** DEFINITIONS ******************/
template<typename T>
struct IfCase {
constexpr T
operator()(T t) const {
return t;
}
};
template<typename T, int N>
constexpr T
recursive(T t) {
return if_then_else_return(t, IfCase<T>{}, ElseCase<T, N>{}, t);
}
template<typename T, int N>
struct ElseCase {
constexpr T
operator()(T t) const {
return recursive<T, N + 1>(t);
}
};
static constexpr int MaxRecursionDepth = 10;
template<typename T>
struct ElseCase<T, MaxRecursionDepth> {
constexpr T
operator()(T t) const {
assert(false); // OK in C++14!
return t;
}
};
#if defined(WORKS)
constexpr bool
if_then_else_return(bool b, IfCase<bool> const& ic, ElseCase<bool> const& ec, bool x) {
return b ? ic(x) : ec(x);
}
#else
template<typename T, typename IfFunctor, typename ElseFunctor>
constexpr T
if_then_else_return(T b, IfFunctor const& ic, ElseFunctor const& ec, T x) {
return b ? ic(x) : ec(x);
}
#endif
/****************** CALL ******************/
int main() {
constexpr auto r = recursive(true);
}
It works at CoLiRu.
I was initially worried about how to detect an actual recursion depth error, as my original partially specialized class would silently return the wrong answer. Since you are using -std=c++14, assertions in constexpr functions are valid, which was news to me. I have updated the code to make use of this.
By using different code paths for runtime and compile time recursion I was able to work around endless recursion:
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/unpack.hpp>
#include <boost/hana/equal.hpp>
#include <type_traits>
#include <tuple>
#include <cassert>
namespace hana = boost::hana;
namespace detail {
/* std::forward_as_tuple(views...) is not constexpr */
template<typename... Xs>
static constexpr auto
forward_as_tuple(Xs&&... xs) {
return std::tuple<Xs...>{
std::forward<Xs>(xs)...
};
}
/* namespace detail */ }
template<typename Condition, typename LastStep, typename RecursionStep>
struct functor_t {
constexpr
functor_t(Condition const c, LastStep ls, RecursionStep rs) : c{c}, ls{ls}, rs{rs} {};
template <typename Args>
constexpr decltype(auto)
eval(std::true_type, Args const& args) const {
return hana::unpack(args, ls);
}
template <typename Args>
constexpr decltype(auto)
eval(std::false_type, Args const& args) const {
auto vt = hana::unpack(args, rs);
return eval( hana::unpack(vt, c), vt);
}
template <typename Args>
constexpr decltype(auto)
eval(hana::true_, Args const& args) const {
return hana::unpack(args, ls);
}
template <typename Args>
constexpr decltype(auto)
eval(hana::false_, Args const& args) const {
auto vt = hana::unpack(args, rs);
return eval( hana::unpack(vt, c), vt);
}
template <typename Args>
decltype(auto)
eval(bool const& b, Args const& args) const {
if (b) {
return hana::unpack(args, ls);
}
auto vt = hana::unpack(args, rs);
return eval(hana::unpack(vt, c), vt);
}
template <typename... Args>
constexpr decltype(auto)
operator()(Args&& ...args) const {
return eval( c(std::forward<Args>(args)...), detail::forward_as_tuple(args...) );
}
Condition const c;
LastStep ls;
RecursionStep rs;
};
struct recurse_t {
template <typename Condition, typename LastStep, typename RecursionStep>
constexpr decltype(auto)
operator()(Condition && c, LastStep && ls, RecursionStep && rs) const {
return functor_t<Condition, LastStep, RecursionStep>{c, ls, rs};
}
};
constexpr recurse_t recurse{};
/****************** TEST ******************/
#include <boost/hana/plus.hpp>
#include <boost/hana/minus.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/ext/std/tuple.hpp>
#include <tuple>
struct Condition {
template<typename I, typename S, typename J>
constexpr decltype(auto)
operator()(I const& i, S const& s, J const& j) const{
return (j == hana::int_c<1>);
}
};
struct LastStep {
template<typename I, typename S, typename J>
constexpr decltype(auto)
operator()(I const& i, S const& s, J const& j) const {
return hana::plus(s, i);
}
};
struct RecursionStep {
template<typename I, typename S, typename J>
constexpr decltype(auto)
operator()(I const& i, S const& s, J const& j) const {
return std::make_tuple(i, hana::plus(s,i), j-hana::int_c<1>);
}
};
int main() {
/* compute: 2*10 == 20 */
assert(recurse(Condition{}, LastStep{}, RecursionStep{})(2,0,10) == 20);
static_assert(recurse(Condition{}, LastStep{}, RecursionStep{})(hana::int_c<2>, hana::int_c<0>, hana::int_c<10>) == hana::int_c<20>, "");
assert(
recurse(
[](auto a, auto b, auto c) { return (a == 1); },
[](auto a, auto b, auto c) { return a+b; },
[](auto a, auto b, auto c) { return std::make_tuple(a, a+b, c-1); }
)(2,0,10) == 20
);
}

how to SFINAE for enabling member function returning `auto`

In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e.
template<int N> struct A {
int sum() const noexcept
{ return _sum<N-1>(); }
private:
int _data[N];
template<int I> typename std::enable_if< I,int>::type _sum() const noexcept
{ return _sum<I-1>() + _data[I]; }
template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept
{ return _data[I]; }
};
However, this won't work if the function in question (_sum() in above example) has auto-detected return type, such as _func() in this example
template<int N> class A
{
/* ... */
private:
// how to make SFINAE work for _func() ?
template<int I, typename BinaryOp, typename UnaryFunc>
auto _func(BinaryOp op, UnaryFunc f) const noexcept -> decltype(f(_data[0]))
{ return op(_func<I-1>(op,f),f(_data[I])); }
};
What else can be done to get SFINAE here?
Following David Rodríguez - dribeas, the following code worked as intended:
template<int N> class A
{
int min_abs() const noexcept
{
return _func<N-1>([](int x, int y)->int { return std::min(x,y); },
[](int x)->int { return std::abs(x); });
}
private:
int _data[N];
template<int I, typename BinaryOp, typename UnaryFunc>
auto _func(BinaryOp op, UnaryFunc f) const noexcept
-> typename std::enable_if< I>,decltype(f(_data[0]))>::type
{ return op(_func<I-1>(op,f),f(_data[I])); }
template<int I, typename BinaryOp, typename UnaryFunc>
auto _func(BinaryOp op, UnaryFunc f) const noexcept
-> typename std::enable_if<!I>,decltype(f(_data[0]))>::type
{ return f(_data[I]); }
};
strictly speaking, we must also ensure that the binary operator op returns the correct type. For simplicity and brevity of the answer, I leave that for the reader to figure out ...