LoginSignup
5
0

More than 5 years have passed since last update.

std::tupleに関数を格納して回してみた

Last updated at Posted at 2018-07-23

はじめに

templateを使って、std::tupleに関数を格納して回してみた(タイトルママ)

やったこと

C++ の std::tuple で動的にループする

静的ポリモーフィズムの安全で簡単な実装 -動的から静的にしてパフォーマンス向上-のこのコメント

を参考に書いてみた

C++
#include <iostream>
#include <vector>
#include <functional>
#include <tuple>

template <typename Func, typename ...Args>
void func_each(std::tuple<Args...> const& args, Func func){
    std::apply([&](auto... arg) constexpr{(func(arg), ...);}, args);
}

void func1(){
    std::cout << "func1" << std::endl;
}

void func2(){
    std::cout << "func2" << std::endl;
}

int main(){

    auto var = std::tuple(&func1, &func2);

    func_each(var, [&](auto it){it();});

    return 0;   
}

実際のコード

参考

C++ の std::tuple で動的にループする

静的ポリモーフィズムの安全で簡単な実装 -動的から静的にしてパフォーマンス向上-のこのコメント

C++のパラメータパック基礎&パック展開テクニック

5
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0