Mingw does not support C++ 11?

Hi,
I am using the Mingw compiler but this code does not work

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <memory>
using namespace std;

int main()
{
    shared_ptr<int> sp(new int(60));

    cout << "Hello world!" << endl;
    return 0;
}


The compiler tells me shared_ptr is not declared in this scope. Does mingw not support C++ 11?
It does, but you have to tell it to.

g++ -std=c++11 foo.cpp

Depending on the age of your compiler, you might have to specify c++0x.

Hope this helps.
Favour std::make_shared<> (exception safe, and usually more efficient) over new
http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared

1
2
// std::shared_ptr<int> sp(new int(60));
auto sp = std::make_shared<int>(20) ;
Last edited on
I'd say it's safer but I'm not sure where you get more efficient from...?
one allocation is more efficient than two allocations
Depending on the age of your compiler, you might have to specify c++0x.

This. Saying "using MinGW" is like saying "using car". The version of MinGW dictates what it supports, just like a version of car affects its features (Ford T-Model vs Bugatti Veyron).

MinGW is GCC and GCC supports multiple standards, but its default is conservative.
1
2
// std::shared_ptr<int> sp(new int(60));
auto sp = std::make_shared<int>(20) ;

one allocation is more efficient than two allocations

Oh, in comparison to what OP used. Apologies for not paying attention....
Thanks guys I toggled a compiler flag in codeblocks and now it is working
Using this code:

1
2
3
4
5
   auto sp = make_shared<int>(20);
    auto sp2 = sp;
    cout << *sp << endl;
    cout << *sp2 << endl;
    return 0;


Will automatically free the memory once all instances have left their scope?
Yes.
Topic archived. No new replies allowed.