2

考虑以下代码和apply函数:

// Include
#include <iostream>
#include <array>
#include <type_traits>
#include <sstream>
#include <string>
#include <cmath>
#include <algorithm>

// Just a small class to illustrate my question
template <typename Type, unsigned int Size>
class Array
{
    // Class body (forget that, this is just for the example)
    public:
        template <class... Args> Array(const Args&... args) : _data({{args...}}) {;}
        inline Type& operator[](unsigned int i) {return _data[i];}
        inline const Type& operator[](unsigned int i) const {return _data[i];}
        inline std::string str() 
        { 
            std::ostringstream oss; 
            for (unsigned int i = 0; i < Size; ++i) 
                oss<<((*this)[i])<<" "; 
            return oss.str();
        }
    protected:
        std::array<Type, Size> _data;
    // Apply declaration
    public:
        template <typename Return, 
                  typename SameType, 
                  class... Args, 
                  class = typename std::enable_if<std::is_same<typename std::decay<SameType>::type, Type>::value>::type> 
        inline Array<Return, Size> apply(Return (*f)(SameType&&, Args&&...), const Array<Args, Size>&... args) const;
}; 

// Apply definition
template <typename Type, unsigned int Size>
template <typename Return, typename SameType, class... Args, class> 
inline Array<Return, Size> Array<Type, Size>::
apply(Return (*f)(SameType&&, Args&&...), const Array<Args, Size>&... args) const
{
    Array<Return, Size> result; 
    for (unsigned int i = 0; i < Size; ++i) {
        result[i] = f((*this)[i], args[i]...);
    }
    return result;
}

// Example
int main(int argc, char *argv[])
{
    Array<int, 3> x(1, 2, 3);
    std::cout<<x.str()<<std::endl;
    std::cout<<x.apply(std::sin).str()<<std::endl;
    return 0;
}

编译失败:

universalref.cpp: In function ‘int main(int, char**)’:
universalref.cpp:45:32: erreur: no matching function for call to ‘Array<int, 3u>::apply(<unresolved overloaded function type>)’
universalref.cpp:45:32: note: candidate is:
universalref.cpp:24:200: note: template<class Return, class SameType, class ... Args, class> Array<Return, Size> Array::apply(Return (*)(SameType&&, Args&& ...), const Array<Args, Size>& ...) const [with Return = Return; SameType = SameType; Args = {Args ...}; <template-parameter-2-4> = <template-parameter-1-4>; Type = int; unsigned int Size = 3u]
universalref.cpp:24:200: note:   template argument deduction/substitution failed:
universalref.cpp:45:32: note:   mismatched types ‘SameType&&’ and ‘long double’
universalref.cpp:45:32: note:   mismatched types ‘SameType&&’ and ‘float’
universalref.cpp:45:32: note:   mismatched types ‘SameType&&’ and ‘double’
universalref.cpp:45:32: note:   couldn't deduce template parameter ‘Return’

我不太确定它为什么会失败。在该代码中,我想:

  • 保持通用引用具有最通用的功能
  • 能够使用语法apply(std::sin)

那么我必须改变什么才能使其编译?

4

1 回答 1

1

function存在多个重载std::sin,并且您的函数模板无法推导出一个会产生完全匹配的重载(在大多数情况下,推导模板参数时不考虑任何转换)。

首先,这些overlads 中没有一个人接受对他们的第一个(也是唯一一个)论点的引用。编译器告诉你这一点,所以Return不能SameType从参数的类型中推断出来f

universalref.cpp:24:200: note:   template argument deduction/substitution failed:
universalref.cpp:45:32: note:   mismatched types ‘SameType&&’ and ‘long double’
universalref.cpp:45:32: note:   mismatched types ‘SameType&&’ and ‘float’
universalref.cpp:45:32: note:   mismatched types ‘SameType&&’ and ‘double’

因此,第一步包括更改apply()函数模板的签名:

[...] apply(Return (*f)(SameType, Args&&...), [...]
                        ^^^^^^^^
                        No URef!

此外,SFINAE 正在消除您的函数模板的所有实例,其中Return没有被推断为int(您正在调用apply()的实例Array<int, 3>):不幸的是,现有的重载都std::sin没有int作为返回类型。

您可以尝试将您的实例化更改为Array<double, 3>,但不幸的是,仅此一项无济于事,因为 C++11 标准的第 14.8.2/5 段规定:

推断的上下文是:

— 使用限定 ID 指定的类型的嵌套名称说明符。

— 非类型模板参数或绑定的数组,其中子表达式引用模板参数。

— 用于函数形参的形参类型中的模板形参,该形参有一个默认实参,该实参在进行实参推导的调用中使用。

—由于关联的函数参数是一个函数或一组重载函数(13.4),因此无法对其进行参数推导的函数参数,并且适用以下一个或多个:

— 多个函数与函数参数类型匹配(导致推理不明确),或

— 没有函数与函数参数类型匹配,或者

作为参数提供的一组函数包含一个或多个函数模板

由于需要支持整数类型的重载(这是 C+11 中的新功能,请参阅 26.8/11),您的库实现很可能确实std::sin. 这就是 stdlibc++ 的定义:

template<typename _Tp>
inline _GLIBCXX_CONSTEXPR
typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
                                double>::__type
sin(_Tp __x)
{ return __builtin_sin(__x); }

有办法离开这里,但你可能不喜欢它们。除了上述必要(但不足)的更改之外,您还需要显式std::sin转换为所需的类型。我还将在这里删除 SFINAE 条件,因为它除了强制SameType等于Type

// Declaration in the `Array` class
template <typename Return, class... Args>
inline Array<Return, Size> apply(
    Return (*f)(Type, Args&&...), 
    const Array<Args, Size>&... args
    ) const;

[...]

// Example
int main(int argc, char *argv[])
{
    Array<double, 3> x(1, 2, 3);
    //    ^^^^^^
    //    NOTICE
    //     THIS

    std::cout<<x.str()<<std::endl;

    std::cout<<x.apply((double(*)(double))std::sin).str()<<std::endl; // OK
    //                 ^^^^^^^^^^^^^^^^^^^
    //                     NOTICE THIS

    return 0;
}
于 2013-02-22T19:56:20.107 回答