Skip to content

Instantly share code, notes, and snippets.

@fabiogaluppo
Created January 25, 2016 16: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 fabiogaluppo/743932759542f8a11d50 to your computer and use it in GitHub Desktop.
Save fabiogaluppo/743932759542f8a11d50 to your computer and use it in GitHub Desktop.
template <typename T>
struct f_pair { T x; T y; };
template <typename T>
inline f_pair<T> make_square_pair(T x)
{
return f_pair<T>{ x, x * x };
}
template <typename T>
inline T get_y(const f_pair<T>& p)
{
return p.y;
}
#include <iostream>
#include <typeinfo>
template <typename T>
void display(T x)
{
T y = get_y(make_square_pair(x));
std::cout << y << " -> "<< (typeid(y).name()) << "\n";
}
...
display(2); //4 -> int
display(2.71828); //7.38905 -> double
display(std::string("...")); //...... -> class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment