【问题标题】:decltype undeclared in mingw g++ 4.7.2mingw g++ 4.7.2 中未声明的 decltype
【发布时间】:2012-12-19 17:56:06
【问题描述】:

由于某种原因,在 mingw 上尝试在 G++ 中编译以下代码时

#include <iostream>
#include <string>
#include <cctype>

int main( int argc, char **argv )
{
    std::string s( "Hello, World!" );

    decltype( s.size(  ) ) punct_cnt = 0;

    for ( auto c : s )
    {
        if ( ispunct( c ) )
            ++punct_cnt;
    }

    std::cout << punct_cnt << " punctuation characters in " << s << std::endl;

    return 0;
}

我收到以下错误

test.cpp: In function 'int main(int, char**)':
test.cpp:9:23: error: 'decltype' was not declared in this scope
test.cpp:9:25: error: expected ';' before 'punct_cnt'
test.cpp:11:13: error: 'c' does not name a type
test.cpp:17:2: error: expected ';' before 'std'
test.cpp:17:15: error: 'punct_cnt' was not declared in this scope
test.cpp:19:2: error: expected primary-expression before 'return'
test.cpp:19:2: error: expected ')' before 'return'

我已经检查了 g++ 编译器的版本是 4.7.2,除了将 decltype 更改为 std::string::size_type 之外,有人知道我可以如何解决吗?

【问题讨论】:

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


    【解决方案1】:

    decltype 是一个 c++11 功能。你需要这样调用gcc

    g++ -std=c++11
    

    【讨论】:

    • @ctor 尽量避免使用 decltype,它并不像看起来那么好,基本上标准并不能确保你在 decltype 方面一无所获。
    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多