gpt4 book ai didi

c++ - 如何使用包含的转换的等级比较两个标准转换序列

转载 作者:行者123 更新时间:2023-12-03 10:03:49 24 4
gpt4 key购买 nike

#include <iostream>
void g(int*); //#1
void g(int (&arr)[2]); //#2

void f(int*); //#3
void f(int const*); //#4
int main(){
int arr[2] ={0};
f(arr); // choose #3
g(arr); //ambiguous
}
考虑上面的代码, #3 被选为 f(ptr)然而, g(arr)给出 ambiguous诊断。
选择最佳函数的规则定义为:

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if

  • S1 is a proper subsequence of S2 (comparing the conversion sequences in the canonical form defined by [over.ics.scs], excluding any Lvalue Transformation; the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence) or, if not that


所以看看 over.ics.scs#3

These are used to rank standard conversion sequences. The rank of a conversion sequence is determined by considering the rank of each conversion in the sequence and the rank of any reference binding.


根据我对上述规则的理解,可以理解为什么 #3f(ptr) 的最佳过载, 那是:
将 S1 设为 (arr => int*):
Array-to-pointer conversion -> (identity conversion)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
int[2] => int* int* => int*
同时将 S2 设为 (ptr => int const*)
Array-to-pointer conversion -> Qualification conversions ->  identity conversion   
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
int[2] => int* int* => int const* int const* => int const*
identity conversionQualification conversions 的真子序列,因此 S1 优于 S2。所以, #3 f(ptr) 的重载决议选择.
当我使用类似的过程来确定哪个最适合 g(arr) ,我遇到了一个问题。
同样,给定 S1 为 (arr => int*)
Array-to-pointer conversion -> identity conversion  
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
int[2] => int* int* => int*
而给定 S2 as(arr => int (&arr)[2])

When a parameter of reference type binds directly to an argument expression, the implicit conversion sequence is the identity conversion, unless the argument expression has a type that is a derived class of the parameter type, in which case the implicit conversion sequence is a derived-to-base Conversion

identity conversion
^^^^^^^^^^^^^^^^^^^
bind to reference
在这里, identity conversionS2Array-to-pointer conversion 的真子序列的 S1 ,因此它应该比 S1 更好,为什么编译器会提示 g(arr)是模棱两可的调用吗?
我对如何对标准转换序列进行排名有任何误读吗?如何比较两个标准 ICS(包含转换的等级)?

最佳答案

关键点在这里:

S1 is a proper subsequence of S2 (comparing the conversion sequences in the canonical form defined by [over.ics.scs], excluding any Lvalue Transformation; the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence) or, if not that


这意味着,对于函数调用 g(arr) , 全部 数组到指针的转换 不用于确定排名。换句话说,来自类型 int[2]输入 int* ,只有一个身份转换用于确定排名。因此, void g(int*); 的 S1和 void g(int (&arr)[2]); 的 S2是无法区分的 ICS,因此编译器给出了一个不明确的错误。
相比之下, void f(int*); 的转换和 void f(int const*);用于比较排名的是 identity conversionqualification conversion , 分别。
根据规则:

the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence


因此, Qualification conversion被认为比 identity conversion 的排名更差.所以, void f(int*)赢得了比赛。

关于c++ - 如何使用包含的转换的等级比较两个标准转换序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65247901/

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