考虑以下代码:
#include <iostream>
void foo(int m);
void foo(int &k);
int main()
{
foo(5); // ok, because there is no ambiguity
int m = 5;
//foo(m); // compile-time error, because of ambiguity
foo(m + 0); // ok, because it's an expression of type int and not object's lvalue
}
void foo(int m)
{
std::cout << "by value\n";
}
void foo(int &k)
{
std::cout << "by reference\n";
}
我知道这会给foo(m)
带来歧义,但是当表达式的类型为int
(或者其他可以转换为int
的类型)时,这种情况是允许的吗?
我尝试查找一些标准参考资料,但是没有成功。
免责声明:请注意,这不是基于值与常量引用的函数重载的副本。因为const
引用可以与rvalues赋值,而"普通"非const
引用则相反。
m
也是int
类型的表达式。 - Kerrek SB