功能模板作为成员 - GCC与CLANG

csc*_*wan 8 c++ c++11 clang++

请考虑以下代码:

#include <type_traits>
#include <utility>

template <typename F>
class function
{
public:
//  using function_type = typename std::decay<F>::type;
    using function_type = F;

    function(F func)
        : function_(func)
    {
    }

private:
     function_type function_;
};

template <typename F>
function<F> make_function(F&& func)
{
    return function<F>(std::forward<F>(func));
}

double f1(double)
{
    return 0.0;
}

template <typename T>
T f2(T)
{
    return T();
}

int main()
{
    // works in both cases
    make_function(f1);

    // needs decay (with CLANG)
    make_function(f2<double>);
}
Run Code Online (Sandbox Code Playgroud)

该类function旨在成为任何一个简单的包装器Callable.代码用GCC编译好(我从git存储库测试了4.9.2和7.0.0 20160427).然而,clang(3.5.0)抱怨:

function.cpp:17:17: error: data member instantiated with function type 'function_type' (aka 'double (double)')
         function_type function_;
                       ^
function.cpp:55:2: note: in instantiation of template class 'function<double (double)>' requested here
        make_function(f2<double>);
Run Code Online (Sandbox Code Playgroud)

这是GCC的错误吗?如果变量是一个参数(在make_function和构造函数中),为什么它可以工作,但如果它是一个成员变量呢?腐烂(已经整理出来)是在正确的地方还是应该将其移入make_function?如果我传递函数(f1)或函数模板(f2)的显式瞬时,为什么会有所不同?

ein*_*ica 1

正如评论者所指出的,这是 Clang 中的一个错误。

该代码开始使用 GCC 4.7.1(甚至可能是 4.7?)和 Clang 3.7 进行编译:请参阅GodBolt.