C++ pass by value / by reference function overload

Zelid

There are 2 function overloads:

MyClass do_something(MyClass param);
const MyClass& do_something(const MyClass& param);

Then I do:

MyClass c1 {"c1"};
do_something(c1);  // I want this to be used by value overload
do_something(c1);  // this to be used by reference overload

Is there any special way to explicitly specify that argument is passed by value or by reference?

For move semantic there is std::move() I wonder if there is anything like std::copy() std::ref for my case?

P.S. It's not to be used in real program, just checking out by myself the difference of passing arguments, returning values and their behaviour in different ways and have all functions with the same name:

// pass by value (copy)
MyClass do_something(MyClass param) {
    cout << "do_something(MyClass param)" << endl;
    param.i = 100;
    return param;
}

// !!! Your normal habit when passing an argument to a function should be to pass by const reference. (thinking in c++)
// pass by reference (reference)
const MyClass& do_something(const MyClass& param) { // doesn't allow to modify the object
    cout << "do_something(MyClass& param)" << endl;
    return param;
}

// pass by move semantic (move)
MyClass&& do_something(MyClass&& param) {
    cout << "do_something(MyClass&& param)" << endl;
    param.name += "__after_do_something(MyClass&& param)";
    param.i = 100;
    return move(param);
}

// pass by pointer (reference)
MyClass* do_something(MyClass* const param) { // allows to modify object, but not pointer (address)
    cout << "do_something(MyClass* const param)" << endl;
    param->i = 100;
    // (*param).i = 100;  // the same as above
    return param;
}
Cheers and hth. - Alf

You can resolve an overload ambiguity by casting to the relevant function pointer type (it's one of the rare cases where the type of an expression is determined by outer context, instead of being built up from inside):

struct MyClass { char const* s; };

MyClass do_something(MyClass) { return MyClass(); }
const MyClass& do_something(const MyClass& param) { return param; }

auto main() -> int
{
    MyClass c1 {"c1"};
    static_cast<MyClass(*)(MyClass)>( do_something )( c1 );         // Value overload
    static_cast<MyClass const&(*)(MyClass const&)>( do_something )( c1 );  // Ref overload
}

But in practice you should just name the functions differently, or use tie-breaker arguments or argument types, i.e., designing the functions for explicit choice of function.

I would name them differently because they do different things, so it indicates the Wrong Thing™ to have the same name for them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass by reference/value overload

From Dev

C string function calls, pass by value or reference?

From Dev

Practial solution to overload ambiguity between pass by reference and pass by value in C++

From Dev

Why is pass by value and pass by rvalue overload c++ function call ambiguous?

From Dev

luabind: How to pass value from C++ to lua function by reference?

From Dev

luabind: How to pass value from C++ to lua function by reference?

From Dev

Pass function as a parameter and overload it

From Dev

Pass function as a parameter and overload it

From Dev

c# object pass by reference or pass by value

From Dev

c# object pass by reference or pass by value

From Java

c++ overload generic method, by-reference and by-value

From Dev

Javascript: pass function by reference, rather than by value

From Dev

Javascript: pass function by reference, rather than by value

From Dev

Pass By Value Instead Of Pass By Reference For Copy Paste Function

From Dev

Why is swap function failing? (Pass by value vs Pass vs reference)

From Dev

C++ How to pass array by reference to function

From Dev

C++ Pass by Reference and Value and Const Issues

From Dev

C: When to return by value or pass reference

From Dev

Does C++ pass objects by value or reference?

From Dev

C# & Unity : Pass reference by value?

From Dev

C++ Pass by Reference and Value and Const Issues

From Dev

How to overload function for different iterator value_types in C++

From Dev

Is Objective-C pass-by-value or pass-by-reference?

From Dev

C# pass by value vs. pass by reference

From Dev

Python, Java, C: Pass by Reference or Pass by Value Differences

From Dev

Which is faster? Pass by reference vs pass by value C++

From Java

Function overload for string literals lvalue and rvalue reference

From Dev

Passing a reference to template function call operator overload

From Dev

Passing a reference to template function call operator overload

Related Related

  1. 1

    Pass by reference/value overload

  2. 2

    C string function calls, pass by value or reference?

  3. 3

    Practial solution to overload ambiguity between pass by reference and pass by value in C++

  4. 4

    Why is pass by value and pass by rvalue overload c++ function call ambiguous?

  5. 5

    luabind: How to pass value from C++ to lua function by reference?

  6. 6

    luabind: How to pass value from C++ to lua function by reference?

  7. 7

    Pass function as a parameter and overload it

  8. 8

    Pass function as a parameter and overload it

  9. 9

    c# object pass by reference or pass by value

  10. 10

    c# object pass by reference or pass by value

  11. 11

    c++ overload generic method, by-reference and by-value

  12. 12

    Javascript: pass function by reference, rather than by value

  13. 13

    Javascript: pass function by reference, rather than by value

  14. 14

    Pass By Value Instead Of Pass By Reference For Copy Paste Function

  15. 15

    Why is swap function failing? (Pass by value vs Pass vs reference)

  16. 16

    C++ How to pass array by reference to function

  17. 17

    C++ Pass by Reference and Value and Const Issues

  18. 18

    C: When to return by value or pass reference

  19. 19

    Does C++ pass objects by value or reference?

  20. 20

    C# & Unity : Pass reference by value?

  21. 21

    C++ Pass by Reference and Value and Const Issues

  22. 22

    How to overload function for different iterator value_types in C++

  23. 23

    Is Objective-C pass-by-value or pass-by-reference?

  24. 24

    C# pass by value vs. pass by reference

  25. 25

    Python, Java, C: Pass by Reference or Pass by Value Differences

  26. 26

    Which is faster? Pass by reference vs pass by value C++

  27. 27

    Function overload for string literals lvalue and rvalue reference

  28. 28

    Passing a reference to template function call operator overload

  29. 29

    Passing a reference to template function call operator overload

HotTag

Archive