Skip to content

Instantly share code, notes, and snippets.

@beyondwdq
Created July 8, 2015 13:51
Show Gist options
  • Save beyondwdq/c90387aa410d6a0e2481 to your computer and use it in GitHub Desktop.
Save beyondwdq/c90387aa410d6a0e2481 to your computer and use it in GitHub Desktop.
#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