两阶段查找 [英] Two-Phase lookup

查看:63
本文介绍了两阶段查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我刚刚第一次遇到两阶段查询而且我很尴尬。两阶段查找背后的想法很容易理解,但我有一个案例无法编译,虽然

在我看来它应该是。


模板< typename TYPE>

void

foo(std :: map< int,TYPE pmap)

{

std :: map< int,TYPE> :: iterator iter;

}


使用g ++ 4.0.2编译时出现以下错误:

test.cc:在函数''void foo(std :: map< int,TYPE,std :: less< int>,

std :: allocator< std :: pair< ; const int,TYPE>)'':

test.cc:31:错误:预期`;''在''iter'之前''

test.cc:在函数''void foo(std :: map< int,TYPE,std :: less< int>,

std :: allocator< std :: pair< const int,TYPE>)[ with TYPE = char]'':

test.cc:42:从这里实例化

test.cc:31:错误:依赖名称

''std :: map< int,TYPE,std :: less< int> ;,std :: allocator< s td :: pair< const int,

TYPE> :: iterator' '被解析为非类型,但是实例化产生

a类型

test.cc:31:注意:说''typename

std :: map< int,TYPE,std :: less< int> ;,std :: allocator< st d :: pair< const int,

TYPE> :: iterator''如果是一个类型


除非我弄错了,这应该是一个从属名称查找,因此

在模板实例化之前不要被查询。


根据这篇文章:
http://www.codeproject.com/cpp/TwoPh...&select=949325


它应该是依赖的,因为它的类型是依赖的。这使得

很有意义。


鉴于这一切,导致这个''被解析为非类型''错误?


这纯粹是一个学术问题,但是我不能想出来的东西。


谢谢,

Paul Davis

Hi,

I''ve just had my first encounter with two-phase lookup and I''m
scratching my head a bit. The idea behind two phase look up is pretty
easy to understand, but I have a case that fails to compile although
it appears to me that it should.

template<typename TYPE>
void
foo( std::map< int, TYPE pmap )
{
std::map< int, TYPE >::iterator iter ;
}

When compiled with g++ 4.0.2 gives the following error:

test.cc: In function ''void foo(std::map<int, TYPE, std::less<int>,
std::allocator<std::pair<const int, TYPE >)'':
test.cc:31: error: expected `;'' before ''iter''
test.cc: In function ''void foo(std::map<int, TYPE, std::less<int>,
std::allocator<std::pair<const int, TYPE >) [with TYPE = char]'':
test.cc:42: instantiated from here
test.cc:31: error: dependent-name
''std::map<int,TYPE,std::less<int>,std::allocator<s td::pair<const int,
TYPE >::iterator'' is parsed as a non-type, but instantiation yields
a type
test.cc:31: note: say ''typename
std::map<int,TYPE,std::less<int>,std::allocator<st d::pair<const int,
TYPE >::iterator'' if a type is meant

Unless I''m mistaken, this should be a dependant name lookup, and thus
not be looked up until template instantiation.

According to this article:
http://www.codeproject.com/cpp/TwoPh...&select=949325

It should be dependant because its type is dependant. Which makes a
great deal of sense.

Given all that, what''s causing this ''parsed as non-type'' error?

This is purely an academic question, but its something I can''t figure
out.

Thanks,
Paul Davis

推荐答案

2007年2月7日下午3:56,文章
11 ********* ************@v45g2000cwv.googlegroups.c om

" pa *************** @ gmail.com" < pa *************** @ gmail.comwrote:
On 2/7/07 3:56 PM, in article
11*********************@v45g2000cwv.googlegroups.c om,
"pa***************@gmail.com" <pa***************@gmail.comwrote:

>

I 我刚刚第一次遇到两阶段查询,而且我很尴尬地抓住了我的脑袋。两阶段查找背后的想法很容易理解,但我有一个案例无法编译,虽然

在我看来它应该是。


模板< typename TYPE>

void

foo(std :: map< int,TYPE pmap)

{

std :: map< int,TYPE> :: iterator iter;

}


使用g ++ 4.0.2编译时出现以下错误:

test.cc:在函数''void foo(std :: map< int,TYPE,std :: less< int>,

test.cc:31:错误:预期`;''之前''iter''


除非我弄错了,这应该是一个依赖名字查找,因此

不被看直到模板实例化。
>
I''ve just had my first encounter with two-phase lookup and I''m
scratching my head a bit. The idea behind two phase look up is pretty
easy to understand, but I have a case that fails to compile although
it appears to me that it should.

template<typename TYPE>
void
foo( std::map< int, TYPE pmap )
{
std::map< int, TYPE >::iterator iter ;
}

When compiled with g++ 4.0.2 gives the following error:

test.cc: In function ''void foo(std::map<int, TYPE, std::less<int>,
test.cc:31: error: expected `;'' before ''iter''

Unless I''m mistaken, this should be a dependant name lookup, and thus
not be looked up until template instantiation.



编译器在

类模板的定义中执行两阶段名称查找,嵌套类模板的类,类的成员

模板,或类模板的嵌套类的成员 - 但不在像$ f $(*)的b $ ba函数模板中在上面的示例中。


Greg

The compiler performs "two phase" name-lookup within the definition of a
class template, a nested class of a class template, a member of a class
template, or a member of a nested class of a class template - but not within
a function template like foo() in the example above.

Greg


2月8日,03:33,Greg Herlihy< gre ... @ pacbell.netwrote:
On 8 Feb., 03:33, Greg Herlihy <gre...@pacbell.netwrote:

2007年2月7日下午3:56,文章

1170892566.094977.95 ... @ v45g2000cwv.googlegroups.c om,



" paul.joseph.da ... @ gmail.com" < paul.joseph.da ... @ gmail.comwrote:
On 2/7/07 3:56 PM, in article
1170892566.094977.95...@v45g2000cwv.googlegroups.c om,

"paul.joseph.da...@gmail.com" <paul.joseph.da...@gmail.comwrote:

我刚刚第一次遇到两阶段查找而且我是

抓了我一下头。两阶段查找背后的想法很容易理解,但我有一个案例无法编译,虽然

在我看来它应该。
I''ve just had my first encounter with two-phase lookup and I''m
scratching my head a bit. The idea behind two phase look up is pretty
easy to understand, but I have a case that fails to compile although
it appears to me that it should.


template< typename TYPE>

void

foo(std :: map< int ,TYPE pmap)

{

std :: map< int,TYPE> :: iterator iter;

}
template<typename TYPE>
void
foo( std::map< int, TYPE pmap )
{
std::map< int, TYPE >::iterator iter ;
}


使用g ++ 4.0.2编译时出现以下错误:
When compiled with g++ 4.0.2 gives the following error:


test.cc:在函数''void foo(std :: map< int,TYPE,std :: less< int>,

test.cc:31:错误:预期`;''之前''iter''
test.cc: In function ''void foo(std::map<int, TYPE, std::less<int>,
test.cc:31: error: expected `;'' before ''iter''


除非我''错了,这应该是一个依赖名称查找,因此在模板实例化之前不要查找

Unless I''m mistaken, this should be a dependant name lookup, and thus
not be looked up until template instantiation.



编译器执行在

类模板的定义中的两阶段名称查找,类模板的嵌套类,类的成员

模板或者成员类模板的嵌套类 - 但不在上面例子中的foo()中的
a函数模板中。


Greg- Zitierten文本ausblenden -


- Zi tierten Text anzeigen -


The compiler performs "two phase" name-lookup within the definition of a
class template, a nested class of a class template, a member of a class
template, or a member of a nested class of a class template - but not within
a function template like foo() in the example above.

Greg- Zitierten Text ausblenden -

- Zitierten Text anzeigen -



Greg Herlihy< gre ... @ pacbell.netwrote:

Greg Herlihy <gre...@pacbell.netwrote:


foo(std :: map< int,TYPE pmap)

{

std :: map< int,TYPE> :: iterator iter;

}
foo( std::map< int, TYPE pmap )
{
std::map< int, TYPE >::iterator iter ;
}



visual studi 2005接受你的代码,

for gcc add typename

例如:

typename std :: map< int,TYPE> :: iterator iter;

lg Rudi

visual studi 2005 accepts your code,
for gcc add typename
e.g.:
typename std::map< int, TYPE >::iterator iter ;
lg Rudi


pa *************** @ gmail.com 写道:




我刚刚第一次遇到两阶段查找而且我是

有点挠我的头。两阶段查找背后的想法很容易理解,但我有一个案例无法编译,虽然

在我看来它应该是。


模板< typename TYPE>

void

foo(std :: map< int,TYPE pmap)

{

std :: map< int,TYPE> :: iterator iter;

}


使用g ++ 4.0.2编译时出现以下错误:

test.cc:在函数''void foo(std :: map< int,TYPE,std :: less< int>,

std :: allocator< std :: pair< ; const int,TYPE>)'':

test.cc:31:错误:预期`;''在''iter'之前''

test.cc:在函数''void foo(std :: map< int,TYPE,std :: less< int>,

std :: allocator< std :: pair< const int,TYPE>)[ with TYPE = char]'':

test.cc:42:从这里实例化

test.cc:31:错误:依赖名称

''std :: map< int,TYPE,std :: less< int> ;,std :: allocator< s td :: pair< const int,

TYPE> :: iterator' '被解析为非类型,但是实例化产生

a类型

test.cc:31:注意:说''typename

std :: map< int,TYPE,std :: less< int> ;,std :: allocator< st d :: pair< const int,

TY PE> :: iterator''如果是一个类型


除非我弄错了,这应该是一个依赖名称查找,因此

在模板实例化之前不要被查询。


根据这篇文章:
http://www.codeproject.com/cpp/TwoPh...&select=949325


它应该是依赖的,因为它的类型是依赖的。这使得

很有意义。


鉴于这一切,导致这个''被解析为非类型''错误?


这纯粹是一个学术问题,但是我不能想出来的东西。


谢谢,

Paul Davis
Hi,

I''ve just had my first encounter with two-phase lookup and I''m
scratching my head a bit. The idea behind two phase look up is pretty
easy to understand, but I have a case that fails to compile although
it appears to me that it should.

template<typename TYPE>
void
foo( std::map< int, TYPE pmap )
{
std::map< int, TYPE >::iterator iter ;
}

When compiled with g++ 4.0.2 gives the following error:

test.cc: In function ''void foo(std::map<int, TYPE, std::less<int>,
std::allocator<std::pair<const int, TYPE >)'':
test.cc:31: error: expected `;'' before ''iter''
test.cc: In function ''void foo(std::map<int, TYPE, std::less<int>,
std::allocator<std::pair<const int, TYPE >) [with TYPE = char]'':
test.cc:42: instantiated from here
test.cc:31: error: dependent-name
''std::map<int,TYPE,std::less<int>,std::allocator<s td::pair<const int,
TYPE >::iterator'' is parsed as a non-type, but instantiation yields
a type
test.cc:31: note: say ''typename
std::map<int,TYPE,std::less<int>,std::allocator<st d::pair<const int,
TYPE >::iterator'' if a type is meant

Unless I''m mistaken, this should be a dependant name lookup, and thus
not be looked up until template instantiation.

According to this article:
http://www.codeproject.com/cpp/TwoPh...&select=949325

It should be dependant because its type is dependant. Which makes a
great deal of sense.

Given all that, what''s causing this ''parsed as non-type'' error?

This is purely an academic question, but its something I can''t figure
out.

Thanks,
Paul Davis



恕我直言,我不认为这是两阶段查找的症状。我认为这是
是C ++解析器认为是什么但结果不是。

我的意思可能最好用一个例子来描述。


A级

{

公开:

typedef int someType;

static int someVariable;

}


现在让我们看一下这段代码(没有任何上下文)


A :: someType

A :: someVariable


似乎很清楚A :: SomethingGoesHere可以

要么是类型或变量(甚至是一个函数!!)


所以当编译器第一次读取这部分内容时:


std ::地图< int,TYPE> :: iterator


第一件事,解析假定是Hmmmmm

std :: map< int,TYPE> ::迭代器必须是成员变量!!"

(技术上,它是非类型:成员变量或函数)

显然不是。但实际上,当模板实例化后,它被证明是致命的!它最初假设的原因

应该是非类型用作类型,此外,实例化过程显示它是一种类型的
。 OMG什么

汇编先生会做什么?!


那么我们如何纠正这个?实际上,你的编译器告诉

你如何纠正它!!

IMHO, I do not think this is a symptom of Two-Phase Lookup. I think this
is how the C++ parser think something is but turns out not to be.
What do I mean about this probably best described by an example.

class A
{
public:
typedef int someType;
static int someVariable;
}

Now let us look at this code (without any context)

A::someType
A::someVariable

It seems to be quite clear that A::SomethingGoesHere could
either be a type or a variable (and even a function too!!)

So when the compiler first reads this part of the line:

std::map< int, TYPE >::iterator

The first thing, the parse assumes is "Hmmmmm
std::map<int,TYPE>::iterator must be a member variable!!"
(technically, it is a non-type: member variable or function)
which obviously is not. But natually, when the template gets
instantiated, it proves to be fatal!! what it originally assumed
was supposed to be a non-type is used as a type and in addition,
the instantiation process revealed it to be a type. OMG What
will Mr. Compiler do?!

So how do we correct this? Well actually, you compiler tells
you how to correct it!!


test.cc:31:注意:说''typename

std :: map< int,TYPE,std :: less< int> ;,std :: allocator< st d :: pair< const int,

TYPE>: :iterator''如果类型是
test.cc:31: note: say ''typename
std::map<int,TYPE,std::less<int>,std::allocator<st d::pair<const int,
TYPE >::iterator'' if a type is meant



这意味着,你应该这样做:

模板< typename TYPE>

void

foo(std :: map< int,TYPE& pmap)

{

typename std :: map< int,TYPE> :: iterator iter;

}


BTW,我添加了对你的pmap输入的引用:)


我希望有所帮助。 :)


祝你好运!

This means, you should do this:
template<typename TYPE>
void
foo( std::map< int, TYPE &pmap )
{
typename std::map< int, TYPE >::iterator iter ;
}

BTW, I added a reference to your pmap input :)

I hope that helped. :)

Good Luck!


这篇关于两阶段查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆