Esempio n. 1
0
int main(void)
{
    // instantiates static members Foo<string>::ctr and Foo<string>::count
    Foo<string> fs;
    // all three objects share the same Foo<int>::ctr and Foo<int>::count
    // members
    Foo<int> fi1, fi2, fi3;

    Foo<int> fi;        // instantiates Foo<int> class and the static data member ctr
    Foo<int>::count();   // instantiates Foo<int>::count
    fi.count();         // uses Foo<int>::count
  //Foo::count();       // error: which template instantiation?

    return 0;
}
Esempio n. 2
0
int main()
{
#if 0
    // specialization stuff
    Foo<string> fs;
    fs.Bar(); // calls regular function Bar
    Foo<int> fi;
    fi.Bar(); // calls specialized function Bar

    FooMan fm("toad", 123, 0.1);
    FooMan fm2("toad", 123, 0.1);
    if (fm == fm2) {
        cout << "equal FooMan" << endl;
    } else {
        cout << "non equal FooMan" << endl;
    }
    cout << hash<FooMan>{}(fm) << endl;
    cout << hash<FooMan>{}(fm2) << endl;

    vector<int> vi = {1,2,3,4}; 
    int x = 42;
    string name = "dude";
    cout << hash<string>{}(name) << endl;
    cout << hash<int>{}(42) << endl;
    cout << hash<int>{}(x) << endl;

    // partial template class specialization
    int i;
    rem_ref<decltype(42)>::type a;
    rem_ref<decltype(i)>::type b;
    rem_ref<decltype(std::move(i))>::type c;
    a = b = c = 43; // all are ints
    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
#endif

#if 1
    // variadic templates
    int i = 0; 
    double d = 3.14;
    string s4 = "how now";
    // template parameter T is deduces from 1st parameter
    foo(i, s4, 42, d);  // three parameters in pack
    foo(s4, 42, "hi");   // two
    foo(d, s4);          // one
    foo("hi");          // zero empty pack
    // test print variadic template
    print(cout, i, d, s4);
    //errorMsg(cout, i, d, s4); // calls appropriate overloaded version of debug_rep
#endif


#if 0 // forwarding
    int it = 43;
    f(42, it);
    it = 43;
    flip2(f, it, 42);
    cout << "after call: " << it << endl;
    it = 43;
    flip2(f, 42, it); // now it will chg, T2 is a ref
    cout << "after call: " << it << endl;
    it = 43;
    flip3(f2, it, 42); // now it will chg, T2 is a ref
    cout << "after call: " << it << endl;
#endif
#if 0 // rvalues
    string s1("hi"), s2;
    s2 = std::move(string("bye!")); // move from rvalue
    cout << s2 << endl;
    s2 = std::move(s1); // s1 is indeterminate state after move
    cout << s2 << endl;
    g(42);
    // these won't compile
    //const int i = 43;
    //const int& j = i;
    //g(j);
#endif
#if 0
    cout << compare(1,0) << endl;
    string a = "hand", b = "bag";
    cout << compare(b,b) << endl;
    cout << compare("dou","bag") << endl;
    const char *p;
    const char *q;
    char ppp[] = "aaa";
    char qqq[] = "bbb";
    p = &ppp[0];
    q = &qqq[0];
    cout << compare(p, q) << endl; // specialization version

    vector<int> v1{1,2,3};
    vector<int> v2{4,5,6};

    cout << compare(v1, v2) << endl;
    cout << compare(v2, v1) << endl;

    cout << compare(1, 0) << endl;
    cout << compare(0, 1) << endl;
    cout << compare(1, 1) << endl;

    int rc = 42;
    rc = foo(&rc);
    cout << rc << endl;

    const char a2[4] = "bag";
    const char b2[4] = "dog";
    cout << compare(a2, b2) << endl;
    cout << compare2(a2, b2) << endl;
    cout << compare3(1,0) << endl;
    cout << compare3("bat","man") << endl;
    int aa = 2;
    long bb = 1;
    cout << FlexibleCompare(aa, bb) << endl;
#endif
#if 0
    vector<int> vi{1,2,3};
    vector<string> vs{"one","two","three"};
    const vector<string> cvs{"four","five","six"};
    auto &ii = fcn(vi.begin(), vi.end());
    auto &ss = fcn(vs.begin(), vs.end());
    // this one uses type traits
    auto &css = fcn2(cvs.begin(), cvs.end());
#endif
    test_blob();
#if 0

    Foo<int> fi;
    auto ct = Foo<int>::count();
    cout << "ct: " << ct << endl;
    ct = fi.count();
    cout << "ct: " << ct << endl;
    // demo customer deleter
    double *p = new double(3.14);
    DebugDelete d;
    d(p);
    int *ip = new int(42);
    DebugDelete()(ip);
#endif
#if 0
    // we can supply DebugDelete as the deleter to unique_ptr's
    unique_ptr<int, DebugDelete> p2(new int(43), DebugDelete());
    unique_ptr<string, DebugDelete> sp2(new string("dude"), DebugDelete());

    shared_ptr<string> ps(new string("the dawg"), DeleteStringPtr);
    shared_ptr<string> ps2(new string("homi"), my_deleter<string>);
    // reset to different deleter at run-time -- only shared_ptr's not unique_ptr's
    // looks like call to reset will call the original deleter first
    ps2.reset(new string("wusup"), my_deleter2<string>);
#endif
#if 0
    // template overloading
    string s("hi");
    cout << debug_rep(s) << endl;
    cout << debug_rep(&s) << endl;
    cout << debug_rep("hi world!") << endl;
    char s2[] = "hello dude";
    cout << debug_rep(s2) << endl;
#endif

}