Created
July 8, 2015 13:51
-
-
Save beyondwdq/c90387aa410d6a0e2481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <list> | |
#include <iostream> | |
#include <cassert> | |
using namespace std; | |
//both foo() and bar() try to do the same thing: | |
//print the first elemnt in c and append it to c. | |
template <typename Container> | |
void foo(Container &c) | |
{ | |
assert( !c.empty() ); | |
auto &head = *(begin(c)); | |
c.push_back(head); | |
cout << head << endl; | |
} | |
template <typename Container> | |
void bar(Container &c) | |
{ | |
assert( !c.empty() ); | |
auto head = begin(c); | |
c.push_back(*head); | |
cout << *head << endl; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
{//1 | |
vector<int> c{1, 2, 3, 4}; | |
foo(c); | |
} | |
{//2 | |
vector<int> c{1, 2, 3, 4}; | |
bar(c); | |
} | |
{//3 | |
list<int> c{1, 2, 3, 4}; | |
foo(c); | |
} | |
{//4 | |
list<int> c{1, 2, 3, 4}; | |
bar(c); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment