7

OS is windows.

I'll start off by saying that I have no experience with C++, or any other compiled language. I've used CPython a bit and am familiar with that, but, until earlier today, I'd never even glanced at C++ source.

I'm trying to teach myself C++, so I've been playing around with it a bit, and one problem I'm having is the error:

error: 'to_string' was not declared in this scope

Apparently, to_string is a C++11 thing, which should be fine. I downloaded the latest MinGW, added it to my path - I have checked, and running

g++ - v

does indeed tell me that I have version 4.8.1 installed. The IDE I'm working with, Code::Blocks finds it no problem, but it simply won't use any of the C++11 stuff, giving me errors such as the one above. Things not exclusive to C++11 compile fine.

There is a section under compiler flags to "follow the C++11 language standard", which I have checked, but, even then, I get the same errors. I'm really not sure what's going on - I've looked this up, and all of the suggestions are to update either the IDE or MinGW (both of which are up to date), or to select that flag, which, as I said, is already selected.

Does anyone with more experience with C++ have any idea what might be going on?b

Kevin
  • 1,870
  • 2
  • 20
  • 22
  • Have you set `-std=c++11`? Also, http://stackoverflow.com/q/12975341/1619294 – Mark Garcia Dec 12 '13 at 03:52
  • maybe you forgot `std::` or `using namespace std;` or forgot to include `` – pyCthon Dec 12 '13 at 03:52
  • @Mark, is that not the compiler flag I mentioned? Also, according to your link, this was fixed in a (as of then) more recent version. – Kevin Dec 12 '13 at 03:54
  • @pyCthon - I've tested my code online with ideone, it works no problems. So it's definitely not namespace issues. – Kevin Dec 12 '13 at 03:54
  • @Kevin Obviously it's a configuration issue. Codeblocks is packaged with TDM 4.7.1, and if you are using a custom mingw, then it is not configured properly. –  Dec 12 '13 at 03:56
  • It appears to be a mingw bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015 – David Brown Dec 12 '13 at 03:57
  • @remyabel - That's what I guess, but I've tinkered around with settings quite a bit and haven't managed to figure it out. – Kevin Dec 12 '13 at 03:58
  • @DavidBrown That bug is marked 'fixed'. – Kevin Dec 12 '13 at 03:59
  • @Kevin Yeah, but reading through the comments it appears appears there is a disagreement between some gcc and mingw devs about who's responsibility it is to fix it so gcc has marked it as fixed. – David Brown Dec 12 '13 at 04:00
  • @Kevin Either way, you should fix your tags. Add 'mingw' and remove 'scope' and 'gcc'. –  Dec 12 '13 at 04:01
  • 1
    @Kevin and it looks like it's been fixed in mingw trunk http://sourceforge.net/apps/trac/mingw-w64/wiki/to_string – David Brown Dec 12 '13 at 04:02
  • MinGW and mingw-w64 are **two** different projects, exactly which are you using? – shhyou Sep 11 '14 at 03:43

4 Answers4

10

My understanding is that, other than regex support, G++'s C++11 support is largely complete with 4.8.1.

The following two links highlight the status of C++11 support in G++ 4.8.1 and libstdc++:

To compile C++11 code, though, you need to include the command line flag -std=c++11 when you compile.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • 1
    The core language is complete, but in the library a lot is missing (Unicode conversions are probably the most prominent) – Cubbi Dec 12 '13 at 04:15
  • @Cubbi : Good to know. I put some links up there to the GCC 4.8.x and libstdc++ status pages covering C++11. Hopefully those are helpful as well. – Joe Z Dec 12 '13 at 04:20
  • 2
    The core language was not complete in the g++ 4.8 series. It did not handle rvalue references correctly. – David Hammen May 01 '16 at 17:10
2

std::string is not compliant; it still uses legacy (and buggy especially with threads) 'copy on write' behavior. You might be able to use __gnu_cxx::__vstring as a workaround.

"Class template basic_string Partial Non-conforming Copy-On-Write implementation"

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x https://gcc.gnu.org/bugzilla/show_bug.cgi?id=21334

Related: Does g++ meets std::string C++11 requirements

Community
  • 1
  • 1
KP99
  • 378
  • 1
  • 9
2

The g++ 4.8 series was not complete with regard to the core language. The following compiles and runs with g++ 4.9 and higher (and also with clang++ 3.3 and higher), but not with g++ 4.8.5 (or with any previous member of the g++ 4.8 series).

#include <iostream>

void ordinary_function (int&)  { std::cout << "ordinary_function(int&)\n"; }   

void ordinary_function (int&&) { std::cout << "ordinary_function(int&&)\n"; }   

template<class T>
void template_function (T&)  { std::cout << "template_function(T&)\n"; }   

template<class T>
void template_function (T&&) { std::cout << "template_function(T&&)\n"; }   

int main () {   
    int i = 42; 
    ordinary_function(42); // Not ambiguous.
    ordinary_function(i);  // Not ambiguous.
    template_function(42); // Not ambiguous.
    template_function(i);  // Ambiguous in g++4.8.
}   
David Hammen
  • 32,454
  • 9
  • 60
  • 108
0

The behavior of std::string and std::list is not conforming C++11 standard. The document of libstdc++ is here [ https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html ] A macro named _GLIBCXX_USE_CXX11_ABI can control the behavior of them in GCC5/higher.

Besides that, C++11 std::function in GCC-4.8 is different from GCC-5 and higher version, and there is no macro can control the behavior of std::function. [ Undocumented ABI changes of std::function between GCC-4 and GCC-5/6/7/8/9, how to make a .so working with devtoolset-4/6/7/8/9? ]

The C++11 in GCC-4.8 is considered as experimental support, so do not depend on it much.

Caleb
  • 124,013
  • 19
  • 183
  • 272
Kirby Zhou
  • 161
  • 1
  • 10