When parsing a file, I want to introduce type checking. Decided to create a list of types with indexing
template<typename... Elements>
class TypeList {};
using MyTypes = TypeList<int, double, float/* etc */>;
/********/
template<typename List>
class FrontT;
template<typename Head, typename... Tail >
class FrontT<TypeList<Head, Tail...>>
{
public:
using Type = Head;
};
template<typename List>
using Front = typename FrontT<List>::Type;
/*********/
template<typename List>
class PopFrontT;
template<typename Head, typename... Tail>
class PopFrontT<TypeList<Head, Tail...>>
{
public:
using Type = TypeList<Tail... >;
};
template<typename List >
using PopFront = typename PopFrontT<List > ::Type;
/********/
template<typename List, unsigned N >
class GetNthType : public GetNthType <PopFront<List>, N - 1 >{};
template<typename List>
class GetNthType<List, 0> : public FrontT<List>{};
template<typename List, unsigned N>
using GetNthTypeT = typename GetNthType<List, N >::Type;
Next, I try to write the following function 'my_common_with', but it doesn't compile. How to select an compile-time index from an input string?
static constexpr unsigned INT_ID = 0;
static constexpr unsigned DOUBLE_ID = 1;
static constexpr unsigned FLOAT_ID = 2;
constexpr unsigned get_by_string(const std::string& s1)
{
if (s1 == "int")
return INT_T;
if (s1 == "double")
return DOUBLE_ID;
if (s1 == "float")
return FLOAT_ID;
return INT_T;
}
constexpr bool my_common_with(const std::string& s1, const std::string& s2)
{
return std::common_with<
GetNthTypeT<MyTypes, get_by_string(s1)>,
GetNthTypeT<MyTypes, get_by_string(s2)>
>;
}
thanks for the help