简体   繁体   中英

In C++, is it bad to pass a const bool by reference?

In a practical environment, using gcc or MS Visual Studio, is it bad to pass the value types which are the same size or less than an int by const reference ?

ie is it bad to write such a function:

void f(const bool& b);

or

void f(const char& c);

rather than:

void f(bool b);

or

void f(char c);

The reason I am asking is that I do not see the benefit of passing a reference in these cases but maybe I am missing something.

It may be slightly bad, or it may not have an effect at all (depends on where the original value is stored, how good the optimizer is, and how it decides to treat your code).

The standard doesn't mandate how references are to be implemented, but in practice compilers implement references using pointers. Therefore in the general case a bool& would be implemented using a bool* , which means that to access the bool you need an extra pointer dereference each time. Since a bool is no bigger than a pointer, there's no reduced memory footprint or less byte copying to offset this drawback.

As a result the accepted practice is to pass primitives around as values since it's more efficient. Of course although passing such around as references won't really blow up anything, and unless you are accessing the value inside a loop will probably not even result in any measurable difference.

Performance aside, there are actually cases where you will get different behavior.

For instance, passing a const reference makes sure that the function cannot change the value of the referenced variable, but another thread might do so. If you pass by reference (even with const ), you will see these changes, if you pass by value, you will not.

Also, the definition of the interface limits what you can do with the variable inside the function. Consider this example:

int foo(int a) {
    a = 5; // valid
}

int bar(const int& a) {
    a = 5; // compiler-error
}

If you pass by reference, and you want to modify the value of the variable for local use, you need to make an extra copy. If you pass by value, you already have a copy.

一个原因是你想向其他程序员传达价值是恒定的,在某些情况下它可能更清晰,尽管const bool就足够了。

I think it's better to pass builtin types by value rather then const reference since it's virtually faster. In case of passing by reference you need to create a reference (ie take an address) and then dereference when using the variable. In most cases it will be optimized by compiler in any case though

Although in theory it won't be a good idea, as references are usually implemented using pointers. But nevertheless every reasonable compiler should be smart enough to recognize the indifference in semantics between by-const-reference and by-value for fundamental types.

Often you don't have a choice if you have some kind of templated interface that has to work for complex types and fundamental types and you don't want excessive specialization overhead (simplest example: std::vector ). But if you have a choice, then passing fundamental types by-value should be preferred.

这无关紧要,通过价值会使你的代码更清晰,因此被视为良好的做法。

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