what C++ idioms are deprecated in C++11
With the new standard, there are new ways of doing things, and many are nicer than the old ways, but the old way is still fine. It's also clear that the new standard doesn't officially deprecate very much, for backward compatibility reasons. So the question that remains is:
What old ways of coding are definitely inferior to C++11 styles, and what can we now do instead?
In answering this, you may skip the obvious things like "use auto variables".
final
specifier to prevent class derivation shrink_to_fit()
member function, which should eliminate the need swapping with a temporary. I think I'll stop there!
At one point in time it was argued that one should return by const
value instead of just by value:
const A foo();
^^^^^
This was mostly harmless in C++98/03, and may have even caught a few bugs that looked like:
foo() = a;
But returning by const
is contraindicated in C++11 because it inhibits move semantics:
A a = foo(); // foo will copy into a instead of move into it
So just relax and code:
A foo(); // return by non-const value
As soon as you can abandon 0
and NULL
in favor of nullptr
, do so!
In non-generic code the use of 0
or NULL
is not such a big deal. But as soon as you start passing around null pointer constants in generic code the situation quickly changes. When you pass 0
to a template<class T> func(T)
T
gets deduced as an int
and not as a null pointer constant. And it can not be converted back to a null pointer constant after that. This cascades into a quagmire of problems that simply do not exist if the universe used only nullptr
.
C++11 does not deprecate 0
and NULL
as null pointer constants. But you should code as if it did.
上一篇: 在C ++ 11中安全吗?