Skip to content

Instantly share code, notes, and snippets.

@Newlifer
Created July 20, 2016 10:52
Show Gist options
  • Save Newlifer/caae72b0581feecd3559be6caa0d7d3f to your computer and use it in GitHub Desktop.
Save Newlifer/caae72b0581feecd3559be6caa0d7d3f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <type_traits>
#include <utility>
#include <tuple>
struct bar {};
struct foo
{
template<int N, typename Self> struct field_data {};
int id;
template<typename Self>
struct field_data<0, Self>
{
Self& self;
typedef Self self_type;
typedef int self_data_type__;
field_data(Self& self) : self(self) {}
};
float other_id;
template<typename Self>
struct field_data<1, Self>
{
Self& self;
typedef Self self_type;
typedef float self_data_type__;
field_data(Self& self) : self(self) {}
};
/*
bar other_id;
template<typename Self>
struct field_data<2, Self>
{
Self& self;
typedef Self self_type;
typedef bar self_data_type__;
field_data(Self& self) : self(self) {}
};
*/
};
template<typename Target, typename Subject, int N, typename = void>
struct equal_type_with_index_b : public std::false_type
{
};
template<typename Target, typename Subject, int N>
struct equal_type_with_index_b
<
Target
,Subject
,N
,typename std::enable_if
<
std::is_same
<
Subject
,typename Target::template field_data
<
N
,Target
>::self_data_type__
>::value>::type
> : public std::true_type
{
};
struct dummy
{
static const int result = -1;
};
template<int I>
struct index_t
{
static const int result = I;
};
template<typename Target, typename Subject, int N, typename = void>
struct static_search
{
static const int result = -1;
};
template<typename Target, typename Subject, int N>
struct static_search<Target, Subject, N, typename std::enable_if< !equal_type_with_index_b<Target, Subject, N>::value >::type>
{
static const int result = static_search<Target, Subject, N-1>::result;
};
template<typename Target, typename Subject, int N>
struct static_search<Target, Subject, N, typename std::enable_if< equal_type_with_index_b<Target, Subject, N>::value >::type>
{
static const int result = N;
};
auto main() -> int
{
std::cout <<
"int type: " << static_search<foo, int, 2>::result
<< "\n" <<
"float type: " << static_search<foo, float, 2>::result
<< std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment