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

zen

I have two generic methods (edit: actually operators but the issue is the same for methods) that are identical, except from that one uses its formal parameter by reference and the other method uses its formal parameter by value.

struct shout_t {
    template<typename T>
    shout_t& operator<<(T &x) { cout << x; return *this; } // by reference
    
    template<typename T>
    shout_t& operator<<(T x) { cout << x; return *this; } // by value
};

The intention of the "by-reference" method is to allow "large" objects to be used without copying. The "by-value" method is aimed at literals.

Since the "by-value" method can handle both cases (objects per se and literals), this gives an error:

int main() { // Here "large object" ~ string, "literal" ~ int
    shout_t shout;
    shout << 42; // OK
    shout << "FTL"; // ERROR: Overloaded operator '<<' is ambiguous
}

The behaviour I was looking for is try first if "by-reference" method applies, if not, apply "by-value" method.

How to resolve this? How to obtain the intended behaviour of two methods that are identical except from their "by-value" and "by-reference" signature?

lubgr

There are two scenarios here, either you might want to change the object passed as a parameter, or you don't. In the latter case, pass as const-qualified reference:

struct shout_t {
    template<typename T>
    shout_t& operator<<(const T &item) { cout << item; return *this; }
};

and otherwise, use a forwarding reference in conjuction with std::forward:

struct shout_t {
    template<typename T>
    shout_t& operator<<(T&& item) { cout << std::forward<T>(item); return *this; }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a generic method overload in C#?

From Dev

C++ pass by value / by reference function overload

From Dev

Method overload and generic parameter

From Dev

Overload method with generic method and parameter

From Dev

Pass by reference/value overload

From Dev

c# find method overload from generic type at runtime

From Dev

overload method with same generic parameter?

From Dev

Overload generic method from interface

From Dev

C# generic method value

From Dev

Is it possible to overload a generic method by input type?

From Dev

Scala method overload for a special case of a generic type

From Dev

Overload resolution issue for generic method with constraints

From Dev

Scala method overload for a special case of a generic type

From Dev

How Can I Force .NET (C#) to Use the non-generic overload of a method?

From Dev

C# how can I get generic class reference at method

From Dev

Passing object by reference vs by value in overload functions

From Dev

C++ Overload an overrided method

From Dev

C# method overload with inheritance

From Dev

Generic Type Method Without Generic Reference in Class

From Dev

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

From Dev

Reference a generic value from a record

From Dev

c# overload resolution between generic and non-generic methods

From Dev

Syntax for specifying a method reference to a generic method

From Dev

Java method reference to a method with generic parameter

From Dev

How to call a method overload based on closed generic type?

From Dev

Calling overloaded method with generic property calls wrong overload

From Dev

How to get a generic method's value in c#

From Dev

Can I create a generic method that takes a value type or a reference type but always returns a nullable type

From Dev

strange method overload resolution with constrained generic arguments: overload with base class arg always called

Related Related

  1. 1

    How to create a generic method overload in C#?

  2. 2

    C++ pass by value / by reference function overload

  3. 3

    Method overload and generic parameter

  4. 4

    Overload method with generic method and parameter

  5. 5

    Pass by reference/value overload

  6. 6

    c# find method overload from generic type at runtime

  7. 7

    overload method with same generic parameter?

  8. 8

    Overload generic method from interface

  9. 9

    C# generic method value

  10. 10

    Is it possible to overload a generic method by input type?

  11. 11

    Scala method overload for a special case of a generic type

  12. 12

    Overload resolution issue for generic method with constraints

  13. 13

    Scala method overload for a special case of a generic type

  14. 14

    How Can I Force .NET (C#) to Use the non-generic overload of a method?

  15. 15

    C# how can I get generic class reference at method

  16. 16

    Passing object by reference vs by value in overload functions

  17. 17

    C++ Overload an overrided method

  18. 18

    C# method overload with inheritance

  19. 19

    Generic Type Method Without Generic Reference in Class

  20. 20

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

  21. 21

    Reference a generic value from a record

  22. 22

    c# overload resolution between generic and non-generic methods

  23. 23

    Syntax for specifying a method reference to a generic method

  24. 24

    Java method reference to a method with generic parameter

  25. 25

    How to call a method overload based on closed generic type?

  26. 26

    Calling overloaded method with generic property calls wrong overload

  27. 27

    How to get a generic method's value in c#

  28. 28

    Can I create a generic method that takes a value type or a reference type but always returns a nullable type

  29. 29

    strange method overload resolution with constrained generic arguments: overload with base class arg always called

HotTag

Archive