Skip to content

Instantly share code, notes, and snippets.

@egpbos
Created October 10, 2017 12:09
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 egpbos/71b7beb7f1ed85f6a9496aaf50e1ec73 to your computer and use it in GitHub Desktop.
Save egpbos/71b7beb7f1ed85f6a9496aaf50e1ec73 to your computer and use it in GitHub Desktop.
#include <complex>
#include <iostream>
template <typename T> using real_t = T;
template < typename precision, template <typename> class input_t, template <typename> class output_t >
void do_stuff(input_t<precision> a, output_t<precision> b) {
std::cout << a + b << std::endl;
}
int main() {
real_t<double> a = 1.;
real_t<double> b = 2.;
std::complex<double> c {0., 10.};
std::complex<double> d {0., 33.};
do_stuff<double, real_t, real_t>(a, b);
do_stuff<double, real_t, std::complex>(a, c);
do_stuff<double, std::complex, std::complex>(c, d);
do_stuff<double, std::complex, real_t>(c, b);
/* prints:
3
(1,10)
(0,43)
(2,10) */
do_stuff(a, b);
do_stuff(a, c);
do_stuff(c, d);
do_stuff(c, b);
/* does not compile:
note: template argument deduction/substitution failed:
note: can't deduce a template for 'input_t<precision>' from non-template type 'double' */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment