Some programming notes: C++ 11 features

MusE is a DAW for Linux with both MIDI and Audio editing. https://muse-sequencer.github.io

Moderators: MattKingUSA, khz, spamatica

Post Reply
Tim E. Real
Established Member
Posts: 660
Joined: Sat Sep 15, 2012 12:36 am
Has thanked: 36 times
Been thanked: 105 times

Some programming notes: C++ 11 features

Post by Tim E. Real »

I've been working with some code that used modern techniques.

The new C++11 (OK it's been a few years now) meaning of the 'auto' keyword is amazing !
'Auto' has been around for decades but nobody, including me, has ever really used it.

So when I saw this code I thought I was looking at some weird new language.

As one SO poster said, "it felt fun again, like using Python or something":
https://stackoverflow.com/questions/757 ... s-it-magic

Instead of this:

Code: Select all

for(someclass:iterator it = someinstance.begin(); it != someinstance.end(); ++it)
{
  const somestruct& = it->second;
  ...
}
We can now do this (notice the dot instead of ->) :

Code: Select all

for(auto it : someinstance)
{
  const somestruct& = it.second;
}
And instead of this method definition:

Code: Select all

somereturnstruct someclass::somemethod(int someparam)
{
}
We can now do this bizarre looking thing:

Code: Select all

auto someclass::somemethod(int someparam) -> somereturnstruct
{
}
Cheers.
jonetsu
Established Member
Posts: 2036
Joined: Sat Jun 11, 2016 12:05 am
Has thanked: 10 times
Been thanked: 22 times

Re: Some programming notes: C++ 11 features

Post by jonetsu »

It always comes at some cost though (if done at runtime). So it depends on the context and use. As for me I'm familiarizing myself with exception handling C++17 style.
empowerg
Established Member
Posts: 34
Joined: Sun Apr 03, 2016 12:36 pm
Contact:

Re: Some programming notes: C++ 11 features

Post by empowerg »

Tim E. Real wrote:I've been working with some code that used modern techniques.
The new C++11 (OK it's been a few years now) meaning of the 'auto' keyword is amazing !
'Auto' has been around for decades but nobody, including me, has ever really used it.
Yeah, take a look at Haskell. Every time I get back to C++ I miss a lot of Haskell stuff, including type inference (which is what auto does).

I know your feeling, I have been working on a system where we were effectively locked into C++ from 2000 (so pre-standard from 2003) and had no chance to update to C++11 or even C++17 until recently.
I think the new features really pay off and make code easier to write. Still a lot opportunities to shoot yourself in the foot with C++, but it's already a lot better now.

lg,
Michael
Tim E. Real
Established Member
Posts: 660
Joined: Sat Sep 15, 2012 12:36 am
Has thanked: 36 times
Been thanked: 105 times

Re: Some programming notes: C++ 11 features

Post by Tim E. Real »

Don't worry I've not gone crazy and started adding it to MusE... yet.
If opportunity knocks I might be tempted though.

Of much greater concern I just discovered is that Qt's signals and slots macros mechanism is actually documented as obsolete.
We have hundreds of them.
Any contributors out there should take note of that obsolescence.

I have taken a small step in the metronome dialog (metronome.cpp) to switch to what I believe is called
the "functors and C++11 lambdas" method of connecting in Qt.

But I had trouble. Certain Qt widgets would not let me connect their methods while others were OK.
For example trying to connect &QSpinBox::valueChanged or &QComboBox::activated would not work,
the IDE highlighted them, and the compiler erred.
It's a mystery to me why. The format should be the same as the others that worked such as &QCheckBox::toggled.
What'd I do wrong?

Also Qt 6 is creeping up on us. I hope 5 is supported for a long time.
User avatar
noedig
Established Member
Posts: 233
Joined: Wed Feb 12, 2014 4:39 am
Location: South Africa
Has thanked: 9 times
Been thanked: 52 times

Re: Some programming notes: C++ 11 features

Post by noedig »

Tim E. Real wrote:But I had trouble. Certain Qt widgets would not let me connect their methods while others were OK.
For example trying to connect &QSpinBox::valueChanged or &QComboBox::activated would not work,
the IDE highlighted them, and the compiler erred.
It's a mystery to me why. The format should be the same as the others that worked such as &QCheckBox::toggled.
What'd I do wrong?
QSpinBox::valueChanged and QComboBox::activated are overloaded, so to use the Qt5 signal/slot connect syntax, you unfortunately end up with an ugly cast as explained in
https://stackoverflow.com/questions/167 ... ts-in-qt-5
Post Reply