Created
September 10, 2015 21:02
-
-
Save kevinkreiser/e89ca55f4f89b5e1b817 to your computer and use it in GitHub Desktop.
When only the return type differs an overload just isn't enough!
This file contains 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 <iostream> | |
#include <list> | |
#include <vector> | |
//the guts that both specializations can use | |
template <class container_t> | |
void fill(container_t& c, int start, int end) { | |
for(;start <= end; ++start) | |
c.push_back(start); | |
} | |
//unspecialed | |
template <class container_t> | |
container_t seq(int start, int end); | |
//specialize for list | |
template <> std::list<int> seq<std::list<int> >(int start, int end) { | |
std::cout << "specialed list: " << std::endl; | |
std::list<int> l; | |
fill(l, start, end); | |
return l; | |
} | |
//specialize for vector | |
template <> std::vector<int> seq<std::vector<int> >(int start, int end) { | |
std::cout << "specialed vector: " << std::endl; | |
std::vector<int> v; | |
v.reserve(end - start); | |
fill(v, start, end); | |
return v; | |
} | |
int main() { | |
auto l = seq<std::list<int> >(0, 10); | |
for(auto e : l) | |
std::cout << e << ' '; | |
std::cout << std::endl; | |
auto v = seq<std::vector<int> >(0, 10); | |
for(auto e : v) | |
std::cout << e << ' '; | |
std::cout << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment