解析分配给默认参数值的重载函数时,会考虑哪些函数集? [英] What set of functions is considered when resolving overloaded functions assigning to default parameter values?

查看:128
本文介绍了解析分配给默认参数值的重载函数时,会考虑哪些函数集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的函数 bar ,其参数具有从调用重载的 foo 初始化的默认值:

Consider the function bar below, whose parameter has a default value initialized from an invocation of an overloaded foo:

#include <iostream>

int foo(int x)
{
  std::cout << "foo(int)" << std::endl;
  return 0;
}

template<typename T>
void bar(T a, int x = foo(T(0))) {}

double foo(double x)
{
  std::cout << "foo(double)" << std::endl;
  return 0;
}

int main()
{
  bar<int>(1);
  bar<double>(1);
  return 0;
}



我希望此程序输出

I expect this program to output

foo(int)
foo(double)

对应于 foo 的两个重载,在 bar 的实例化中可见。

corresponding to foo's two overloads which are visible at bar's instantiation.

当使用 g ++ - 4.6 编译时,输出为

$ g++-4.6 -std=c++0x test.cpp; ./a.out 
foo(int)
foo(int)

当实现不同于正常过载分辨率的默认参数值时,是否考虑过一组过载?这是在ISO C ++标准中描述的吗?

Is the set of overloads considered when implementing a default parameter value different from normal overload resolution? Is this case described in the ISO C++ standard?

推荐答案

首先,在8.3.6 [dcl.fct.default]第5段:

Here is what the standard says about this. First, in 8.3.6 [dcl.fct.default] paragraph 5:


...默认参数中的名称是bound,并在出现默认参数的点检查语义约束。对函数模板中的默认参数和类模板的成员函数的语义约束的名称查找和检查,如第14.7.1节所述执行。 ...

... The names in the default argument are bound, and the semantic constraints are checked, at the point where the default argument appears. Name lookup and checking of semantic constraints for default arguments in function templates and in member functions of class templates are performed as described in 14.7.1. ...

进一步在14.7.1 [temp.inst]第12段:

Further in 14.7.1 [temp.inst] paragraph 12:


如果以需要使用默认参数的方式调用函数模板f,则查找依赖名称,检查语义约束,并使用任何模板的实例化在默认参数中完成,如同默认参数是在具有相同范围,相同模板参数和与在该点使用的函数模板f
的访问相同的函数模板专用化中使用的初始化器。这种分析称为默认参数实例化。实例化的默认参数然后用作f的参数。

If a function template f is called in a way that requires a default argument to be used, the dependent names are looked up, the semantics constraints are checked, and the instantiation of any template used in the default argument is done as if the default argument had been an initializer used in a function template specialization with the same scope, the same template parameters and the same access as that of the function template f used at that point. This analysis is called default argument instantiation. The instantiated default argument is then used as the argument of f.

我不太确定这是什么意思。我可以在本文中看到两种解释。如果有问题,我认为EDG同意gcc和clang。

I'm not quite sure what this exactly says. I can see both interpretations in this text. In case it matters, I think EDG agrees with gcc and clang.

这篇关于解析分配给默认参数值的重载函数时,会考虑哪些函数集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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