简体   繁体   中英

Register specifier appearing in the function argument

I'm trying to understand, what is the point of applying register specifier to a function argument. The following code is valid:

#include <iostream>

using std::cout;

struct A
{
    A(){ cout << "A\n"; } 
    ~A(){ cout << "~A\n"; }
};

void foo(register A a)
{
}

int main()
{
    cout << "first line\n";
    foo(A());
    cout << "last line\n";
}

Why does register specifier apply to function parameter?

register is an old keyword that has its origins way back in K&R C. It's purpose was to hint the compiler to keep the variable as a processor register, instead of loading and unloading the data from memory, which had overhead. When you access this variable very often the one or two instructions you saved when you declare it as register was very valuable back then.

Since then compilers have became pretty good at optimizing your code, and this keyword lost its purpose. It's now ignored by most implementations unless you tell the compiler to use it anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM