Auto - the pros and cons of
Introduction
Kind time of the day, friends! Today we will talk about the operator auto
in C ++.
A little background: this operator came to us in C ++ 11 and since then has deeply entered into the life of many programmers and various projects. Today I would like to talk about some of the pros and cons from my point of view. lambda functions
. Consider the example:
//C ++ 98
struct
{
bool operator () (int a, int b) const
{
return a < b;
}
} customLess;
std :: sort (s.begin (), s.end (), customLess);
Old kind std :: sort with an overload on its sorting algorithm. In C ++ 9? for this code, you needed to create a wrapper through the structure. With the advent of lambd, the situation has changed dramatically:
//C ++
std :: sort (s.begin (), s.end (),
//Lambda begin
.[](int a, int b)
{
return a> b;
}
//End
);
2. STD Types
We all used std sometime and use it now. But std types have one big problem They are too long.
int n1 = 3;
std :: vector v {? ? ? ? 4};
//Case 1
std :: vector :: iterator OldStyle = std :: find (v.begin (), v.end (), n1);
//Case 2
auto NewStyle = std :: find (v.begin (), v.end (), n1);
The first case: std :: find returns a type equal to std :: vector :: iterator
, But we know this very well. In many projects, for example, in the same game engine X-Ray, for such iterations tayddefs were made, ala:
typedef std :: vector vec_int;
typedef vec_int :: iterator vec_int_it;
But I'm not a fan of this code, so it's easier to use auto
, as shown in the second case.
3. Range-base-for loops
The long-awaited innovation in C ++ was the R-B-For cycles. Well, as an innovation, in C # and C ++ /CLI they already were, and the native still lagged behind
When we need to sort through, for example, the std :: map elements, we have to access std :: pait. But, do not forget about auto! Consider the situation by referring to the source code xray-oxygen :
for (auto & entities_it: entities)
{
if (entities_it.second-> ID_Parent! = 0xffff) continue;
found = true;
Perform_destroy (entities_it.second);
break;
}
A good kind of cycle. Without auto it would be like this:
for (std :: pair & entities_it: entities)
{
if (entities_it.second-> ID_Parent! = 0xffff) continue;
found = true;
Perform_destroy (entities_it.second);
break;
}
As you understand, std :: pair
will be a reference to the second subtitle, but oh well, these types are not only in STD Namespace, but also in the X-Ray itself. However, there is one more problem in these iterators, but more on this later
And why is it bad?
1. The main problem is the ambiguity of types
Let's say the situation, we have in the code:
auto WhyIsWhy = TestPointer-> GetCrows ();
A person who does not write this code, you need to quickly look at the contents of the function, run the IDE for too long, use Notepad ++. He stumbles upon this line, and receives Logic Error The search for the GetCrows function begins.
2. Reference to the array object or let's talk more about Range-Base-For!
LimbUpdate (CIKLimb & refLimb);
{
//Code from XRay Oxygen, system of inverse kinematics, ignore,
//I take from what is at hand
for (auto it: _bone_chains)
LimbUpdate (it);
}
It seems that everything is fine, but! We are working not with an array element, but with a copy of the object of the element. Because of auto
we are invisible that it is not a pointer, but an object. Therefore, in places where we have a one-dimensional array, I advise you to write the types completely:
for (CIKLimb & it: _bone_chains)
LimbUpdate (it);
3. And you have x64!
Those who worked with writing code for AMD64 know that the compiler loves more double
, rather than float
. And our friend is auto
it is cast during compilation in the type preferred by the program. Consider the example.
auto isFloat = 1.0; //1.0f (x32) 1.0d (x64)
Of course, such things are not critical, but the compiler can start to pile a bunch of type truncation warnings, which is not very pleasant, and besides, a constant write to the log slows down the assembly. But, there are situations when such casting plays a role. In cases with the natural number 'e' you need to be careful. For example:
auto eps = 1e-8;
In x3? this value is : ???e-09
In x6? this value is : ???e-08
Conclusion
Of course, it's up to you to decide where and how to use auto
, but I like the full concept of static typing where it simplifies the understanding of the code.
It may be interesting
weber
Author22-09-2018, 02:24
Publication DateDevelopment / Programming
Category- Comments: 0
- Views: 235
Can I find someone to write my paper for me free? At our cheap for-pay academic help service with writers across all subjects. Discover more about us here. write a paper online free
vitamin tablets