Search
 
SCRIPT & CODE EXAMPLE
 

CPP

iterator on std::tuple

#include <tuple>
#include <iostream>

int main()
{
    std::tuple t{42, 'a', 4.2}; // Another C++17 feature: class template argument deduction
    std::apply([](auto&&... args) {((std::cout << args << '
'), ...);}, t);
}
Comment

iterator on std::tuple

{
    auto tup = std::make_tuple(0, 'a', 3.14);
    template for (auto elem : tup)
        std::cout << elem << std::endl;
}
Comment

iterator on std::tuple

std::apply([](auto ...x){std::make_tuple(x.do_something()...);} , the_tuple);
Comment

iterator on std::tuple

#include <tuple>
#include <iostream>
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>

struct Foo1 {
    int foo() const { return 42; }
};

struct Foo2 {
    int bar = 0;
    int foo() { bar = 24; return bar; }
};

int main() {
    using namespace std;
    using boost::hana::for_each;

    Foo1 foo1;
    Foo2 foo2;

    for_each(tie(foo1, foo2), [](auto &foo) {
        cout << foo.foo() << endl;
    });

    cout << "foo2.bar after mutation: " << foo2.bar << endl;
}
Comment

iterator on std::tuple

template <size_t ...I>
struct index_sequence {};

template <size_t N, size_t ...I>
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};

template <size_t ...I>
struct make_index_sequence<0, I...> : public index_sequence<I...> {};
Comment

iterator on std::tuple

#include <tuple>
#include <utility>

template<std::size_t N>
struct tuple_functor
{
    template<typename T, typename F>
    static void run(std::size_t i, T&& t, F&& f)
    {
        const std::size_t I = (N - 1);
        switch(i)
        {
        case I:
            std::forward<F>(f)(std::get<I>(std::forward<T>(t)));
            break;

        default:
            tuple_functor<I>::run(i, std::forward<T>(t), std::forward<F>(f));
        }
    }
};

template<>
struct tuple_functor<0>
{
    template<typename T, typename F>
    static void run(std::size_t, T, F){}
};
Comment

iterator on std::tuple

// prints every element of a tuple
template<size_t I = 0, typename... Tp>
void print(std::tuple<Tp...>& t) {
    std::cout << std::get<I>(t) << " ";
    // do things
    if constexpr(I+1 != sizeof...(Tp))
        print<I+1>(t);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: string hex to int c++ 
Cpp :: arduino sprintf float 
Cpp :: get current directory cpp 
Cpp :: vhdl integer to std_logic_vector 
Cpp :: modify file cpp 
Cpp :: c++ usleep() 
Cpp :: add arbitrum to metamask 
Cpp :: c++ delete directory 
Cpp :: c++ bold text 
Cpp :: how to initialized a 2d vector 
Cpp :: compute the average of an array c++ 
Cpp :: eosio parse string 
Cpp :: c++ edit another processes memory address 
Cpp :: qimage transformed 
Cpp :: collections c# vs c++ 
Cpp :: file handling 
Cpp :: cpp iterate words of string 
Cpp :: ostream was not declared in this scope 
Cpp :: temporary mobile number 
Cpp :: max three values c++ 
Cpp :: cannot find "-lsqlite3" C++ 
Cpp :: c++ random number generator uniform distribution 
Cpp :: c++ split string by space into vector 
Cpp :: how to make crypto 
Cpp :: binary string addition 
Cpp :: all of the stars lyrics 
Cpp :: minimum spanning trees c++ 
Cpp :: c++ array loop 
Cpp :: how to split a string into words c++ 
Cpp :: how to iterate from second element in map c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =