为什么类型扣除会失败(非销售)功能类型

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

本文关键字:类型 功能 失败 为什么      更新时间:2023-10-16

从某些元图代码开始:

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;
};

此代码为此工作正常:

typedef typename front<list<int, float, char>>::type type; //type is int

,但是当第一个项目是函数类型时未能编译:

// no matching function for call to 'deduce_type'
typedef typename front<list<void (), float, char>>::type type;

我目前只能访问XCode,无法确认这是否仅仅是Xcode错误。我正在使用Xcode 4.5.1,使用Apple LLVM编译器4.1。

推导到deduce_type的模板参数时,front_typevoid()作为候选人。但是,这将使deduce_type具有类型void ()()(函数返回功能 - alias<void()>()如果您假设template<typename T> using alias = T;处于范围中)。这是一个错误,类型扣除失败。

解决方案是让deduce_type返回identity<front_type>之类的东西,而typetypename decltype(deduce_type((in_list_type*)nullptr))::type的别名。