void testLogAndAdd() { std::string petName("Darla"); logAndAdd(petName); // pass lvalue std::string, ok // Problem 1: // pass rvalue std::string // the name is copied into names. // but as the argument is rvalue, we can do move actually! logAndAdd(std::string("Persephone")); // Problem 2: // an implicit constructor of std::string is called. // the name is copied into names // we can pass the const char* into emplace directly // so there is no need to construct std::string. logAndAdd("Patty Dog"); }
int main(int argc, const char * argv[]) { // insert code here... std::string petName("Darla"); logAndAdd(petName); logAndAdd(22); // Functions taking universal references are the greediest functions in C++. short shortVar = 16; logAndAdd(shortVar); std::cout << "Perfect forwarding." << std::endl; Person p("Nancy"); auto cloneOfP(p); std::cout << "Overloading test." << std::endl; BigObject ob("test"); auto copy(ob); const BigObject cobj("test"); auto copy1(cobj); std::cout << "Hello, World!\n"; return 0; }