I've seen this one function I have no idea what's going on here:
template <typename Container>
auto MaxElement(Container &c,int num_of_el)->decltype(c[0]){
int index=0;
for(int i=1;i<num_of_el;i++)
if(c[i]>c[index])
index=i;
return c[index];
}
This here is the main part of the program:
int main(){
int a=7;
vector<decltype(a)> v;
v.push_back(a);
a=10;
v.push_back(5);
cout<<v[0]<<" "<<v[1]<<endl;
MaxElement(v,v.size())=10;
cout<<v[0]<<" "<<v[1]<<endl;
return 0;
}
I don't have a problem understanding how MaxElement function works, but rather with things like ->decltype(c[0])? What does that do? Also how can we do something like MaxElement(v,v.size())=10, what happens here?
In combination with
autoas the return type, it declares the return type of the function to be of the same type asc[0], which is anint&.In this case, it means that the type of the function has the same type as a reference to an element in the
Container.This is to help C++11 compilers which can't deduce this with just
auto.A simplified example:
This doesn't work in C++11:
But this does:
In a function returning a non-
constreference to an object, you can assign to that object like any other.Note that
auto(without the trailingdecltype(C[0])in C++14 and forward) will not return a reference to the returned object. It'll return it by-value - and then the assignment would not compile.