从函数返回 ::iterator 不编译
Returning ::iterator from function does not compile
以下 repo 编译正常:
#include <tuple>
#include <vector>
class Foo
{
public:
typedef std::tuple<int, int> foo_type;
std::vector<foo_type>::iterator Find() {
return Listeners.begin();
}
std::vector<foo_type> Listeners;
};
int main()
{
Foo foo;
auto i = foo.Find();
}
但是当我在 P、Q 上模板化元组时,如以下 repo 所示,我收到此错误:
syntax error : missing ';' before identifier 'Find'
#include <tuple>
#include <vector>
template<typename P, typename Q>
class Foo
{
public:
typedef std::tuple<P, Q> foo_type;
std::vector<foo_type>::iterator Find() {
return Listeners.begin();
}
std::vector<foo_type> Listeners;
};
int main()
{
Foo<int, int> foo;
auto i = foo.Find();
}
您在这里缺少 typename
:
typename std::vector<foo_type>::iterator Find() {
^^^^^^^^
return Listeners.begin();
}
有关更详细的说明,请参阅 Where and why do I have to put the "template" and "typename" keywords?
以下 repo 编译正常:
#include <tuple>
#include <vector>
class Foo
{
public:
typedef std::tuple<int, int> foo_type;
std::vector<foo_type>::iterator Find() {
return Listeners.begin();
}
std::vector<foo_type> Listeners;
};
int main()
{
Foo foo;
auto i = foo.Find();
}
但是当我在 P、Q 上模板化元组时,如以下 repo 所示,我收到此错误:
syntax error : missing ';' before identifier 'Find'
#include <tuple>
#include <vector>
template<typename P, typename Q>
class Foo
{
public:
typedef std::tuple<P, Q> foo_type;
std::vector<foo_type>::iterator Find() {
return Listeners.begin();
}
std::vector<foo_type> Listeners;
};
int main()
{
Foo<int, int> foo;
auto i = foo.Find();
}
您在这里缺少 typename
:
typename std::vector<foo_type>::iterator Find() {
^^^^^^^^
return Listeners.begin();
}
有关更详细的说明,请参阅 Where and why do I have to put the "template" and "typename" keywords?