views:

62

answers:

3

I would assume that this is covered in the C++ Standard, but I've not been able to find it. I am writing some templates that are going to do arithmetic on their non-type integral parameters, and I find I need the equivalent of MAX_INT for the parameter 'x' in a template like template <int x> Foo.

Ideally someone could point me to the paragraph in the standard (if one exists) that gives the allowable ranges for the sizes of integral template parameters and any way of determining the actual number of bits on a particular implementation.

--
To clarify: its the templates that will be doing the math within the type system, as part of a metaprogramming library. The 'int' will never be instantiated, and will never take up runtime storage. In many ways its analogous to the math done by the preprocessor, and in that case I know that the integral types are not guaranteed to be the same size as an 'int'. What I am looking for is the part of the standard that says if the types ARE the same or not, and if not, how many bits are used by template integral parameters during compilation.

+3  A: 

See numeric limits. The documentation for std::numeric_limits<>::digits says:

For integer types: number of non-sign bits (radix base digits) in the representation. For floating types: number of digits (in radix base) in the mantissa (equivalent to FLT_MANT_DIG, DBL_MANT_DIG or LDBL_MANT_DIG).

Edit:

There are other methods, such as min(), max() and the like.

André Caron
How do I know that compile-time template calculations use the same size of int as at runtime? The preprocessor doesn't.
swestrup
@swestrup: The template engine knows about (and uses) runtime types. The preprocessor doesn't.
Potatoswatter
+2  A: 

See here: http://www.boost.org/doc/libs/1_41_0/libs/integer/integer_traits.html

Edit: Actually it's not giving you any information about the standard however you can get the min and max value on compile time for certain types.

Edit2: According to your update I can suggest you to use boost.mpl, boost.type_traitsand the before mentioned boost.integer_traits library.

Boost also provides a header called cstdint.hpp (part of the boost.integer library) which defines types like boost::uint32_t or boost::int32_t which will ensure that you get a type which supports 32 Bits. (Of course also for 8, 16 and 64 Bits)

With boost.type_traits you can for example compare two types if they are equal. You'd be using boost::is_same for it.

Boost MPL offers you compile time algorithms, to make conditions etc and integer traits will offer you a compile time way to get the limits for types.

HTH

Vinzenz
I am already using boost.mpl. If it addresses my question, I haven't found it. The integer_traits library also appears to be useless to me, as the values produces are not integer constant expressions and so cannot be used in template math.
swestrup
boost::integer_traits<int>::const_min and boost::integer_traits<int>::const_max are definitely compile time constants. For [selecting between multiple types](http://www.boost.org/doc/libs/1_44_0/libs/mpl/doc/refmanual/type-selection.html) (or values however they are represented as types which contain a value in boost.mpl) or values [based on some conditions](http://www.boost.org/doc/libs/1_44_0/libs/mpl/doc/refmanual/comparisons.html) ( [including logical operations](http://www.boost.org/doc/libs/1_44_0/libs/mpl/doc/refmanual/logical-operations.html) ) boost.mpl fits beautifully.
Vinzenz
There are also [bitwise operations and others see the list for the meta functions](http://www.boost.org/doc/libs/1_44_0/libs/mpl/doc/refmanual/metafunctions.html)
Vinzenz
+2  A: 

You need the numeric_limit class http://www.cplusplus.com/reference/std/limits/numeric_limits/

Specifically,

numeric_limits<T>::max()

will give you CHAR_MAX, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX, INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, FLT_MAX, DBL_MAX or LDBL_MAX, depending on T.

Andrew Stein