I have below code. In order to cut off cost of calling fun. I set fun(string &a) use reference parameter. But it can't be compiled.
#include <iostream>
using namespace std;
void
fun (string &a) {
        string concat = "prefix_" + a;
        cout << "[fun] concat:" << concat << endl;
}
int
main(int argc, char **argv) {
        fun("test");
        return 0;
}
cannot bind non-const lvalue reference of type ‘std::string&’ {aka ‘std::__cxx11::basic_string&’} to an rvalue of type ‘std::string’ {aka ‘std::__cxx11::basic_string’}
I change fun (string &a) to fun (string a). It works. But it seems to lead to unnecessary construction.
BTW, How shold I write the right code to minimum cost of unnecessary implicit construction?
Aucun commentaire:
Enregistrer un commentaire