gpt4 book ai didi

c++ - 在重载选择期间不考虑默认参数的转换?

转载 作者:太空狗 更新时间:2023-10-29 21:00:32 25 4
gpt4 key购买 nike

我试图为这个问题编写一个简单的解决方案:array decay to pointer and overload resolution

在原文中很明显,两个重载具有相等的转换强度(精确匹配),因此首选非模板。实际上,如果我将另一个更改为模板,调用就会变得不明确:

struct stg
{
template<typename T = void>
stg(const char* const& c_str, T* = 0);

template<int N>
stg(const char (&str) [N]);
};

所以我想引入一个用户定义的转换,这将比第二次重载的精确匹配严格差。

struct stg
{
template<typename> struct cvt { operator int() { return 0;} };

template<typename T = void>
stg(const char* const& c_str, int = cvt<T>());

template<int N>
stg(const char (&str) [N]);
};

但是g++ says this is still ambiguous .为什么默认参数中的用户自定义转换不影响重载排名?

最佳答案

[over.match.viable]/2(来自 >n3797,github 5f7cb4)

First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list.

  • If there are m arguments in the list, all candidate functions having exactly m parameters are viable.

  • A candidate function having fewer than m parameters is viable only if it has an ellipsis in its parameter list (8.3.5). For the purposes of overload resolution, any argument for which there is no corresponding parameter is considered to “match the ellipsis” (13.3.3.1.3).

  • A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default argument (8.3.6). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.

[强调我的]

关于c++ - 在重载选择期间不考虑默认参数的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21974678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com