Skip to content

2022/tuple-iteration-apply/ #75

Open

Description

@utterances-bot

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.

https://www.cppstories.com/2022/tuple-iteration-apply/

Activity

  1. iPMSoftware commented on Feb 14, 2022

    @iPMSoftware

    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

  2. PiotrNycz commented on Feb 14, 2022

    @PiotrNycz

    This HelperCallable looks familiar, it looks like generic lambda,
    So I'd just use lambda in such case:

    auto printImpl = [](const auto&... tupleArgs) {
        size_t index = 0;
        auto printElem = [&index](const auto& x) {
            if (index++ > 0) 
                std::cout << ", ";
            std::cout << x;
            };
    
        (printElem(tupleArgs), ...);
    };
    

    But actually this is not a real problem -- since you have types (Args...) for printImpl to instantiate:

    template <typename... Args>
    void printTupleApplyFn(const std::tuple<Args...>& tp) {
        std::cout << "(";
        std::apply(printImpl<Args...>, tp); // (!)
        std::cout << ")";
    }
    
    
  3. fenbf commented on Feb 14, 2022

    @fenbf
    Owner

    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/YWEn3YEMq

  4. PiotrNycz commented on Feb 14, 2022

    @PiotrNycz

    @fenbf It compiles - you have just trivial error in functionname - see https://godbolt.org/z/WTscsnvnz
    ((printTupleApplyFn, not printTupleApply)) - missing Fn suffix

  5. fenbf commented on Feb 14, 2022

    @fenbf
    Owner

    @PiotrNycz ah, eh, wow! yes, that's the bug :) Thanks for pointing this out, I'll update the article

  6. PiotrNycz commented on Feb 14, 2022

    @PiotrNycz

    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:

    template <typename TupleT>
    void printTupleApply(const TupleT& tp) {
        std::cout << "(";
        if constexpr (std::tuple_size_v<TupleT> >= 0u)
            std::apply([](const auto& a0, const auto&... a1) {
                    std::cout << a0;
                    ((std::cout << ", " << a1), ...);
                }, tp);
        std::cout << ")";
    }
    
    
    
    
  7. fenbf commented on Feb 15, 2022

    @fenbf
    Owner

    Thanks @PiotrNycz I've just updated the article with a new section with that code.

  8. PiotrNycz commented on Feb 15, 2022

    @PiotrNycz

    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.

  9. fenbf commented on Feb 15, 2022

    @fenbf
    Owner

    @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.

  10. tahsinkose commented on May 15, 2022

    @tahsinkose

    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?

  11. PiotrNycz commented on May 16, 2022

    @PiotrNycz

    @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:

    for_each_tuple(tp, [] (auto&& ...args) { 
             multiply(std::forward<decltype(args)>(args)...); } );
    
  12. Coding-dude12351 commented on Nov 4, 2026

    @Coding-dude12351

    very interesting , i think i have work implement something that consolidate std::tuple, and std::apply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions