std :: make_unsigned未给出预期结果

问题描述 投票:0回答:2

我有这样的事情:

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 ...修复错字。


编辑 - 我的实际课程:

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;        
}
c++ templates unsigned
2个回答
4
投票

我正确使用std::make_unsigned吗?

不完全的。固定:

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();
}

0
投票

越野车循环

我甚至不知道你想用循环实现什么,但它肯定不会做你想要的。你也在循环中混合有符号和无符号类型,所以这是完全错误的。


您似乎想要打印无符号类型的二进制表示。杰里有一个很老的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的幂来优化除以比特移位。如果你不是在过于奇特的系统上,编译器将始终优化它,以及更多。

© www.soinside.com 2019 - 2024. All rights reserved.