int16, int32 etc as template parameter

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

54 Replies - 3802 Views - Last Post: 21 November 2016 - 02:30 PM Rate Topic: -----

#1 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

int16, int32 etc as template parameter

Posted 17 November 2016 - 05:43 AM

so i wrote an Int class, now i want to extend it - make it a template:
template<unsigned int n = 32>
class Int {
  int value;
  //...
};

how should i "force" the class to use, for instance, 16-bit int for Int<16>, etc.? does this even make sense?
Is This A Good Question/Topic? 0
  • +

Replies To: int16, int32 etc as template parameter

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 06:57 AM

Read about template specialization.
Was This Post Helpful? 0
  • +
  • -

#3 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 07:40 AM

but in that case i'll have to write the whole class for 8,16,32,64 - 4 plus one (template-class), 5 times. is that the only way out?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 07:44 AM

Your response indicates that you haven't read about templates. They're designed to do exactly what you want. Read about them!
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:18 AM

If you don't want to pass a type such as int16_t and want to use a number, then maybe you could inherit a class with just the data and specialize that.
Was This Post Helpful? 0
  • +
  • -

#6 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:21 AM

i had definitely read about them before. what i meant was, if i write specializations:
template<>
class Int<8> {
// ...
}

template<>
class Int<16> {
// ...
}

this means that i write the whole class 5 times, aren't i?

This post has been edited by bb8: 17 November 2016 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:27 AM

It means that you don't understand what templates are.
template<class T>
class Int {
  T anInteger;
};


This post has been edited by CTphpnwb: 17 November 2016 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:32 AM

if i was to write
Int<int16_t> i;
, your code would be okay, but i want to pass an int as parameter. in that case i can't write
template<unsinged int n>
class Int {
  n anInteger;
}

This post has been edited by bb8: 17 November 2016 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:36 AM

Maybe this will make it clearer.
#include <iostream>

using namespace std;

template <class T>
class Int {
	T anInteger;

public:
	Int(T x) : anInteger(x) {}
	T getInt() { return anInteger; }
};

int main(int argc, const char * argv[]) {
	int x = 55;
	long y = 8446744073709551615;
	cout << x << " " << y << endl;

	Int<long> testLong(y);
	Int<int> testInt(x);

	cout << testInt.getInt() << " " << testLong.getInt() << endl;

	return 0;
}



If you really need to pass the type as a parameter, I suppose you could do:
	Int<typeof(y)> testLong(y);
	Int<typeof(x)> testInt(x);


but you really should know the type.

This post has been edited by CTphpnwb: 17 November 2016 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#10 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:40 AM

i do understand every example you say, but since int/short/etc. aren't the same size on all machines, i want the user to specify the bits of an int, not an alias (short, long). in that case a number needs to be passed. in class-template, i can't say that anInteger's type is int[n]_t, where n is the bit count. my question is, an int is passed to the template, is specialization the only way to "make" an class for int8_t from Int<8>?

This post has been edited by bb8: 17 November 2016 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:50 AM

I think you're making this more difficult than it needs to be. Under what circumstances do you find yourself not knowing the type in advance? If you do know the type, where is the problem?
Was This Post Helpful? 0
  • +
  • -

#12 Peter O   User is offline

  • D.I.C Regular

Reputation: 131
  • View blog
  • Posts: 309
  • Joined: 19-October 13

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:56 AM

You could create another class template that only has the purpose of mapping the number of bits to the correct type.
template<unsigned int n> struct fixed_width_int;
template<> struct fixed_width_int<8> { typedef std::int8_t type; };
template<> struct fixed_width_int<16> { typedef std::int16_t type; };
template<> struct fixed_width_int<32> { typedef std::int32_t type; };
template<> struct fixed_width_int<64> { typedef std::int64_t type; };

template<unsigned int N>
class Int
{
	typedef typename fixed_width_int<N>::type T;
	T value;
};

This post has been edited by Peter O: 17 November 2016 - 08:57 AM

Was This Post Helpful? 2
  • +
  • -

#13 bb8   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 335
  • Joined: 31-January 16

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:56 AM

i said why in the in the previous answer. i mean, if on one machine sizeof(int)!=4, but they want a variable that has 32 bits, they can specify it as a parameter (Int<32>), not find out which int has 32 bits and then after that write for exmaple Int<long>. of course your version is much simpler
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 08:59 AM

When dealing with templates you're dealing with types, not sizes.

By the way you need to understand that the int16_t is a typedef of one of the fundamental types. On most systems today that fundamental type is a short int. And the compiler will probably not be able to distinguish the difference between a short and an int16_t.

Edit:

Quote

but they want a variable that has 32 bits, they can specify it as a parameter (Int<32>)

But remember that the intX_t types are optional they may not exist on all implementations.

Jim

This post has been edited by jimblumberg: 17 November 2016 - 09:02 AM

Was This Post Helpful? 1
  • +
  • -

#15 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: int16, int32 etc as template parameter

Posted 17 November 2016 - 09:04 AM

Well, I guess that Peter's solution would work for you, but I still don't see why there's a problem. If you know you want a 32 bit integer regardless of platform, then what's wrong with Int<int32_t> someVariable(0);?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »