Re: variadic templates/unpacking pattern/perfect forwarding problem
On 2013-09-11 07:12, hansewetz@hotmail.com wrote:
Many thanks, I realized right after the post what was wrong ... but didn't
think of contraining the function to tuplets with enable_if<> (as you showed)
which has now been added:
template<typename Func,typename Tuple>
typename std::enable_if<is_tuple<
typename std::remove_cv<
typename std::remove_reference<Tuple>::type>::type>::value>::type
applyWithIndex(Func f,Tuple&&t){
typedef typename std::remove_cv<typename std::remove_reference<Tuple>::type>::type TupleNonRef;
static const int N=std::tuple_size<TupleNonRef>::value;
ApplyWithIndex<N,Func,Tuple>::apply(f,std::forward<Tuple>(t));
}
It might be worth adding in this context, that this kind of constraint
is even more useful, if you might want to extend the idea of what *is* a
tuple in the future. For example, the standard specification of
std::tuple_cat (20.4.2.4 [tuple.creation] p9),
template <class... Tuples>
constexpr tuple<CTypes ...> tuple_cat(Tuples&&... tpls);
was intentionally formed to allow the extension of the notion of "Tuple"
in the future, see especially for the non-normative wording in p13:
"Note: An implementation may support additional types in the parameter
pack Tuples that support the tuple-like protocol, such as pair and array."
At the current moment any "Tuple" that is used as argument of
std::tuple_cat that is not a std::tuple specialization is undefined
behaviour for a general program, but if your implementation documents
that it will for example support std::pair and std::array (or some other
types satisfying some extended protocol), you can use them as well.
There was a very late request to specify a "TupleLike" requirement set
for the Standard Library,
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2975.pdf
but it didn't make it into C++11 due to the lateness of that proposal. I
can only recommend everyone who is interested in bringing something like
a TupleLike requirement set into the standard, to write a fresh new
library proposal for this, see
http://isocpp.org/std/submit-a-proposal
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]