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

前端 未结 6 1668
渐次进展
渐次进展 2021-02-12 23:08

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 ?

i.e. is it

相关标签:
6条回答
  • 2021-02-12 23:19

    It really doesn't matter, passing by value makes cleaner code thou and is therefore considered good practice.

    0 讨论(0)
  • 2021-02-12 23:22

    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.

    0 讨论(0)
  • 2021-02-12 23:28

    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 (i.e. take an address) and then dereference when using the variable. In most cases it will be optimized by compiler in any case though

    0 讨论(0)
  • 2021-02-12 23:33

    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.

    0 讨论(0)
  • 2021-02-12 23:34

    One reason would be that you would like to convey to other programmers that the value is constant, it may in some cases be clearer although const bool would suffice.

    0 讨论(0)
  • 2021-02-12 23:38

    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.

    0 讨论(0)
提交回复
热议问题