C++ const modifier with primitive types

后端 未结 5 2068
死守一世寂寞
死守一世寂寞 2020-12-31 12:16

Should I pay attention on const modifier working with primitive types? Which one is more syntactically correct and why?

First version:

f         


        
5条回答
  •  我在风中等你
    2020-12-31 12:58

    Short answer: It does not matter.

    Long answer: Since you are passing the two arguments by value and returning the argument by value. Either one of those are fine, but you will more commonly see the first version.

    If you pass by reference (as others have suggested), then it does matter, and you should use a const-reference. However, passing primitive types by reference doesn't really give you any advantages or make sense (if it's a const reference). The reason for this is because passing by primitive types by value will not produce any overhead compared to passing primitive by reference.

提交回复
热议问题