Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C++

How to get the integer type of a C++ enum

Rate me:
Please Sign up or sign in to vote.
4.94/5 (17 votes)
20 Mar 2015MIT6 min read 63.7K   432   39  
This article will show you how to determine the right integer type for a C++ enum in compile time with template meta-programming.
/*
 
 Copyright (c) 2013 Gergely Mancz
 
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 http://www.opensource.org/licenses/mit-license.html
 
 */

#pragma once
#ifndef __TYPES_H__
#define __TYPES_H__

#include <stdint.h>
#include <limits.h>

typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;

typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;

typedef intmax_t intmax;
typedef uintmax_t uintmax;

namespace Base
{
    
	template <typename _TType> struct Type
    {
        static const bool cIsIntegerType = false;
    };

	template <>
	struct Type<uint8>
	{
        static const bool cIsIntegerType = true;
		static const uint8 cMinValue = 0;
		static const uint8 cMaxValue = UINT8_MAX;
		static const bool cIsSigned = false;
        static const char* const cName;
	};
//	const char* Type<uint8>::cName = "uint8";
	
	template <>
	struct Type<int8>
	{
        static const bool cIsIntegerType = true;
		static const int8 cMinValue = (int8)INT8_MIN;
		static const int8 cMaxValue = (int8)INT8_MAX;
		static const bool cIsSigned = true;
        static const char* const cName;
	};
//	const char* Type<int8>::cName = "int8";
	
	template <>
	struct Type<uint16>
	{
        static const bool cIsIntegerType = true;
		static const uint16 cMinValue = 0;
		static const uint16 cMaxValue = UINT16_MAX;
		static const bool cIsSigned = false;
        static const char* const cName;
	};
//	const char* Type<uint16>::cName = "uint16";
	
	template <>
	struct Type<int16>
	{
        static const bool cIsIntegerType = true;
		static const int16 cMinValue = (int16)INT16_MIN;
		static const int16 cMaxValue = (int16)INT16_MAX;
		static const bool cIsSigned = true;
        static const char* const cName;
	};
//	const char* Type<int16>::cName = "int16";
	
	template <>
	struct Type<uint32>
	{
        static const bool cIsIntegerType = true;
		static const uint32 cMinValue = 0;
		static const uint32 cMaxValue = UINT32_MAX;
		static const bool cIsSigned = false;
        static const char* const cName;
	};
//	const char* Type<uint32>::cName = "uint32";
	
	template <>
	struct Type<int32>
	{
        static const bool cIsIntegerType = true;
		static const int32 cMinValue = (int32)INT32_MIN;
		static const int32 cMaxValue = (int32)INT32_MAX;
		static const bool cIsSigned = true;
        static const char* const cName;
	};
//	const char* Type<int32>::cName = "int32";
	
	template <>
	struct Type<uint64>
	{
        static const bool cIsIntegerType = true;
		static const uint64 cMinValue = 0;
		static const uint64 cMaxValue = (uint64)UINT64_MAX;
		static const bool cIsSigned = false;
        static const char* const cName;
	};
//	const char* Type<uint64>::cName = "uint64";
	
	template <>
	struct Type<int64>
	{
        static const bool cIsIntegerType = true;
		static const int64 cMinValue = (int64)INT64_MIN;
		static const int64 cMaxValue = (int64)INT64_MAX;
		static const bool cIsSigned = true;
        static const char* const cName;
	};
//	const char* Type<int64>::cName = "int64";
	
}

#endif /* TYPES_H_ */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Employed (other) http://www.otpbank.hu
Hungary Hungary

Comments and Discussions