I don't get c++11.

Started by
34 comments, last by tanzanite7 10 years, 1 month ago

c++11 to me was largely a quality of life improvement. Some other features that haven't been mentioned:

Non-static data member initialization - Being able to declare the default value of a member variable at declaration ( where it should have been ) instead of some constructor somewhere else. ( My personal favorite )

Delegating / Inheriting constructors - How many times have you had to write an init() because of multiple constructors.

constexpr - Kind of like a type safe macro's.

--Z

Advertisement
I also like the new initializer lists, because they make my code more clear in some common situations:

  for (int delta : {-8, -1, +1, +8}) {
    // ...
  }

  // ...

  std::map<std::string, std::string> dictionary = {
    {"spring", "muelle"},
    {"summer", "verano"},
    {"fall", "caida"},
    {"winter", "invierno"}
  };

Rant (slightly off topic):

And still, in 2014, Microsoft does not fully support the standard. The latest thing I came across: No defaulted move constructors or defaulted move assignment operators.

It's quite remarkable that the "convener" of the ISO C++ committee, Herb Sutter, is MS employee...

I have grown to accept MSVCs deficiencies. I can live with them when I'm being paid.

For my personal projects I have moved over to QtCreator + MinGW though. After some initial friction (QtCreator can be a bit quirky when you are not used it) the only thing I'm actually missing is MSVC's debugger.

So long as you try not to overly consume language features and kinda stick to the lowest common denominator, I find that code should remain portable with older / non-standard compilers.

The really annoying one for me was when std::shared_ptr<T> was in the tr1 namespace on some compilers (std::tr1::shared_ptr<T>) for seemingly a very long time. I had to use a macro to attempt to hack round it whilst fighting off suggestions of "use boost::shared_ptr<T>". (Javascript developers here might feel the same way when every sample code around the internet imposes jquery on your codebase).

I am looking forward to the day however when Clang is working fully on OpenBSD and I can ditch eg++ but retain the C++11 features!

Edit: Stroustrup actually summarizes my issues with boost quite well here

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


Edit: Stroustrup actually summarizes my issues with boost quite well here

This made me lough:

"Some of the libraries are too clever for my taste. Sometimes, generality and simplicity coincide; in Boost, the balance is IMO too often so far towards generality that novices and average users are lost."

And this says Stroustrup!

Somewhere within Stroustrup, there is a simple man struggling to get out ;)

Saying that, I have recently ordered his latest (4th Edition) book (The C++ Programming Language) since I really like the way he explains the language. It is almost as enjoyable as the K&R C book (but oh so much longer ;)

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

The really annoying one for me was when std::shared_ptr<T> was in the tr1 namespace on some compilers (std::tr1::shared_ptr<T>) for seemingly a very long time. I had to use a macro to attempt to hack round it whilst fighting off suggestions of "use boost::shared_ptr<T>".

When using the 'using' keyword, you don't have to bring it into the 'global' scope, you can bring it into existing or new namespaces as well. Macroes aren't necessary.


#ifdef SOME_COMPILER
namespace std //Within the 'std' namespace...
{
    using tr1::shared_ptr; //...bring 'shared_ptr' down to the 'std' namespace.
}
#endif

[example]

Hmm, that is a much better solution than the one I was using during that period. Cheers for the suggestion!

Edit: On further inspection of this. I guess I would still need to include a header with this in or add it to each file. With the macro hack, I could pass it in as a compile time flag (-D) and leave the rest of the code untouched.

I can't recall the define and cannot even remember which projects I used it in but I think it was something similar to:

#define shared_ptr tr1::shared_ptr

Though, I remember it being quite non-trivial since the "::" caused complexities within that macro substitution.

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

Lambdas can only be defined and used inside functions:

O_o, wut? Lambda is a closure object - saying that it can only be defined / used inside functions makes as much sense than saying one can use/define the value 7 ONLY inside functions. Do not get confused that the lambda object is called "closure object" - that too is still just an object. In short, there is no such limitation.

Perhaps you meant that defining a capturing Lambda outside functions is conceptually kinda totally bonkers -> capture list requires function scope.


Another discrepancy i noticed:

Lambda object defines a few operators. One that seemed to cause a bit unfortunate/confusing wordings here is: type conversion operator to the underlying function type (only defined if the lambda does not capture anything - as otherwise the operator would not make any sense). IE. Lambda object is NOT just a function, however it might be possible to get a function pointer out of one via implicit/explicit conversion:

void (*snafu)() = [ ]()->void { ... } // this is legal through conversion operator
void (*snafu)() = [&]()->void { ... } // ... this is not as there is no applicable conversion.

This topic is closed to new replies.

Advertisement