I have something like this:
template<class T>
class SomeClass {
public:
typedef std::make_unsigned<T> unsigned_t;
unsigned_t maxBinBitRep_ { - 1 };
};
int main() {
// Okay prints out 255 as expected.
SomeClass<unsigned char> unsignedChar; // unsigned version
// New to `make_unsigned<>` but should print 255.
//std::cout << unsignedChar.maxBinBitRep << std::endl;
// Should be printing the same as above however it's printing: 4294967295
SomeClass<char> signedChar;
// same as above.
//std::cout << signedChar.maxBinBitRep << std::endl; // signed version
std::cout << "/nPress any key and enter to quit./n" );
char q;
std::cin >> q;
return 0;
}
I'm trying to take the integral
type that is passed into the template's parameter list and make a unsigned
version of it and initializing it to -1
. This should give me the value that I need for any integral type
.
Am I using std::make_unsigned
correctly?
my mistake miss the keyword class
in the declaration... fixed typo.
EDIT - My Actual Class:
template<class T>
class showBinary {
public:
static const std::size_t standardByteWidth_ { 8 };
private:
typedef std::make_unsigned<T> unsigned_t;
unsigned_t maxVal_ { -1 };
T t_;
std::vector<unsigned> bitPattern_;
std::size_t size_ = sizeof( T );
public:
explicit showBinary( T t ) : t_( t ) {
bitPattern_.resize( size_ * standardByteWidth_ );
for ( int i = ((maxVal_ + 1) / 2); i >= 1; i >>= 1 ) {
if ( t_ & i ) {
bitPattern_.emplace_back( 1 );
} else {
bitPattern_.emplace_back( 0 );
}
}
}
template<typename U>
friend std::ostream& operator<<( std::ostream& out, const showBinary<U>& val ) {
std::ostringstream ostring;
ostring << "Val: " << val.t_ << " ";
ostring << "Max Value: " << val.maxVal_ << "\n";
ostring << "Size in bytes: " << val.size_ << " ";
ostring << "Number of bits: " << val.size_ * val.standardByteWidth_ << "\n";
ostring << "Bit Pattern: ";
for ( auto t : val.bitPattern_ ) {
ostring << t;
}
ostring << std::endl;
out << ostring.str();
return out;
}
};
Its use:
int main() {
showBinary<unsigned char> ucBin( 5 );
showBinary<char> scBin( 5 );
std::cout << ucBin << std::endl;
std::cout << scBin << std::endl;
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return 0;
}