C++14增加了字符串字面量,即在字符串后面加上s,如"hello"s
#include <iostream>
#include <string>
using namespace std;
void f(const string& a)
{
cout<<"1 "<<a<<endl;
}
void f(const char* a)
{
cout<<"2 "<<a<<endl;
}
int main()
{
f("hello"s);
f("hello");
}
运行程序输出:
1 hello
2 hello
可见通过"hello"s会调用重载函数中参数为string的函数,而直接通过"hello"会调用参数为const char*的版本。