Function Overloading Based on Value vs. Const Reference

前端 未结 6 876
借酒劲吻你
借酒劲吻你 2020-11-27 06:21

Does declaring something like the following

void foo(int x)        { std::cout << \"foo(int)\"         << std::endl; }
void foo(const int &x)         


        
6条回答
  •  广开言路
    2020-11-27 07:14

    You could do this with a template:

    template void foo(T x) { ... }

    Then you can call this template by value or by reference:

    int x = 123;
    foo(x);  // by value
    foo(x);  // by refernce
    

提交回复
热议问题