Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数模板取函数模板_C++_Templates_Lambda_C++ Concepts - Fatal编程技术网

C++ 函数模板取函数模板

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

我正在学习概念和模板。我正在尝试创建一个函数模板,它将调用另一个函数模板。目前,它可用于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)
{
    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);});