C ++模板中的名称查找 [英] Name lookups in C++ templates

查看:110
本文介绍了C ++模板中的名称查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C ++代码,不再编译没有-fpermissive选项。
这是我不能共享的专有代码,但我认为我已经能够提取一个简单的测试用例来演示该问题。这里是g ++的输出

  template_eg.cpp:在'void Special_List< T> :: do_other_stuff [with T = int]':
template_eg.cpp:27:35:从这里需要
template_eg.cpp:18:25:错误:'下'没有在此范围中声明,通过参数依赖查找在实例化点[-fpermissive]
template_eg.cpp:18:25:注意:依赖基础'List< int>中的声明不能通过无限制查找找到
template_eg.cpp:18:25:note:use'this-> next'instead

是代码生成问题:

 模板< class T& class List 
{
public:
void next(T *){
cout<<<Doing some stuff<
}
};

template< class T> class Special_List:public List< T>
{
public:
void do_other_stuff(T * item){
next(item);
}
};


int main(int argc,char * argv [])
{
Special_List< int> b;
int test_int = 3;
b.do_other_stuff(& test_int);
}


$ b $ p

我不是想知道如何修复代码,再次。
这只是一个问题,改变下一个(项目)到这个 - >下一个(项目)
我想更好地了解为什么这个更改是必要的。
我在此页面上找到了解释: http://gcc.gnu.org/onlinedocs/ gcc / Name-lookup.html
虽然这个解释很有用,我仍然有一些问题。不应该事实,我的函数接受T *(指向类型T的指针)使它依赖于模板参数。
在我自己的写法,不应该编译器(gcc 4.7)能够弄清楚next()函数是在基类List?
为什么需要在每个这样的调用之前加上this->
我注意到clang 3.1展现了相同的行为,所以我假设在c ++标准中有一些需要这个行为的要求。任何人都可以提供一个理由吗?

解决方案

问题是模板在两遍处理(根据标准,VS否则)。在第一遍,在类型替换之前,不依赖于模板参数的一切都被查找和检查。然后,一旦类型被替换,则依赖名称在第二遍中被解析。



现在,在第一遍没有什么表明下一个依赖于模板参数,因此它需要在类型替换之前解决 。现在,因为基本类型是基于当前模板的模板参数的模板,编译器不能查看它(它可能专用于某些类型,并且不知道什么类型 T 我们正在实例化模板,我们不知道使用哪个专门化,即 T 的基础 ,我们在知道<$



添加 this-> next 转换为依赖名称,这意味着查找被延迟到第二遍,其中 T 已知的,并且因为 T 是已知的,所以 List< T> p>




编辑:上述答案措辞中缺少的一个重要细节是第二阶段查找后类型替换)将只添加在参数依赖查找期间找到的函数。也就是说,如果 next 是与 T 关联的命名空间中的自由函数,成员,对于 T 上的ADL不可见。


I have some C++ code that is no longer compiling without the -fpermissive option. It's propriety code that I can't share, but I've think I've been able to extract a simple test case that demonstrates the problem. Here is the output from g++

template_eg.cpp: In instantiation of 'void Special_List<T>::do_other_stuff(T*) [with T = int]':
template_eg.cpp:27:35:   required from here
template_eg.cpp:18:25: error: 'next' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
template_eg.cpp:18:25: note: declarations in dependent base 'List<int>' are not found by unqualified lookup
template_eg.cpp:18:25: note: use 'this->next' instead

So here is the code the generates the problem:

template<class T> class List  
{
        public: 
        void next(T*){
            cout<<"Doing some stuff"<<endl;
        }       
};

template<class T> class Special_List: public List<T>
{
    public:
        void do_other_stuff(T* item){
                next(item);
        }       
};


int main(int argc, char *argv[])
{
    Special_List<int> b;
    int test_int = 3;
    b.do_other_stuff(&test_int);
}

I am not trying to find out how to fix the code to make it compile again. That's simply a matter of changing next(item) to this->next(item) I'm trying to better understand why this change is necessary. I found an explanation on this page: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html While that explanation was useful, I still have some questions. Shouldn't the fact that my function takes a T* (pointer to type T) make it dependent on the template argument. In my own wording, shouldn't the compiler (gcc 4.7) be able to figure out that the next() function is in the base class List? Why is it necessary to prepend this-> in front of every such call? I've notice that clang 3.1 exhibits the same behavior, so I assume that there is some requirement in the c++ standard that requires this behavior. Could anyone provide a justification for it?

解决方案

The problem is that templates are processed in two passes (according to the standard, VS does otherwise). In the first pass, before the type substitution, everything that does not depend on the template arguments is looked up and checked. Dependent names are then left to resolve in the second pass, once the type has been substituted.

Now, in the first pass there is nothing that indicates that next is dependent on template arguments, and thus it needs to resolve before type substitution. Now, because the base type is templated on the template argument of your current template, the compiler cannot look into it (it might be specialized for some types, and without knowing what type T we are instantiating the template with, we cannot know which specialization to use, i.e. the base depends on T and we are checking before knowing T).

The trick of adding this-> turns next into a dependent name, and that in turn means that lookup is delayed until the second pass, where T is known, and because T is known, List<T> is also known and can be looked up into.


EDIT: One important detail missing in the wording of the answer above is that second phase lookup (after type substitution) will only add functions found during argument dependent lookup. That is, if next was a free function in a namespace associated with T it would be found, but it is a member on the base, which is not visible for ADL on T.

这篇关于C ++模板中的名称查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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