【发布时间】: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。只需制作一个常规模板功能,然后继续您的生活。