I use Eclipse CDT as my C++ editor of choice on my Mac at work and at Home.
It’s really a very good editor for C++. Xcode, is a decent editor. When doing iphone / mac OSX projects with Objective-C its an amazing editor. However it is very bad at C++, in my opinion Xcode 4 is much worse than Xcode 3 at C++ code editing.
Is it a conspiracy to nudge developers towards developing using (basically) Apple’s own Objective-C programming language? Maybe I’m not sure, however I do love Objective-C, it’s my second and sometimes first favorite programing language, behind only C++.
To be honest it’s probably not, since they support the Clang+LLVM project which supports C++11, in fact it supports more features than GCC4.7 and is a faster compiler.
C++ has matured so much in the last few years, and a large part of that is due to the Boost libraries. In fact Boost, is sort of used as a testing ground for C++ – and many parts of C++0x11 started off as Boost Libraries ( for-each, any containers, lambdas, smart pointers, thread ).
As an OSX affectionato, I have to admit that I was upset that my latest version of OSX ( Lion at the time of writing ) was using GCC 4.2… GCC 4.2.1 is from July 2007!. The compiler and accompanying library is over 5 years old! In computer years, from what I sophisticatedly computed, thats 1 zillion years ago. That doesn’t seem right… Apple is defineintly making amzing strides with LLVM+Clang compiler, it’s actually better and faster than the latest GCC (4.7 at time of writing), however Xcode is such aTERRIBLE C++ editor, often not providing auto-complete, and not being able to find references until upto a minute after click. One minute? That’s crazy.
Hearing all this talk about how great C++0x11 was, I wanted to use it. To be honest, a lot of the stuff can be faked using the amazing Boost libraries – some of which are now standard. However I felt ripped off, these are officially part of C++ I should be able to use them now right!? Well not on OSX you can’t – well at least not without a little bit of work.
In tomorrow’s post I’ll guide you through the steps I went through to get C++11 to work on my OSX 10.6 / 10.7 mac.
As a preview, all of these features I found from the C++11 wikipedia page compiled and work correctly.
/*
* main.cpp
*
* Created on: Feb 7, 2012
* Author: onedayitwillmake
*/
//============================================================================
// Name : HelloCPP11.cpp
// Author : Mario Gonzalez
// Version :
// Copyright : (c) Mario Gonzalez
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
#include
#include
using namespace std;
void hello() {
std::cout << "Hello Concurrent World\n";
}
int main(int argc, char *argv[]) {
std::vector myvec;
// Constant iterators
for (std::vector::const_iterator itr = myvec.cbegin();
itr != myvec.cend(); ++itr) {
std::cout << "Hello!" << std::endl;
}
// Auto!
for (auto itr = myvec.cbegin(); itr != myvec.cend(); ++itr) {
std::cout << "Hello!" << std::endl;
}
// the keyword decltype can be used to determine the type of an expression at compile-time.
// For example:
int some_int;
decltype(some_int) other_integer_variable = 5;
//The type denoted by decltype can be different from the type deduced by auto originally
const std::vector v(1);
auto a = v[0]; // a has type int
decltype(v[0]) b = 1; // b has type const int&, the return type of
// std::vector::operator[](size_type) const
auto c = 0; // c has type int
auto d = c; // d has type int
decltype(c) e; // e has type int, the type of the entity named by c
decltype((c)) f = c; // f has type int&, because (c) is an lvalue
decltype(0) g; // g has type int, because 0 is an rvalue
// Range based loop - ( i always thought these were stupid but whatever)
int my_array[5] = { 1, 2, 3, 4, 5 };
for (int &x : my_array) {
x *= 2;
}
// Initialize standard container easier way, instead of pushback
std::vector alphabetsoup = { "xyzzy", "plugh", "abracadabra" };
// Lamda
[](int x, int y) {return x + y;};
//The following two examples demonstrate usage of a lambda expression:
std::vector some_list;
some_list = {0,1,2,3,4,5}; // Instead of for(int i=0;i<5;i++)some_list.push_back(i);
// FOR EACH! - with LAMDA!
int total = 0;
std::for_each(begin(some_list), end(some_list), [&total](int x) {
total += x;
});
// THREADING!
cout << "starting" << endl;
thread t(hello);
t.join();
cout << "ending" << endl;
}
Your code snippet seems incomplete. Runs out at the end, and the #include code at the start doesn’t make sense.
@Kaitain – Uh oh – looks like something got mangled along the way. Thanks for the heads up, fixed!