メタ関数は、こんな形式の関数
meta-function<type ...>::value()typeに対してプリミティブ型でなく、typedefした型を入力すれば、型の変更に合わせてコードを書き換える必要なく値を取得できる。
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
// | |
// limits.cpp | |
// CplusplusPractice | |
// | |
// Created by masai on 2015/05/19. | |
// Copyright (c) 2015年 masai. All rights reserved. | |
// | |
#include <iostream> | |
#include <limits> | |
typedef long HashValueLong; | |
typedef unsigned long HashValueULong; | |
int main(int argc, const char * argv[]) { | |
// マクロ定義による取得 | |
std::cout << "int max: " << INT_MAX << std::endl; | |
std::cout << "int min: " << INT_MIN << std::endl; | |
// クラステンプレートによる取得 | |
std::cout << "int max: " << std::numeric_limits<int>::max() << std::endl; //2147483647 | |
std::cout << "long max: " << std::numeric_limits<long>::max() << std::endl; //2147483647 | |
// 型に依存しない書き方 | |
std::cout << "HashValue(long): " << std::numeric_limits<HashValueLong>::max() << std::endl; | |
std::cout << "HashValue(unsigned long): " << std::numeric_limits<HashValueULong>::max() << std::endl; | |
return 0; | |
} |
参考サイト
C++のプリミティブ型が取りうる最大値を取得する。
https://www.c3.club.kyutech.ac.jp/archives/1203
0 件のコメント:
コメントを投稿