64,661
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename TYPE>
struct CallBackBase
{
bool operator()(const TYPE &ele)
{
return true;
}
};
/*
将下面的struct 定义在函数Proc_All_1()中会出现下面的错误。
error C2918: 'CallBack1' : illegal use of local type in template instantiation
*/
struct CallBack1 : public CallBackBase <int>
{
bool operator()(const int user)
{
return true;
}
};
void ProcAll_1(vector<int> users)
{
for_each(users.begin(), users.end(),CallBack1());
}
int main()
{
// vector<int> v;
return 0;
}
void ProcAll_1(vector<int> users)
{
struct CallBack1 : public CallBackBase <int>
{
bool operator()(const int user)
{
return true;
}
};
for_each(users.begin(), users.end(),CallBack1());
}
这样的确有问题。
There was an attempt to generate a template function (that is, a function generated from a function template) based on a local type. A template can be instantiated only using a type with external linkage.
The following code will generate this error:
template<class T> void f(T t) {};
void g()
{
struct X {};
X x;
f(x); // error
}