Page 1 of 1
Some programming notes: C++ 11 features
Posted: Fri Mar 08, 2019 9:33 pm
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.
Re: Some programming notes: C++ 11 features
Posted: Fri Mar 08, 2019 10:12 pm
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.
Re: Some programming notes: C++ 11 features
Posted: Sat Mar 09, 2019 4:02 pm
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
Re: Some programming notes: C++ 11 features
Posted: Mon Mar 25, 2019 3:49 am
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.
Re: Some programming notes: C++ 11 features
Posted: Mon Mar 25, 2019 5:50 am
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