Some programming notes: C++ 11 features

MusE is a MIDI/Audio sequencer with recording and editing capabilities, aiming to be a complete multitrack virtual studio for Linux. http://muse-sequencer.org/

Moderators: khz, MattKingUSA, spamatica

Tim E. Real
Established Member
Posts: 273
Joined: Sat Sep 15, 2012 12:36 am

Some programming notes: C++ 11 features

Postby Tim E. Real » Fri Mar 08, 2019 9:33 pm

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: 1439
Joined: Sat Jun 11, 2016 12:05 am

Re: Some programming notes: C++ 11 features

Postby jonetsu » Fri Mar 08, 2019 10:12 pm

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: 28
Joined: Sun Apr 03, 2016 12:36 pm
Contact:

Re: Some programming notes: C++ 11 features

Postby empowerg » Sat Mar 09, 2019 4:02 pm

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: 273
Joined: Sat Sep 15, 2012 12:36 am

Re: Some programming notes: C++ 11 features

Postby Tim E. Real » Mon Mar 25, 2019 3:49 am

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: 137
Joined: Wed Feb 12, 2014 4:39 am
Location: South Africa

Re: Some programming notes: C++ 11 features

Postby noedig » Mon Mar 25, 2019 5:50 am

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


Return to “MusE Sequencer”

Who is online

Users browsing this forum: No registered users and 6 guests