Starting with some metaprogramming code:
template<class... Ts>
class list {}; //a generic container for a list of types
template<class in_list_type>
class front //get the type of the first template parameter
{
template<template<class...> class in_list_less_template_type, class front_type, class... rest_types>
static front_type deduce_type(in_list_less_template_type<front_type, rest_types...>*);
public:
typedef decltype(deduce_type((in_list_type*)nullptr)) type;
};
This code works fine for this:
typedef typename front<list<int, float, char>>::type type; //type is int
But fails to compile when the first item is a function type:
// no matching function for call to 'deduce_type'
typedef typename front<list<void (), float, char>>::type type;
I only have access to XCode at the moment and can't confirm whether this is simply an XCode bug. I'm using XCode 4.5.1, using Apple LLVM compiler 4.1.
When the template arguments to deduce_type are being deduced, front_type has void() as a candidate. However this would make deduce_type have type void ()() (function returning a function -- alias<void()>() if you assume template<typename T> using alias = T; to be in scope). This is an error and type deduction fails.
A solution is to have deduce_type return something like identity<front_type>, and type be an alias to typename decltype(deduce_type((in_list_type*)nullptr))::type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With