为什么C ++ 11不支持这样的名称查找? [英] Why does C++11 not support name lookup like this?

查看:173
本文介绍了为什么C ++ 11不支持这样的名称查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct A
{
    enum InnerEnum { X };

    A(InnerEnum x)
    {}
};

int main()
{
    A a(X);
}

编译器提示: error C2065:'X' :undeclared identifier

编译器知道构造函数的参数类型,因此当我传递X作为参数时,编译器应该知道一个有效的参数。

The compiler knows what the constructor's parameter type is, so when I pass X as the argument, the compiler should know it is a valid argument.

我知道这不是ADL(依赖于参数的名称查找,也称为Koenig Lookup),但我认为它是有用的和相当方便。因为我不必写如下:

I know this is not ADL(Argument-dependent Name Lookup, also known as Koenig Lookup), but I think it's useful and pretty handy. Because I don't have to write as follows:

A a(A::X);

我认为ADL规则应该归纳为这种情况。

I think the ADL rule should be generalized to such a case.

我是对的吗?

推荐答案

C ++中的函数调用受函数重载解析。过载分辨率由参数类型驱动。也就是说参数类型具有给定名称的函数的特定版本。

Function calls in C++ are subject to function overload resolution. Overload resolution is driven by the argument types. I.e. the language "works" specifically in that direction: from argument types to specific version of the function with the given name.

您建议根据函数名称引入逆过程 - 参数类型推导。这在一般情况下不工作。它可能在只有一个候选函数(如在你的示例中)的情况下工作,但是,这与在函数重载时在一般情况下工作的原则相反。

You are proposing to introduce a reverse process - argument type deduction based on function name. This will not work in general case. It might work in cases when there's only one candidate function (as in your example), but, again, in is contrary to principles that work in the general situation when the function is overloaded.

当然,当对不合格名称 X 的名称查找可以看到名为 X 除了 A :: X 。我认为它很容易得到非常反直觉。

Of course, the situation will get even more complicated when name lookup on unqualified name X can see something else named X in addition to your A::X. I think it can easily get very counterintuitive.

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

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