簡體   English   中英

函數模板作為成員 - GCC 與 CLANG

[英]function template as member - GCC vs. 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>);
}

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

那么這是GCC的錯誤嗎? 為什么如果變量是參數(在make_function和構造函數中)它會make_function ,但如果它是成員變量則不起作用? 衰減(注釋掉)是否在正確的位置,還是應該將其移動到make_function 如果我傳遞函數 ( f1 ) 或函數模板 ( f2 ) 的顯式實例化,為什么會有所不同?

正如評論者指出的那樣,這是 Clang 中的一個錯誤。

代碼開始使用 GCC 4.7.1(甚至 4.7?)和 Clang 3.7 進行編譯:在GodBolt查看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM