我注意到std :: is_convertible和std :: is_assignable在涉及std :: function object和std :: bind时出现了一些奇怪的结果.
我假设当这些函数返回true时,可以进行转换.或者我错过了什么?
以下代码在不同的编译器上打印不同的结果,我希望它打印0,因为这些类型无法分配.
#include <type_traits>
#include <functional>
#include <iostream>
int main()
{
std::cout << std::is_convertible<std::function<void(int)>, std::function<void()>>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它在以下编译器上打印0:
它在以下编译器上打印1:
有没有正确的答案?是编译器中的这些错误还是我搞乱编译器特定的东西?
我写了一些代码,在给定通用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: …
Run Code Online (Sandbox Code Playgroud)