How can I compile C++11 code with Orwell Dev-C++?
Trying to compile the following code:
#include <iostream>
#include <memory>
struct Foo {
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
void bar() { std::cout << "Foo::bar\n"; }
};
void f(const Foo &foo)
{
std::cout << "f(const Foo&)\n";
}
int main()
{
std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo
if (p1) p1->bar();
{
std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo
f(*p2);
p1 = std::move(p2); // ownership returns to p1
std::cout << "destroying p2...\n";
}
if (p1) p1->bar();
// Foo instance is destroyed when p1 goes out of scope
}
with Orwell Dev-c++ 5.3.0.3 yields the following error:
'unique_ptr' is not a member of 'std'.
How can I handle this?
Please make sure you supply the correct -std flag when compiling. The default setting that Orwell Dev-C++ uses (don't pass any -std option), will not enable some shiny new C++11 functions, like unique_ptr. The fix is quite simple:
- For non-project compilations, go to: Tools >> Compiler Options >> (select your compiler) >> Settings >> Code Generation >> (set 'Language standard' to a C++11 option)
- For project compilations, go to: Project >> Compiler >> Code Generation >> (set 'Language standard' to a C++11 option)
Here's a bit more information about the -std flag: http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options
As you can see, GCC uses a GNU dialect of C++03 by default (which doesn't seem to support unique_ptr).