tex*_*xus 5 c++ lambda gcc generic-lambda c++14
我写了一些代码,在给定通用lambda函数时检索非auto参数的类型.正如您在下面的代码中看到的那样,我们的想法是使用通用lambda调用connect函数,并为auto参数提供参数(在我的用例中总是位于前面).所以在下面的代码中我的目标是检测第二个参数是float类型.
代码适用于clang 3.8,但它不能用gcc 6.1.1编译,所以我想知道这是否是gcc中的错误或者这是不是有效的c ++代码?我可以假设泛型lambda是使用模板化的operator()函数实现的,还是这个特定于编译器的?
template <typename Functor, typename... AllArgs, typename... ProvidedArgs>
void findArgTypes(void(Functor::*)(AllArgs...) const, Functor, ProvidedArgs...)
{
// AllArgs == int, float
// ProvidedArgs == int
}
template <typename Func, typename... ProvidedArgs>
void connect(Func func, ProvidedArgs... providedArgs)
{
findArgTypes(&Func::template operator()<ProvidedArgs...>, func, providedArgs...);
}
int main()
{
int tmp = 0;
connect([&](auto, float){ ++tmp; }, 0);
}
Run Code Online (Sandbox Code Playgroud)
gcc给出的错误是这样的:
main.cpp: In instantiation of ‘void connect(Func, ProvidedArgs ...) [with Func = main()::<lambda(auto:1, float)>; ProvidedArgs = {int}]’:
main.cpp:16:33: required from here
main.cpp:11:17: error: no matches converting function ‘operator()’ to type ‘void (struct main()::<lambda(auto:1, float)>::*)() const’
findArgTypes(&Func::template operator()<ProvidedArgs...>, func, providedArgs...);
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:16:27: note: candidate is: template<class auto:1> main()::<lambda(auto:1, float)>
connect([](auto, float){}, 0);
^
Run Code Online (Sandbox Code Playgroud)
删除constin findArgTypes会得到相同的结果.
使用以下代码适用于两个编译器:
struct Foo
{
template <typename T>
void operator()(T, float) const {}
};
int main()
{
Foo f;
connect(f, 0);
}
Run Code Online (Sandbox Code Playgroud)
你有错误,因为你期待函子(对象),但带有空捕获的 lambda 可转换为自由函数:
int main() {
using function = void (*)(int, float);
function a = [](auto, float){};
}
Run Code Online (Sandbox Code Playgroud)
对于您问题的最新版本,实现满足两个编译器的要求:
template <typename Func, typename... ProvidedArgs>
void connect(Func func, ProvidedArgs... providedArgs)
{
auto mf = &Func::template operator()<ProvidedArgs...>;
findArgTypes(mf, func, providedArgs...);
}
Run Code Online (Sandbox Code Playgroud)
我认为这是 gcc 编译器错误,gcc 需要这个auto局部变量才能正常工作......
顺便说一句,有一个问题 - clang 中的一个错误,gcc 中的一个错误 - 我真的建议你找到更简单的方法来实现你的目标 - 也许考虑只使用而std::function不是相当新鲜的泛型 lambda?
| 归档时间: |
|
| 查看次数: |
399 次 |
| 最近记录: |