在mingw g 4.7.2中未申报

decltype undeclared in mingw g++ 4.7.2

本文关键字:mingw      更新时间:2023-10-16

出于某种原因或其他原因,当试图在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外,任何人都可以解决任何想法?

decltype是C 11功能。您需要以这种方式致电GCC

g++ -std=c++11