重载期间提升参数
Promotion of parameters during overloading
我正在研究超载,但我对促销完全感到困惑。我在 SO (implicit conversion sequence in function overloading) and I am sure some more are available, but could not find the right article. I was also referring to http://www.dcs.bbk.ac.uk/~roger/cpp/week20.htm.
我在看 Stroustrup 的 C++ Programming 特别版,看到了以下解释。
Finding the right version to call from a set of overloaded functions
is done by looking for a best match between the type of the argument
expression and the parameters (formal arguments) of the functions. To
approximate our notions of what is reasonable, a series of criteria
are tried in order: 1 Exact match [2] Match using promotions;
[3] Match using standard conversions [4] Match using user-defined
conversions [5] Match using the ellipsis ......
void print(int);
void print(double);
void print(long);
void print(char);
void h(char c, int i, short s, float f)
{
print(s); // integral promotion: invoke print(int)
print(f); // float to double promotion: print(double)
}
我写了下面的代码。我在想,如果我调用值为 1 的函数,则 func1(long) 将被调用,因为发生了提升。但是我收到错误消息 "error: call of overloaded 'func1(int)' is ambiguous"。它甚至不调用带有 unsigned char 类型变量的函数。
此外,如果我通过调用 func1(3.4f),则会调用 func1(double) 并按照我的预期进行提升。为什么 1 没有提升为 long int 而 float 提升为 double?进行了哪些整数促销?
void func1(unsigned char speed)
{
cout<<"Func1 with unsigned char: speed =" << speed <<" RPM\n";
}
void func1(long speed)
{
cout<<"Func1 with long Int: speed =" << speed <<" RPM\n";
}
void func1(double speed)
{
cout<<"Func1 with double: speed =" << speed <<" RPM\n";
}
int main(void)
{
func1(1);
func1(3.4f);
return(0);
}
标准规定:
[C++11: 4.13/1]:
("Integer conversion rank")
Every integer type has an integer conversion rank defined as follows:
- [..]
- The rank of
long long int
shall be greater than the rank of long int
, which shall be greater than the rank of int
, which shall be greater than the rank of short int
, which shall be greater than the rank of signed char.
- The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.
- [..]
这在您的示例中需要歧义。
至于func1(3.4f);
,只是将float提升为double,最匹配,因为另外两个重载方法有long
和unsigned char
.
同时检查这个 table:
其中子条款指定:
[conv.fpprom]:
(7.7 Floating-point promotion )
- A prvalue of type
float
can be converted to a prvalue of type double
. The value is unchanged.
- This conversion is called floating-point promotion.
我正在研究超载,但我对促销完全感到困惑。我在 SO (implicit conversion sequence in function overloading) and I am sure some more are available, but could not find the right article. I was also referring to http://www.dcs.bbk.ac.uk/~roger/cpp/week20.htm. 我在看 Stroustrup 的 C++ Programming 特别版,看到了以下解释。
Finding the right version to call from a set of overloaded functions is done by looking for a best match between the type of the argument expression and the parameters (formal arguments) of the functions. To approximate our notions of what is reasonable, a series of criteria are tried in order: 1 Exact match [2] Match using promotions; [3] Match using standard conversions [4] Match using user-defined conversions [5] Match using the ellipsis ......
void print(int);
void print(double);
void print(long);
void print(char);
void h(char c, int i, short s, float f)
{
print(s); // integral promotion: invoke print(int)
print(f); // float to double promotion: print(double)
}
我写了下面的代码。我在想,如果我调用值为 1 的函数,则 func1(long) 将被调用,因为发生了提升。但是我收到错误消息 "error: call of overloaded 'func1(int)' is ambiguous"。它甚至不调用带有 unsigned char 类型变量的函数。
此外,如果我通过调用 func1(3.4f),则会调用 func1(double) 并按照我的预期进行提升。为什么 1 没有提升为 long int 而 float 提升为 double?进行了哪些整数促销?
void func1(unsigned char speed)
{
cout<<"Func1 with unsigned char: speed =" << speed <<" RPM\n";
}
void func1(long speed)
{
cout<<"Func1 with long Int: speed =" << speed <<" RPM\n";
}
void func1(double speed)
{
cout<<"Func1 with double: speed =" << speed <<" RPM\n";
}
int main(void)
{
func1(1);
func1(3.4f);
return(0);
}
标准规定:
[C++11: 4.13/1]:
("Integer conversion rank")Every integer type has an integer conversion rank defined as follows:
- [..]
- The rank of
long long int
shall be greater than the rank oflong int
, which shall be greater than the rank ofint
, which shall be greater than the rank ofshort int
, which shall be greater than the rank of signed char.- The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.
- [..]
这在您的示例中需要歧义。
至于func1(3.4f);
,只是将float提升为double,最匹配,因为另外两个重载方法有long
和unsigned char
.
同时检查这个 table:
其中子条款指定:
[conv.fpprom]:
(7.7 Floating-point promotion )
- A prvalue of type
float
can be converted to a prvalue of typedouble
. The value is unchanged.- This conversion is called floating-point promotion.