Skip to content

Instantly share code, notes, and snippets.

@sean-gilliam
Created August 28, 2024 17:59
Show Gist options
  • Save sean-gilliam/1ccd6f3ec550198f614cb6193fa4def5 to your computer and use it in GitHub Desktop.
Save sean-gilliam/1ccd6f3ec550198f614cb6193fa4def5 to your computer and use it in GitHub Desktop.
C++ - Filtering tuple by type
#include <iostream>
#include <vector>
#include <tuple>
#include <typeinfo>
template<class... Tp>
std::vector<int*> GetData(std::tuple<Tp...>&& t)
{
std::vector<int*> accumulator;
std::apply([&] (const auto&... tupleArgs)
{
auto processTuple = [&](const auto& x)
{
//int* s;
//std::cout << typeid(x).name() << std::endl;
//std::cout << typeid(s).name() << std::endl;
//if constexpr (std::is_same<decltype(x), int*>::value)
if constexpr (std::is_same<typename std::decay<decltype(x)>::type,int*>::value)
accumulator.push_back(x);
};
(processTuple(tupleArgs), ...);
}, t);
return accumulator;
}
int main()
{
int*i;int*p; int k = 6; int q = 9; i = &k; p = &q;
std::vector<int*> vector = GetData(std::make_tuple(i, "hello", p));
for (const auto v : vector)
std::cout << *v << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment