What does the auto c++ keyword do? [duplicate] - c++

This question already has answers here:
What is the point of the 'auto' keyword?
(8 answers)
C++ auto keyword. Why is it magic?
(8 answers)
Closed 9 years ago.
I recently came accross the keyword auto in c++.
In the code:
auto maxIterator = std::max_element(&spec[0], &spec[sampleSize]);
float maxVol = *maxIterator;
// Normalize
if (maxVol != 0)
std::transform(&spec[0], &spec[sampleSize], &spec[0], [maxVol] (float dB) -> float { return dB / maxVol; });
This is to do with running a frequency analysis on a audio stream.
From the website: http://katyscode.wordpress.com/2013/01/16/cutting-your-teeth-on-fmod-part-4-frequency-analysis-graphic-equalizer-beat-detection-and-bpm-estimation/
I have searched the forums but It says that there is no use for the keyword. Could someone please explain the use of it here.
I am quite new to c++ so please try not to make the answers too complicated.
Thanks so much all.
Did auto make maxIterator a pointer also?

Compiler guess maxIterator's type. If spec's type is float [], maxIterator type is float *.

In C++11, the keyword auto deduces the type of declared variable from its initialization expression. Hence, in your code it deduces type of maxIterator.
For more information on auto look here

Related

What does (void) inside of a function does? [duplicate]

This question already has answers here:
Why cast unused return values to void?
(10 answers)
What does casting to `void` really do? [duplicate]
(4 answers)
Closed 5 months ago.
I was working and suddenly I faced a function like that:
void func(SomeStruct *parameter) { (void)parameter; }
I could not wrap my head on what it does.
I mean, there is a function func which the only thing it does is (void)parameter; but I can't get what (void)parameter; is doing int this example.
Would someone mind to explain me, please?

what is the purpose of the [] that follow after auto keyword? [duplicate]

This question already has answers here:
What is the auto Bracketed List Syntax?
(1 answer)
Why do C++17 structured bindings not use { }?
(5 answers)
Why structured bindings only work with auto
(2 answers)
Closed 8 months ago.
Hi I came across this example
auto [ptr, ec] { std::from_chars(str.data(), str.data() + str.size(), result) };
Can someone please explain the what is the syntax autp [ptr,ec] {} , this is something I am seeing for the first time. Is this new in C++ ?
Any link to the documentation will be very much appreciated.
Thanks

square bracket and the pair type in C++ [duplicate]

This question already has answers here:
Understand structured binding in C++17 by analogy
(1 answer)
What are the new features in C++17?
(1 answer)
Structured binding and tie()
(2 answers)
Closed 4 years ago.
I read a piece of code from a book but I do not really understand the grammar,
const auto& [local_min, local_max] = minmax(A[i], A[i+1]);
where A is vector of int and local_min and local_max are int.
I know minmax returns a pair, but what does the square brackets do, i.e. [local_min, local_max]? I guess it is not for arrays here.
Thanks.

What's the difference between using-style and typedef-style? [duplicate]

This question already has answers here:
What is the difference between 'typedef' and 'using' in C++11?
(8 answers)
Closed 9 years ago.
using IntegerType1 = int;
typedef int IntegerType2;
int main()
{
IntegerType1 n1 = 1; // OK
IntegerType2 n2 = 2; // OK
}
My questions are:
What's the difference between using-style and typedef-style?
As we already have typedef-style, what's the motivation to make using-style become a C++ standard?
The "using-style" was introduced to allow templated typedefs:
template< typename T >
using int_map = std::map< int, T >;
You can not do this with typedef. I found it strange myself that it was decided to use using and not typedef as the keyword for this, but I guess the committee must have found some problem with extending the typedef syntax.
I find that readability is greatly improved even for non-templates:
typedef void (*FunctionPtr)(); // right-to-left, identifier in the middle of the definition
using FunctionPtr = void (*)(); // left-to-right, same as variables
It's probably minor, but in template-metaprogramming this syntactic advantage makes a program easier to read, and makes template metafunctions easier to refactor towards constexpr functions. Essentially replace
using T = type_expression;
constexpr auto v = value_expression;
Furthermore (appeal to authority), it's also in the draft Effective C++11/14 guidelines.

How do I static_assert that a value parsed as const& is the right type? [duplicate]

This question already has answers here:
Is there a type-trait to remove top-level cv and reference at once?
(2 answers)
Closed 8 years ago.
MyClass::MyClass(std::list<int> const& some_sequence)
{
static_assert(
std::is_same<decltype(some_sequence),std::list<int>>::value ,
"some_sequence should be an integer list"
);
}
How do I make the static assert work? The important thing is that the type is an integer list.
Cheers.
There is no need to use static_assert(...): the compiler will make sure that this function is called with a std::list<int>. If you want to make the above code compile, you need to use
MyClass::MyClass(std::list<int> const& some_sequence)
{
static_assert(
std::is_same<decltype(some_sequence),std::list<int> const&>::value ,
"some_sequence should be an integer list"
);
}
some_sequence is declared as std::list<int> const& and that is the type obtained by decltype(some_sequence). This static_assert() will never fail, however.