C++ 函数模板取函数模板
我正在学习概念和模板。我正在尝试创建一个函数模板,它将调用另一个函数模板。目前,它可用于lambda,但不能用于普通函数C++ 函数模板取函数模板,c++,templates,lambda,c++-concepts,C++,Templates,Lambda,C++ Concepts,我正在学习概念和模板。我正在尝试创建一个函数模板,它将调用另一个函数模板。目前,它可用于lambda,但不能用于普通函数 // This also works with lambda but not the normal function: //void callFunc(std::regular_invocable<T> auto invocable) template<typename T> void callFunc(auto invocable) { i
// This also works with lambda but not the normal function:
//void callFunc(std::regular_invocable<T> auto invocable)
template<typename T>
void callFunc(auto invocable)
{
invocable(T(5));
}
// Doesn't work, whether I use "T" or "auto"
//template<typename T>
void testFuncToPass(const auto& a)
{
std::cout << a << " CALLED\n";
}
//...
auto lambda = [](const auto& a){std::cout << a << " CALLED\n";};
//callFunc<int>(testFuncToPass); // ERROR
callFunc<int>([](const auto& a){std::cout << a << " CALLED\n";});
callFunc<int>(lambda);
//这也适用于lambda,但不适用于普通函数:
//void callFunc(标准::常规发票自动发票)
模板
void callFunc(自动开票)
{
发票(T(5));
}
//不管我用“t”还是“自动”都不行
//模板
void testFuncToPass(常量自动&a)
{
你的印象是
auto lambda = [](const auto& a){std::cout << a << " CALLED\n";};
template<typename T>
void lambda(const T& a) { std::cout << a << " CALLED\n"; }
struct SomeType {
template<typename T>
void operator()(const T& a) const { std::cout << a << " CALLED\n"; }
};
SomeType lambda;
callFunc<int>([](auto const& a){testFuncToPass(a);});