std::make_unsigned 没有给出预期的结果
std::make_unsigned not giving expected results
我有这样的东西:
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;
}
我正在尝试获取传递到模板参数列表中的 integral
类型,并制作它的 unsigned
版本并将其初始化为 -1
。这应该给我任何 integral type
所需的值。
我使用 std::make_unsigned
正确吗?
我的错误 遗漏了声明中的关键字 class
... 修正了拼写错误。
编辑 - 我的实际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;
}
};
它的用途:
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;
}
Am I using std::make_unsigned
correctly?
不完全是。修复:
typedef typename std::make_unsigned<T>::type unsigned_t;
您可以使用 std::bitset<>::to_string
函数将任何整数类型的值转换为其二进制字符串表示形式:
template<class T>
std::string as_binary_string(T value) {
return std::bitset<sizeof(T) * 8>(value).to_string();
}
越野车循环
我什至不知道你想用那个循环实现什么,但它肯定不会做你想要的。此外,您在循环中混合了有符号和无符号类型,所以这是完全错误的。
您似乎想要打印无符号类型的二进制表示。 Jerry 有一个很老的post。
您的代码:
我认为您应该直接存储 std::string
,或者 std::bitset
。要填充前者,只需使用:
for (std::size_t i = 0; i < sizeof(UnsignedIntegral) * CHAR_BIT; ++i)
{
bview += (value % 2) '1' : '0';
value /= 2;
}
std::reverse(bview.begin(), bview.end());
demo.
总的来说,您的逻辑似乎很复杂,并且您认为编译器无法将 2 的幂除法优化为 bitshift。如果您不是在过于奇特的系统上,编译器将始终对此进行优化,以及更多。
我有这样的东西:
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;
}
我正在尝试获取传递到模板参数列表中的 integral
类型,并制作它的 unsigned
版本并将其初始化为 -1
。这应该给我任何 integral type
所需的值。
我使用 std::make_unsigned
正确吗?
我的错误 遗漏了声明中的关键字 class
... 修正了拼写错误。
编辑 - 我的实际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;
}
};
它的用途:
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;
}
Am I using
std::make_unsigned
correctly?
不完全是。修复:
typedef typename std::make_unsigned<T>::type unsigned_t;
您可以使用 std::bitset<>::to_string
函数将任何整数类型的值转换为其二进制字符串表示形式:
template<class T>
std::string as_binary_string(T value) {
return std::bitset<sizeof(T) * 8>(value).to_string();
}
越野车循环
我什至不知道你想用那个循环实现什么,但它肯定不会做你想要的。此外,您在循环中混合了有符号和无符号类型,所以这是完全错误的。
您似乎想要打印无符号类型的二进制表示。 Jerry 有一个很老的post。
您的代码:
我认为您应该直接存储 std::string
,或者 std::bitset
。要填充前者,只需使用:
for (std::size_t i = 0; i < sizeof(UnsignedIntegral) * CHAR_BIT; ++i)
{
bview += (value % 2) '1' : '0';
value /= 2;
}
std::reverse(bview.begin(), bview.end());
demo.
总的来说,您的逻辑似乎很复杂,并且您认为编译器无法将 2 的幂除法优化为 bitshift。如果您不是在过于奇特的系统上,编译器将始终对此进行优化,以及更多。