Wrapping numerical types in classes?
#1 Members - Reputation: 1010
Posted 22 October 2012 - 10:13 AM
For example a template class for a 3D grid where you pass in the edge lenght. If it was wrapped, you could do something like pow(T.GetMaxValue(),3) so you know how large datatype you need to index the cells (this could be combined with some log and other fancy stuff to get the required byte count and use a template thing to get the best type for the situation)
Pros and cons i came up with:
Pros:
-Easily accessible information about the capabilities of the type
-Easily accessible methods for operating on it (The ide will list all the available methods i assume...)
-Possible to add debug code or such? (not that great benefit though?)
-Makes primitive types more OOP
Cons:
-There would be a lot of methods and code to add (to convert between types etc.)
-Possibly not optimized as well? (I would expect the compiler to make the same code as without wrapping though...)
-Possible issues with existing code?
#3 Crossbones+ - Reputation: 521
Posted 22 October 2012 - 12:25 PM
I don't think it's an efficient way of going about doing it, mainly because it would make your classes too bulky. I mean, you would need another int to store MAX_INT, and another double to store MAX_DOUBLE. Much easier to just find the global defininition and go from there.
#4 Members - Reputation: 1010
Posted 22 October 2012 - 12:39 PM
I thought max/min values were already globally defined... You would als have a seriously large amount of operators to overload.
I don't think it's an efficient way of going about doing it, mainly because it would make your classes too bulky. I mean, you would need another int to store MAX_INT, and another double to store MAX_DOUBLE. Much easier to just find the global defininition and go from there.
The max and min values would of course be static, so there is zero overhead.
I know you can already find this data, but it would ve nicer to have it directly in the class like with everything else...
Would be nice if it was added to some later c++ standard (as the current ints etc are kind of legacy from c?) on top of the legacy stuff.
Maybe even make literals act like classes, so you could do something like
0x1234.BitCount()
would make c++ more consistent in my opinion. (if i go perfectionist i say the language must not have hard coded arithmetic because those clearly should be compiler intrinsics acting on byte arrays xP)
#5 Moderators - Reputation: 4637
Posted 22 October 2012 - 12:48 PM
template<unsigned int bits> struct bitcount {
static unsigned int const value = bitcount<(bits>>1)>::value + 1;
};
template<> struct bitcount<0> {
static unsigned int const value = 0;
};
Now you can get the number of bits of a value by int foo=bitcount<42>::value.
#6 Members - Reputation: 1010
Posted 22 October 2012 - 01:08 PM
Template programming can handle things like that rather easily.
template<unsigned int bits> struct bitcount { static unsigned int const value = bitcount<(bits>>1)>::value + 1; }; template<> struct bitcount<0> { static unsigned int const value = 0; };Now you can get the number of bits of a value by int foo=bitcount<42>::value.
But that code looks nonstandard, i would feel a lot more comfortable using simple methods like for most other things.
And, i believe you wouldnt need to make too much code as you can reuse the same code for all integer types...
something like integer<bits> would be cool.
#9 Members - Reputation: 1010
Posted 22 October 2012 - 01:34 PM
Just like if you made a fixed size array class. You dont create a class for each array size and then create separate template magic to retrieve data about it...
#10 Moderators - Reputation: 4637
Posted 22 October 2012 - 01:56 PM
#11 Moderators - Reputation: 6662
Posted 22 October 2012 - 08:07 PM
You realize that actual computer hardware works with set numbers of bits at a time, right? The reason why your average PC C++ compiler has 8-bit, 16-bit, 32-bit and (usually) 64-bit integers is because those are the number of bits that your CPU works with most efficiently at a time. There's no 13-bit integer type because your standard x86-family processor doesn't have any assembly operations that work with 13-bit operands. If you created your hypothetical integer template, then implementing that template would require some hardcore special template magic in order to work anywhere near efficiently.Just like if you made a fixed size array class. You dont create a class for each array size and then create separate template magic to retrieve data about it...
#12 Members - Reputation: 2772
Posted 22 October 2012 - 11:49 PM
Making primitive types more OO is not a goal of C++ either. C++ is not an "OO language," it's a multi-paradigm language that has OO support.
There's no reason why you can't write a library to do what you propose. It's unlikely to have enough widespread applicability to end up in the standard, but I would have thought the same thing of special numeric functions yet there they are. The usual place for such specialized things is an external library, though.
Professional Free Software Developer
#13 Moderators - Reputation: 13569
Posted 23 October 2012 - 12:29 AM
This is the C#/Java way of thinking ("corrupted OOP" IMO), where everything has to be in a class, not the C++ way of thinking.but it would be nicer to have it directly in the class like with everything else
The first way of thinking leads to #1 below, whereas #2 is more C++ style (in my experience).
//#1
class Foo
{
public: int Bar( int );
void UtilityHelper() { Bar(42); }
private: int m_Baz;
};
//#2
class Foo
{
public: int Bar( int );
private: int m_Baz;
};
void UtilityHelper(Foo& foo) { foo.Bar(42); }The reason is that in #1, the private details such as m_Baz have been unnecessarily exposed to the utility/helper function. This means that if you're investigating a bug involving m_Baz, then you have to treat UtilityHelper as a suspect, increasing the amount of code to read/maintain.In the 2nd style, the utility is known to only have access to the public interface, not the private details.
Edited by Hodgman, 24 October 2012 - 06:36 PM.
#14 Members - Reputation: 1010
Posted 23 October 2012 - 05:42 AM
Maybe utility functions werent a good idea to put there, but is there any major reasons why not to make a class like
integer<minimumBits> (or bytes?)
where it internally has a typedef for the primitive data type to use, and specialized operator etc. templates for each type? (if needed, probably a single template is fine for char, short and int...)
So basically just a class to make the different integer types a single class for the sole purpose of being able to pass in the amount of bits needed and get the best type for it.
C++ guarantees each of the primitive types to have some amount of bits, which probably was fine in C when the programmer had to copy the code anyways if lets say dimension or size changed, but now with templates the computer should do it, which in my opinion is easiest to achieve by making the integers template classes. This also makes them all carry their bit count with them, and doesnt require external template classes to lets say pick tge best integer type for a number of bits or to get the number of bits...
#15 Moderators - Reputation: 4637
Posted 23 October 2012 - 06:24 AM
Edited by Brother Bob, 23 October 2012 - 06:24 AM.
#16 Members - Reputation: 360
Posted 24 October 2012 - 04:25 PM
This is the C#/Java way of thinking ("corrupted OOP" IMO), where everything has to be in a class, not the C++ way of thinking.
but it would be nicer to have it directly in the class like with everything else
The first way of thinking leads to #1 below, whereas #2 is more C++ style (in my experience).//#1 class Foo { public: int Bar( int ); static void UtilityHelper() { Bar(42); } private: int m_Baz; }; //#2 class Foo { public: int Bar( int ); private: int m_Baz; }; void UtilityHelper(Foo& foo) { foo.Bar(42); }The reason is that in #1, the private details such as m_Baz have been unnecessarily exposed to the utility/helper function. This means that if you're investigating a bug involving m_Baz, then you have to treat UtilityHelper as a suspect, increasing the amount of code to read/maintain.
In the 2nd style, the utility is known to only have access to the public interface, not the private details.
Does #1 actually work; on what instance is Bar invoked?
#18 Members - Reputation: 2772
Posted 25 October 2012 - 08:29 AM
The way std::atomic does?I think that as Brother Bob said std::numeric_limits should serve your purpose. However, I don't think wrapping numeric types in templates would be a bad idea otherwise. I'm doing it for atomic variables.
Professional Free Software Developer
#20 Members - Reputation: 2772
Posted 25 October 2012 - 05:00 PM
Professional Free Software Developer






