Created
December 24, 2015 16:26
-
-
Save loliGothicK/2be2ac6157340b967e6c to your computer and use it in GitHub Desktop.
テンプレートが手招きしているよ ref: http://qiita.com/_EnumHack/items/d29eaf2013f753bf8e99
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
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が呼ばれる | |
} | |
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
template < typename T > | |
T max( T a, T b ) | |
{ | |
return a < b ? b : a ; | |
} |
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
max( 1, 2 ) ; |
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
template < typename T > | |
T max( T a, T b ) |
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
max< int >( 1, 2 ); |
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
template < typename T > | |
struct pos_2d{ | |
T x = T{} ; | |
T y = T{} ; | |
} ; |
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
auto&& pos = pos_2d<int>{ 20, 20 } ; |
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
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