我最近升级到GCC 4.4(MinGW TDM构建),现在以下代码会产生以下警告:

这是代码:

void Console::print( const std::string& str ) {
        std::string newLine( str );
        if( newLine.size() > MAX_LINE_LENGTH ) {
            sf::Uint32 stringSize = newLine.size();
            for( sf::Uint32 insertPos = MAX_LINE_LENGTH;
                    insertPos < stringSize; insertPos += MAX_LINE_LENGTH ) {
                newLine.insert( insertPos, "\n" );
            }
        }

        StringList tokens;
        boost::split( tokens, newLine, boost::is_any_of("\n") );

        for( StringList::iterator it = tokens.begin();
                it != tokens.end(); ++it ) {
            addLine( *it );
        }
    }
有任何想法吗?

正是这样做的优化...
也似乎是造成这一情况的这一行:
boost::split( tokens, newLine, boost::is_any_of("\n") );

是的,我找到了它,它是boost::is_any_of()的参数,通过将其包装在string()构造函数中,警告消失了,谢谢大家的帮助:)
boost::split( tokens, newLine, boost::is_any_of( string( "\n" ) ) );

最佳答案

得到了同样的错误。作为解决方法,我更换了

is_any_of(" ")


is_from_range(' ', ' ')

这也可能会稍微更有效率。

关于C++ GCC4.4警告: array subscript is above array bounds,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1168525/

10-10 17:53
查看更多