我试图实现一个函数被传递一些简单的参数(例如std :: string)但不能被置换。
想象两个函数,比如
void showFullName(std::string firstname, std::string lastname) {
cout << "Hello " << firstname << " " << lastname << endl;
}
void someOtherFunction() {
std::string a("John");
std::string b("Doe");
showFullName(a, b); // (1) OK
showFullName(b, a); // (2) I am trying to prevent this
}
如您所见,可以混合函数参数的顺序——这是我试图阻止的。
我的第一个想法是某种 typedef,例如
typedef std::string Firstname;
typedef std::string Lastname;
void showFullName(Firstname firstname, Lastname lastname)
//...
但是(至多 GNU 的)c++ 编译器的行为不像我想要的那样;)
有人对此有很好的解决方案吗?