ITNOA
I want to leaern variable aliasing in C++ for make much code lesser than before without pay any cost (Zero Cost), for I can better discribe my purpose, please see below details
I have a structure like below
struct info
{
size_t d1;
size_t d2;
size_t d3;
size_t d4;
};
I use this struct in another class. (that call LargeName
class) like below
class LargeName
{
public:
// … many fields and methods
Info get_large_name_info() const
{
return info;
}
private:
Info info;
}
In this scenario if I want to use info fields from LargeName
instant in external function, I have to something like below
void foo(LargeName large_name)
{
large_name.get_large_name_info().d1;
large_name.get_large_name_info().d2;
large_name.get_large_name_info().d3;
large_name.get_large_name_info().d4;
}
As you can see in above example if my class name and getter access name of info is too long, I have to write very character to use d1 to d4. But I want to avoid them. so I write below code
void foo(LargeName large_name)
{
const Info& info = large_name.get_large_name_info();
info.d1;
info.d2;
info.d3;
info.d4;
}
As you can see my code is little cleaner and very shorter than before. (I called info
is alias variable for large_name.get_large_name_info()
) But the problem is I have not sure to compiler generate equal code for new version of my code that same as before.
I think second foo implementation my pay reference and dereferencing cost for using info variable.
My question is how to variable aliasing in C++ without pay any cost?
Aucun commentaire:
Enregistrer un commentaire