You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
C++ Templates: How to Iterate through std::tuple: std::apply and More - C++ Stories
In the previous article on the tuple iteration, we covered the basics. As a result, we implemented a function template that took a tuple and could nicely print it to the output. There was also a version with operator <<.
Today we can go further and see some other techniques. The first one is with std::apply from C++17, a helper function for tuples.
@fenbf It compiles - you have just trivial error in functionname - see https://godbolt.org/z/WTscsnvnz
((printTupleApplyFn, not printTupleApply)) - missing Fn suffix
Everybody makes mistakes, including me ;) It should be > 0 not >= 0 in my if constexpr condition. But I noticed you skipped that part, a wise move - the more focused article, the better. BTW, nice book "C++17 in detail" - I have my own copy.
@PiotrNycz heh :) Having an empty tuple is probably very rare, maybe it could be even solved by having some special function template specialization to report a warning in that case... or ignore it. It's probably only needed for generic code in some libraries.
thanks for a good opinion about the book.
Here is a quite simple application, which mimics a stream of heterogeneous data and still works with the compatible sub-content. It uses what has been discussed in this post and if constexpr:
std::tuple tp{10, 20, 3.14, "blabla", 42};
auto mult = [](auto&& x) {
if constexpr (std::is_arithmetic_v<std::remove_cvref_t<decltype(x)>>) {
x *= 2;
}
};
printTuple(tp);
for_each_tuple(tp, mult);
printTuple(tp);
I tried to do this with concepts, but couldn't manage as it reports couldn't deduce template parameter 'Fn' on multiply function. See the snippet:
emplate <class T, class U>
concept Multipliable = requires(T a, U b) {
a*b;
};
template <class T, class U>
concept NotMultipliable = !Multipliable<T,U>;
template <class T> requires Multipliable<T, int>
void multiply(T&& x) {
x *= 2;
}
template <class T> requires NotMultipliable<T,int>
void multiply(T&& x) {}
...
for_each_tuple(tp, multiply);
Any ideas on how to accomplish this with concepts?
@tahsinkose In C++ a function template is not a thing you can pass as argument to other functions. You can pass function template instantiation (as well as objects, variables other functions (pointers to functions). Here -- you need to wrap this function template with lambda:
Activity
iPMSoftware commentedon Feb 14, 2022
Hi Bartlomiej. I was doing some iteration over tuple like last week too.
You can check it on https://github.com/iPMSoftware/CppFunAndLearn/blob/master/template_recursive_iteration_over_tuple/main.cpp
PiotrNycz commentedon Feb 14, 2022
This HelperCallable looks familiar, it looks like generic lambda,
So I'd just use lambda in such case:
But actually this is not a real problem -- since you have types (Args...) for printImpl to instantiate:
fenbf commentedon Feb 14, 2022
Thanks @iPMSoftware, I'll see your implementation. Do you have some different techniques used there?
Thanks @PiotrNycz - unfortunately even with Args... it doesn't compile:
std::apply(printImpl<Args...>, tp);see here https://godbolt.org/z/YWEn3YEMqPiotrNycz commentedon Feb 14, 2022
@fenbf It compiles - you have just trivial error in functionname - see https://godbolt.org/z/WTscsnvnz
((printTupleApplyFn, not printTupleApply)) - missing Fn suffix
fenbf commentedon Feb 14, 2022
@PiotrNycz ah, eh, wow! yes, that's the bug :) Thanks for pointing this out, I'll update the article
PiotrNycz commentedon Feb 14, 2022
It is always nice to help.
As a bonus, one idea how to print nicely all of these tuple elementa w/o extra additional lambda/function nor index:
fenbf commentedon Feb 15, 2022
Thanks @PiotrNycz I've just updated the article with a new section with that code.
PiotrNycz commentedon Feb 15, 2022
Everybody makes mistakes, including me ;) It should be
> 0not>= 0in myif constexprcondition. But I noticed you skipped that part, a wise move - the more focused article, the better. BTW, nice book "C++17 in detail" - I have my own copy.fenbf commentedon Feb 15, 2022
@PiotrNycz heh :) Having an empty tuple is probably very rare, maybe it could be even solved by having some special function template specialization to report a warning in that case... or ignore it. It's probably only needed for generic code in some libraries.
thanks for a good opinion about the book.
tahsinkose commentedon May 15, 2022
Here is a quite simple application, which mimics a stream of heterogeneous data and still works with the compatible sub-content. It uses what has been discussed in this post and
if constexpr:I tried to do this with
concepts, but couldn't manage as it reportscouldn't deduce template parameter 'Fn'onmultiplyfunction. See the snippet:Any ideas on how to accomplish this with concepts?
PiotrNycz commentedon May 16, 2022
@tahsinkose In C++ a function template is not a thing you can pass as argument to other functions. You can pass function template instantiation (as well as objects, variables other functions (pointers to functions). Here -- you need to wrap this function template with lambda:
Coding-dude12351 commentedon Nov 4, 2026
very interesting , i think i have work implement something that consolidate std::tuple, and std::apply