【问题标题】:Templatd lambda calling templated lambda (C++20) doesn't work with clang 12 / 13调用模板化 lambda (C++20) 的模板 lambda 不适用于 clang 12 / 13
【发布时间】:2021-10-11 16:11:05
【问题描述】:

考虑这段代码:

#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?

【问题讨论】:

  • Clang 13 工作 here(clang 12.0.1 产生 ICE)。
  • 哦,你是对的!仅适用于符合 MSVC 的版本 clang-cl。
  • @BonitaMontero:仅供参考:如果你必须这样做:decltype(lB)::template operator (),你根本不应该使用 lambda。只需制作一个常规模板功能,然后继续您的生活。

标签: c++ c++20


【解决方案1】:

还有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);
};

Demo.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2021-05-12
    • 2021-05-16
    • 2022-01-14
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多