Beispiel #1
0
    void testLogAndAddEfficiently()
    {
        std::string petName("Darla");

        // instantiate logAndAddEfficiently<std::string &>(std::string& name);
        logAndAddEfficiently(petName);

        // name is moved to names
        // instantiate logAndAddEfficiently<std::string>(std::string&& name);
        logAndAddEfficiently(std::string("Persephone"));

        // construct std::string in names with string literal.
        // instantiate logAndAddEfficiently<const char (&)[10]>(const char (&name)[10]);
        logAndAddEfficiently("Patty Dog");

        // construct std::string in names with string literal.
        // instantiate logAndAddEfficiently<const char (&)[6]>(const char (&name)[6]);
        logAndAddEfficiently("Patty");
    }
Beispiel #2
0
    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");
    }
Beispiel #3
0
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;
}