繁体   English   中英

使用size_t的负整数是否安全?

[英]Is it safe to use negative integers with size_t?

我刚看到一些像这样的C ++代码。 它使用一个条件来决定是通过std::vector前进还是后退。 编译器没有抱怨,但我认为size_t是无符号的。 这有危险吗?

vector<int> v { 1,2,3,4,5 };    
bool rev = true;

size_t start, end, di;
if (rev) {
    start = v.size()-1;
    end = -1;
    di = -1;
}
else {
    start = 0;
    end = v.size();
    di = 1;
}

for (auto i=start; i!=end; i+=di) {
    cout << v[i] << endl;
}

通过这种方式很好地定义了使用无符号整数(并且size_t是无符号的),并且有回绕:标准保证了这种行为,而不是有符号整数,标准不保证这种行为。

然而,这是不必要的聪明。

作为一般规则,为了避免由于隐式包装促销到无符号的问题,使用无符号整数用于位级别的东西,使用有符号整数来表示数字。 你需要一个对应于size_t有符号整数,你可以找到ptrdiff_t 定义带有签名结果的n_items函数,例如

using Size = ptrdiff_t;

template< class Container >
auto n_items( Container const& c )
    -> Size
{ return end( c ) - begin( c ); }

你已经准备好了,不再是编译器的愚蠢行为。


而不是太聪明的给定代码

vector<int> v { 1,2,3,4,5 };    
bool rev = true;

size_t start, end, di;
if (rev) {
    start = v.size()-1;
    end = -1;
    di = -1;
}
else {
    start = 0;
    end = v.size();
    di = 1;
}

for (auto i=start; i!=end; i+=di) {
    cout << v[i] << endl;

做例如

const vector<int> v { 1,2,3,4,5 };    
const bool reverse = true;  // whatever

for( int i = 0; i < n_items( v );  ++i )
{
    const int j = (reverse? n_items( v ) - i - 1 : i);
    cout << v[j] << endl;
}

我不能说代码有多安全 ,但我认为这是一种非常糟糕的风格。 更好的方法是使用支持正向或反向迭代的迭代器。

例如:

std::vector<int> v = { 1, 2, 3, 4, 5 };
bool rev = true;

if (rev)
{
    for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
    {
        std::cout << *itr << "\n";
    }
}
else
{
    for (auto itr = v.begin(); itr != v.end(); ++itr)
    {
        std::cout << *itr << "\n";
    }
}

每当我需要处理签名类型时,我总是使用:

typedef std::make_signed<std::size_t>::type ssize_t; // Since C++11

...作为std :: size_t的签名替代。

我很欣赏这个问题已经有几年了,但我希望能帮到别人。 感谢moodycamel :: ConcurrentQueue

使用size_t的负整数是否安全?

不,这很危险。 溢出。

size_t a = -1;
std::cout << a << "\n";

输出:

4294967295 // depends on the system, largest value possible here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM