Function that accepts both lvalue and rvalue arguments

后端 未结 6 1090
长发绾君心
长发绾君心 2020-12-08 13:13

Is there a way to write a function in C++ that accepts both lvalue and rvalue arguments, without making it a template?

For example, suppose I write a function

6条回答
  •  醉话见心
    2020-12-08 13:48

    Another rather ugly alternative is to make the function a template and explicitly instantiate both versions:

    template
    void print(T&&) { /* ... */ }
    
    template void print(istream&);
    template void print(istream&&);
    

    This can be compiled separately. The client code only needs the declaration of the template.

    I'd personaly just stick with what Andy Prowl suggests, though.

提交回复
热议问题