簡體   English   中英

Templatd lambda 調用模板化 lambda (C++20) 不適用於 clang 12 / 13

[英]Templatd lambda calling templated lambda (C++20) doesn't work with clang 12 / 13

考慮這段代碼:

#include <utility>
#include <functional>

using namespace std;

int main( int argc, char **argv )
{
    static
    auto lA = []<bool FLAG_A, bool FLAG_B>( unsigned a ) -> unsigned
    {
        return (unsigned)FLAG_A + FLAG_B + a;
    };
    static
    auto lB = []<bool FLAG_A, bool FLAG_B>( unsigned a ) -> unsigned
    {
        return lA.template operator ()<FLAG_A, FLAG_B>( a );
    };
    using fn_t = function<unsigned ( unsigned )>;
    fn_t fn = bind( &decltype(lB)::template operator ()<false, false>, &lB, placeholders::_1 );
}

使用 MSVC 2019 編譯沒有任何問題,但 clang 12 / 13 給出以下錯誤:

test.cpp(11,12): error: multiple overloads of '__invoke' instantiate to the same signature 'auto (unsigned int) const -> unsigned int'
        auto lA = []<bool FLAG_A, bool FLAG_B>( unsigned a ) -> unsigned
                  ^
test.cpp(11,12): note: in instantiation of member class '' requested here
test.cpp(21,42): note: in instantiation of function template specialization 'main(int, char **)::(anonymous class)::operator()<false, false>' requested here
        fn_t fn = bind( &decltype(lB)::template operator ()<false, false>, &lB, placeholders::_1 );
                                                ^
test.cpp(11,12): note: previous implicit declaration is here
        auto lA = []<bool FLAG_A, bool FLAG_B>( unsigned a ) -> unsigned
                  ^

gcc 11 也編譯代碼沒有任何錯誤。 有沒有一種方法可以使代碼與 clang 一起工作而無需任何精心設計的解決方法?

仍然有std::integral_constant (在你的情況下甚至是std::bool_constant )允許扣除:

static auto lA = []<bool FLAG_A, bool FLAG_B>(std::bool_constant<FLAG_A>,
                                              std::bool_constant<FLAG_B>,
                                              unsigned a ) -> unsigned
{
    return (unsigned)FLAG_A + FLAG_B + a;
};
static
auto lB = []<bool FLAG_A, bool FLAG_B>( unsigned a ) -> unsigned
{
    return lA(std::bool_constant<FLAG_A>{}, std::bool_constant<FLAG_B>{}, a);
};

演示

暫無
暫無

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

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