const/non-const 重载决策中的右值引用
const/non-const rvalue reference in overload resolution
#include <iostream>
#include <string>
using namespace std;
void func(string &&a) { cout << "#1" << endl; }
void func(const string &&a) { cout << "#2" << endl; }
void func(int &&a) { cout << "#3" << endl; }
void func(const int &&a) { cout << "#4" << endl; }
int main()
{
func(string("1")); // call func(string &&)
func((const string)string("1")); // call func(const string &&)
func(1); // call func(int &&)
func((const int)1); // call func(int &&) not func(const int &&)
return 0;
}
来自 C++ 标准:
Standard conversion sequence S1 is a better conversion sequence than
standard conversion sequence S2 if
...
S1 and S2 are reference bindings (8.5.3), and the types to which the
references refer are the same type except for top-level cv-qualifiers,
and the type to which the reference initialized by S2 refers is more
cv-qualified than the type to which the reference initialized by S1
refers.
最后一次调用似乎没有按预期运行。谁能给我解释一下?
(const int)1
的类型在重载解析之前调整为 int
。
If a prvalue initially has the type “cv T
”, where T
is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T
prior to any further analysis.
#include <iostream>
#include <string>
using namespace std;
void func(string &&a) { cout << "#1" << endl; }
void func(const string &&a) { cout << "#2" << endl; }
void func(int &&a) { cout << "#3" << endl; }
void func(const int &&a) { cout << "#4" << endl; }
int main()
{
func(string("1")); // call func(string &&)
func((const string)string("1")); // call func(const string &&)
func(1); // call func(int &&)
func((const int)1); // call func(int &&) not func(const int &&)
return 0;
}
来自 C++ 标准:
Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if
...
S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.
最后一次调用似乎没有按预期运行。谁能给我解释一下?
(const int)1
的类型在重载解析之前调整为 int
。
If a prvalue initially has the type “cv
T
”, whereT
is a cv-unqualified non-class, non-array type, the type of the expression is adjusted toT
prior to any further analysis.