简体   繁体   中英

Why does type deduction fail on (non-pointer-to) function types

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM