I’m on mobile when I get home I’ll post some modern cpp auto use cases
does an advanced c++ programmer know all of C++ features
h8CplusplusGuru said:
I’m on mobile when I get home I’ll post some modern cpp auto use cases
… looking forward that, to learn some things.
Surely nobody criticizes proper usage of auto. It's about general / bad usage, due to lazyness, sort of. I think.
you can pass templated types:
auto print = [&]( const auto& value1, const auto& value2){ std::cout << value1 << ", " << value2;
};
print( std::string("x"), 10 );
print( 255, 0.1f );
you can do initializer lists:
auto x = {0,1,2,3,4,5}; //std::array
//std::array and std::string
for( const auto& name : {"joe", “bob”, “tom” } ){
std::cout << name;
}
you can do structured binding:
auto foo( const auto& data ){
std::array = {0,1,2,3};
return std::make_tuple( std::ref(data), 5, “astring”, array );
};
auto [ data, number, string, array ] = foo( x );
you can return initializers:
auto foo = [&]()→ auto { return {0,1,2,3}; }
auto array = foo();
you can make names less tokenized:
std::vector<std::pair<std::string, float> > pairs = foo();
vs
auto pairs = foo();
the for loop
for( const auto& x: numbers ){}
the delegate:
auto foo = [&]( const auto& x)- > auto { return x+1;}
template<typename Action>
void bar( Action action ){
int z= action( 0 );
}
bar( foo );
I'm on vacation and away from compiler, but I'm pretty sure this is all correct
These are all great, but none are absolutely necessary. When it comes to readability, auto is not good.
To each their own I guess and its only going to become more feature rich in the future
Thanks for the examples. I especially like structured bindings, called destructuring in some other languages. Is there something similar to let bindings in C++?let a = {
let a = 10;
a + 10
};
In old-fashioned C++ it would look like:int a = 10;
int b = a + 10;
Of course the bottom code is shorter in this case so I'm not doing it complete justice. But the point of the let binding is that you do a bunch of stuff in a new scope kind of hidden away more or less saying “Don't care too much what's in that scope, the important thing is that we somehow have declared the variable a".
perry_blueberry said:
Thanks for the examples. I especially like structured bindings, called destructuring in some other languages. Is there something similar to let bindings in C++?
let a = {
let a = 10;
a + 10
};
In old-fashioned C++ it would look like:int a = 10;
int b = a + 10;
Of course the bottom code is shorter in this case so I'm not doing it complete justice. But the point of the let binding is that you do a bunch of stuff in a new scope kind of hidden away more or less saying “Don't care too much what's in that scope, the important thing is that we somehow have declared the variable a".
if you want to create a named scope, you would use a lambda:
auto a = [&]()->auto{ int a = 10; return a+10; };
int b = a();
lambdas are the new c++ paradigm, you should practice them.
you could also accomplish similar using simple unnamed scopes:
int a = 0;
{
int a =10;
::a = a+10; //i think this may work, away from compiler
}
h8CplusplusGuru said:
if you want to create a named scope, you would use a lambda:
auto a = [&]()->auto{ int a = 10; return a+10; }; int b = a();
lambdas are the new c++ paradigm, you should practice them.
Using a lambda seems a bit too verbose I think.
h8CplusplusGuru said:
you could also accomplish similar using simple unnamed scopes:
int a = 0; { int a =10; ::a = a+10; //i think this may work, away from compiler }
I tried this out but ::a
is looking for a
in the global scope. I guess it needs to look for it in the function scope for it to work.