如何在函数重载决策中转储候选者?

发布于 2024-09-24 07:04:40 字数 496 浏览 4 评论 0原文

如何转储函数调用的候选函数(或可行函数或最佳可行函数)?

我知道 g++ 提供了一个转储类层次结构的选项。 (事实上​​,Visual Studio 2010 提供了一个类似的选项,但它没有文档记录。我记得读过一些关于它的内容——可能是在 VC++ 团队博客中——但我记不太清楚了。)

最近,我一直在阅读有关重载解析的内容C++0x 草案,这真的让我很尴尬。

是否有编译器提供转储候选函数、可行函数或最佳可行函数的选项?

注意:重载决策场景中的候选函数与编译器错误中的候选函数不同。重载解析场景中的候选/可行/最佳可行函数有其自己的含义。我知道重载决策分为三个阶段:找到候选函数;找到可行的功能; 找到最佳可行的功能。通常,最佳可行函数只是一个候选函数;否则,调用是不明确的。每个阶段都有自己的规则。

How can I dump candidate functions (or viable functions or best viable functions) for a function invocation?

I know g++ provides an option to dump class hierarchy. (In fact, Visual Studio 2010 provides a similar option, but it's undocumented. I remember reading something about it—maybe in the VC++ team blog—but I can't remember it clearly.)

Recently, I have been reading about overload resolution in the C++0x draft, and it really embarrassed me.

Does any compiler provide an option to dump candidate functions, viable functions, or best viable functions?

Note: The candidate functions in overload resolution scenario is different from the candidate functions in the compiler error. The candidate/viable/best viable function in overload resolution scenario has their own meaning. I know their are three stages in overload resolution: find the candidate functions; find the viable functions;
find the best viable functions. Normally, the best viable function is just one candidate; otherwise, the call is ambiguous. Each stage has their own rules.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流绪微梦 2024-10-01 07:04:40

在 Visual Studio 中执行此操作的最简单方法是编译不明确的调用。编译器将抛出一个错误并列出可用候选者的列表。也许 g++ 也会做同样的事情。

The easiest way to do this in Visual Studio is to compile an ambiguous call. The compiler will spit an error with a list of available candidates. Probably g++ will do the same.

满地尘埃落定 2024-10-01 07:04:40

转储所有已考虑的函数的一种方法是使用带有一组不匹配任何内容的参数的特定函数名称:

struct DumpThemAll {};

int main(int argc, char* argv[])
{
  std::cout << DumpThemAll() << std::endl;
}

这将(通常)转储所有operator<<被考虑进行过载解决。对于这个特定的例子,它可能会变得很复杂。

如果您只想转储某些特定功能,那就困难得多。做到这一点的唯一方法是人为地创建一个不明确的调用,但是,只有当您提供的虚假函数具有与最佳匹配完全相同的“分数”时,调用才是不明确的……因此,在以下情况下很难设计一个这样的函数:你不明白这个评分是如何工作的(就我个人而言,我不明白这一切......有太多的规则......)

但是我想补充一点,即使可能只有少数人可以立即引用超载规则,或者甚至理解它,一般来说,它不会阻止您工作,因为该标准试图解决所有可能的情况,而您只处理其中的几个情况。

此外,滥用函数/运算符重载会使您的程序无法读取,因为它对人类来说是神秘的(特别是因为受当前翻译单元中包含的文件的特殊性影响)。

One way to dump all the functions that have been considered, is to use a particular function name with a set of parameters that won't match anything:

struct DumpThemAll {};

int main(int argc, char* argv[])
{
  std::cout << DumpThemAll() << std::endl;
}

This will (normally) dump all operator<< that were considered for overload resolution. For this particular example, it can get hairy.

If you wish to dump only some specific functions, it's much more difficult. The only way to do that would be to artificially create an ambiguous call, however a call is ambiguous only if the spurious function you provide has the exact same "score" than the best match... so it's hard to devise one such function when you don't understand how this scoring work (and personally, I don't understand it all... there are just too many rules...)

However I would add that even though there are probably only a handful of persons who can cite the overload rules off the top of their head, or perhaps even make sense of it, in general it doesn't prevent you from working, for the standard tries to address every possible case while you only work with a couple of them.

Furthermore, abusing function / operator overload makes your program unreadable for it's arcane for humans (especially since subject to the particular of files included in the current translation unit).

相思故 2024-10-01 07:04:40

我认为没有任何直接的方法。

一种方法是故意人为地制造冲突/歧义。大多数编译器都会发出以下形式的错误,并列出所考虑的候选者的列表。

namespace A {
    void f(int x) {}
}
void f(int x) {}
void f(char x) {}

using namespace A;

int main(){
    f(2.2);
}

G++错误信息:

prog.cpp: In function ‘int main()’:
prog.cpp:10: error: call of overloaded ‘f(double)’ is ambiguous 
prog.cpp:4: note: candidates are: void f(int)
prog.cpp:5: note:                 void f(char) 
prog.cpp:2: note:                 void A::f(int)

I don't think there is any direct way.

One way is to deliberately create a conflict/ambiguity artificially. Most compilers emit an error of the form below, spewing out the list of considered candidates.

namespace A {
    void f(int x) {}
}
void f(int x) {}
void f(char x) {}

using namespace A;

int main(){
    f(2.2);
}

G++ error message:

prog.cpp: In function ‘int main()’:
prog.cpp:10: error: call of overloaded ‘f(double)’ is ambiguous 
prog.cpp:4: note: candidates are: void f(int)
prog.cpp:5: note:                 void f(char) 
prog.cpp:2: note:                 void A::f(int)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文