c++ - 调用模板化 lambda (C++20) 的模板 lambda 不适用于 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);
};
演示。
推荐阅读
- python - AttributeError: 'super' object has no attribute '__getattr__' 在 Kivy 中使用带有多个 kv 文件的 BoxLayout 时出错
- groovy - 传递 Map 和使用 `body.resolveStrategy = Closure.DELEGATE_FIRST` 有什么区别
- angular - Angular 7 将路由器从 AppModule 传递到自定义库
- google-apps-script - pageElement 上的调用方法失败并出现错误“TypeError:找不到函数”
- intellij-idea - 如何将我的 IntelliJ IDEA 插件重置为默认值?
- java - 试图理解 RxJavaFX 的重点
- python - 如何在 for 循环中修改 Numpy 数组
- shell - 使用 shell 脚本将多个 csv 文件附加到单个 csv 文件中
- python-3.x - 如何使用 python aiohttp 连接到 .onion 站点?
- c - 有什么方法可以在 C 中使用带有可编辑默认文本的 scanf() 吗?