返回参数的typename查找 [英] Typename lookup of return parameter

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

问题描述

我最近被一个学生问到一个编译问题。答案很简单,但现在我正在努力的原因。
一个简单的例子:

I was recently asked by a student about a compile issue. The answer was quite simple but right now I am struggling about the reason. A simple example:

#include <iostream>
#include <vector>

struct MyStruct
{
    typedef std::vector<int> MyIntVector;

    MyIntVector CopyVector(MyIntVector const& vector);
};


MyStruct::MyIntVector MyStruct::CopyVector(MyIntVector const& vector)
^^^^^^^^
{
    MyIntVector vec;
    return vec;
}

int main(int /*argc*/, char** /*argv*/)
{
    MyStruct st;
}

为了成为有效的c ++代码,返回参数必须是完全限定的。这么多的答案,使编译器/学生快乐。

To be valid c++ code the return parameter has to be fully qualified. So much to the answer and to make the compiler/student happy.

但是为什么要使用类和函数的参数限定返回值不是?

But why has the return value to be qualified with the class and the parameter to the function not?

我一直这样做,我知道它与ADL查找有关,但现在我被要求我寻找一个更好的答案。

任何人都可以给我一个参考的指导或一个提示,我可以找到一些更多的信息。

I always did this and I know that it has to do with the ADL lookup, but now that I was asked I searching for a better answer.
Can anyone give a me reference to the spec or a hint where I can find some more information?

推荐答案

语法是这样的,返回类型是独立于声明的,并且可以声明(但不是定义)几个具有相同类型的东西。这是有效的C ++:

The structure of the grammar is such that the return type is independant of what is declared and it is possible to declare (but not define) several things with the same type. This is valid C++:

int f(int), g(int);

因此具有影响类型的查找的声明对象的精确范围将是有问题的。在

so having the precise scope of the declared objects influencing the lookup for the type would be problematic. In

id1 ns1::f(int), ns2::g(int);

在哪里查找id1?

可以在函数定义中添加特殊的规则(只能有一个函数定义,所以没有歧义,但可以是几个对象),但是我不确定可能性,我认为增加的复杂性不会被优势补偿。

One could have added special rules to use in function definition (there can be only one function definition -- so there would be no ambiguity --, but can be several object one), but I'm not sure the possibility has been examined, and I think the added complication would not be compensated by the advantage.

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

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