Skip to content

Instantly share code, notes, and snippets.

@cbsmith
Created March 1, 2014 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbsmith/9284195 to your computer and use it in GitHub Desktop.
Save cbsmith/9284195 to your computer and use it in GitHub Desktop.
Some examples of template argument deduction
// -*- compile-command: "clang++ -ggdb -std=c++11 -stdlib=libc++ -Wall -Werror tad_test.cpp -o tad_test" -*-
#include <iostream>
using namespace std;
template <typename T, typename U>
struct Foo {
T v;
U w;
};
template <typename T, typename U>
Foo<T,U> make_foo(const T& x, U v);
template <typename T, typename U>
Foo<T,U> make_foo(const T& x, U v) {
return Foo<T,U>{x, v};
}
template <template<typename, typename> class C, typename T, typename U>
C<T,U> copy(C<T,U>& source) {
return source;
}
template <typename T>
T add(T x, T y) {
return x + y;
}
int main(int argc, const char* argv[]) {
const long x = 42001201001201L;
auto f = make_foo(x, 42);
auto g = copy(f);
cout << g.v << endl;
cout << g.w << endl;
cout << (42 + 11.0) << endl;
cout << add(42, 11) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment