Je crois que personne n'est assez cale dans le coin... Meme moi au bout d'1 an de C++ oriente JV, je comprends un peu, mais pas beaucoup.
Bonjour,
Mais non il ne faut pas se faire plus petit qu'une souris. Certaines de ces innovations sont parfaitement compréhensibles.
Le type auto par exemple :
auto -- deduction of a type from an initializer
Consider
auto x = 7;
Here x will have the type int because that's the type of its initializer. In general, we can write
auto x = expression;
and the type of x will be the type of the value computed from "expression".
The use of auto to deduce the type of a variable from its initializer is obviously most useful when that type is either hard to know exactly or hard to write. Consider:
template<class T> void printall(const vector<T>& v)
{
for (auto p = v.begin(); p!=v.end(); ++p)
cout << *p << "\n";
}
In C++98, we'd have to write
template<class T> void printall(const vector<T>& v)
{
for (typename vector<T>: p = v.begin(); p!=v.end(); ++p)
cout << *p << "\n";
}
When the type of a variable depends critically on template argument it can be really hard to write code without auto. For example:
template<class T, class U> void multiply(const vector<T>& vt, const vector<U>& vu)
{
// ...
auto tmp = vt*vu;
// ...
}
The type of tmp should be what you get from multiplying a T by a U, but exactly what that is can be hard for the human reader to figure out, but of course the compiler knows once it has figured out what particular T and U it is dealing with.
The auto feature has the distinction to be the earliest to be suggested and implemented: I had it working in my Cfront implementation in early 1984, but was forced to take it out because of C compatibility problems. Those compatibility problems disappeared when C++98 and C99 accepted the removal of "implicit int"; that is, both languages require every variable and function to be defined with an explicit type. The old meaning of auto ("this is a local variable") is now illegal. Several committee members trawled through millions of lines of code finding only a handful of uses -- and most of those were in test suites or appeared to be bugs.
Being primarily a facility to simplify notation in code, auto does not affect the standard library specification.
See also
the C++ draft section 7.1.6.2, 7.1.6.4, 8.3.5 (for return types)
Jaakko Jarvi, Bjarne Stroustrup, and Gabriel Dos Reis: Deducing the type of variable from its initializer expression (revision 4).
deduction of a type from an initializer. C'est bizarre ce n'est pas une déduction de type par rapport à une initialisation, mais par rapport à une affectation. Sauf si les Américains emploient initializer pour affectation en Français.
Range-for statement
A range for statement allows you to iterate through a "range", which is anything you can iterate through like an STL-sequence defined by a begin() and end(). All standard containers can be used as a range, as can a std:, an initializer list, an array, and anything for which you define begin() and end(), e.g. an istream. For example:
void f(vector<double>& v)
{
for (auto x : v) cout << x << '\n';
for (auto& x : v) ++x; // using a reference to allow us to change the value
}
You can read that as "for all x in v" going through starting with v.begin() and iterating to v.end(). Another example:
for (const auto x : { 1,2,3,5,8,13,21,34 }) cout << x << '\n';
The begin() (and end()) can be a member to be called x.begin() or a free-standing function to be called begin(x). The member version takes precedence.
See also
the C++ draft section 6.5.4 (note: changed not to use concepts)
Thorsten Ottosen: Wording for range-based for-loop (revision 2).
Jonathan Wakely and Bjarne Stroustrup: Range-based for statements and ADL (Option 5 was chosen).
Là rien de bien méchant. C'est bien pour parcourir une série du début à la fin. Mais cela ne sert à rien pour un parcours spécialisé. Il faut savoir qu'en C++ le parcours d'une série par ses indices quand c'est possible, représente une grossièreté qui vous place dans la catégorie des bleus, des losers.
Des peccadilles tout cela, mais si vous avez une idée, n'hésitez pas.
Cordialement.