What are the use cases for having a function return by const value for non-builtin type?

前端 未结 4 1858
清酒与你
清酒与你 2020-11-28 06:22

Recently I have read that it makes sense when returning by value from a function to qualify the return type const for non-builtin types, e.g.:

const Result o         


        
4条回答
  •  温柔的废话
    2020-11-28 06:54

    There is no benefit when returning by value. It doesn't make sense.

    The only difference is that it prevents people from using it as an lvalue:

    class Foo
    {
        void bar();
    };
    
    const Foo foo();
    
    int main()
    {
        foo().bar(); // Invalid
    }
    

提交回复
热议问题