Created
March 1, 2014 02:38
-
-
Save cbsmith/9284195 to your computer and use it in GitHub Desktop.
Some examples of template argument deduction
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
// -*- 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