GCC 4.4没有实现C ++ 11范围循环。它支持哪些其他范围循环语法?

时间:2014-01-15 23:12:00

标签: c++ c++11 g++

我有一些使用某些c++11功能的第三方工具,我需要在gcc 4.4下编译它。由于我对c++11新功能一点都不熟悉,但我认为在谷歌搜索结果毫无结果之后我会寻求帮助。

我已启用c++0x切换功能,但在此处无效:

for (auto const& fixup : self->m_Fixups)

产生的错误是:

error: expected initializer before ':' token

C++11范围循环等效的其他范围循环语法是否支持GCC 4.4

2 个答案:

答案 0 :(得分:7)

代码是一个基于范围的for循环,在C ++ 11中确实是新的。与C ++ 11中的其他一些功能不同,它没有在GCC 4.4中实现。试试这个:

for( auto it = self->m_Fixups.begin(); it != self->m_Fixups.end(); ++it )
{
    const auto& fixup = *it;
    // the rest of the code...
}

以上使用一些 C ++ 11功能,这些功能应该在GCC 4.4中提供。


正如Ben Voigt所指出的:如果你需要提高代码的效率,你也可以使用这个稍微简洁的版本:

for( auto it = self->m_Fixups.begin(), end = self->m_Fixups.end(); it != end; ++it )
{
    const auto& fixup = *it;
    // the rest of the code...
}

答案 1 :(得分:0)

如果你有提升,以下内容应与-std=c++0x一起使用(在gcc 4.4.7,RHEL6系统上测试):

#include <boost/foreach.hpp>

BOOST_FOREACH(const auto &fixup, self->m_Fixups) {
    ...
}