Skip to content

Instantly share code, notes, and snippets.

@loliGothicK
Created December 24, 2015 16:26
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 loliGothicK/2be2ac6157340b967e6c to your computer and use it in GitHub Desktop.
Save loliGothicK/2be2ac6157340b967e6c to your computer and use it in GitHub Desktop.
テンプレートが手招きしているよ ref: http://qiita.com/_EnumHack/items/d29eaf2013f753bf8e99
int max( int a, int b ) // #1
{
return a < b ? b : a ;
}
double max( double a, double b ) // #2
{
return a < b ? b : a ;
}
int main(){
max( 1, 2 ) ; // 1,2はintなので#1が呼ばれる
max( 1.5, 2.3 ) ; // 1.5,2.3はdoubleなので#2が呼ばれる
}
template < typename T >
T max( T a, T b )
{
return a < b ? b : a ;
}
max( 1, 2 ) ;
template < typename T >
T max( T a, T b )
max< int >( 1, 2 );
template < typename T >
struct pos_2d{
T x = T{} ;
T y = T{} ;
} ;
auto&& pos = pos_2d<int>{ 20, 20 } ;
template < typename T >
constexpr T PI = static_cast<T>(3.14159265358979323846L) ;
int main(){
std::cout << PI<int> << std::endl;; // 3
std::cout << PI<double> << std::endl; // 3.14159
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment