我有以下代码:
#include <iostream>
template<typename T>
struct A
{
T a;
T b;
static bool(*foo)(T, T);
};
template<>
bool(*A<int>::foo)(int, int) = [](int a, int b)->bool{ return a == b; };
struct B
{
int b;
};
template<typename T, typename U>
T bar(U(*func)(const int&)) { return func(23).b; }
int main()
{
A<int> a = {.a=1, .b=1};
std::cout << std::boolalpha << A<int>::foo(a.a, a.b) << std::endl;
std::cout << bar<int, B>([](const int& val) -> B { return {.b = val}; });
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 …