0

I want to pass an overloaded operator to a function, which can't figure out which of the two overloads it should use.

// This works, not overloaded:
chai.add(chaiscript::fun(&HttpRequest::operator+), "+");
// This does not, overloaded:
chai.add(chaiscript::fun(&(
    std::map<std::string,std::string>::operator[]), "[]");

The chaiscript::fun expects a generic parameter, but it can't figure out which overload to use.
I need to specify the overload, but I don't know the syntax. I tried things like:

chai.add(chaiscript::fun(&(
    std::map<std::string,std::string>::operator[]<foo>), "[]");

but this doesn't work.

How is the syntax to specify the overload?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • possible duplicate of [Function overloading and function pointers](http://stackoverflow.com/questions/6182885/function-overloading-and-function-pointers) – Deduplicator Aug 03 '14 at 16:45
  • operator overloading is a red hering here, you just want to select a specific function from an overload set... – Deduplicator Aug 03 '14 at 16:46
  • Note, that the parentheses are not balanced. Interesting that fun eats binary operators as well as unary. – Tobias Aug 03 '14 at 16:51

2 Answers2

1

The following may help you to choose one overload of map::operator[]

static_cast<
    std::string& (std::map<std::string, std::string>::*)(const std::string&)>(
        &std::map<std::string, std::string>::operator []);

Or with a typedef:

using MyMap = std::map<std::string, std::string>;

static_cast<std::string& (MyMap::*)(const std::string&)>(&MyMap::operator []);
//          ReturnType    Class     params
Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

Firstly, operators are just functions with a special syntax. So, overloaded operators are just overloaded functions. With that in mind, you question boils down to "How do I select one of an overloaded set of functions?". The simple answer to that is the use of a static_cast.

Alternatively, I think you can also use it in a context where the context already dictates which of the overloads to select. In your case, you are using a template function (I think) and it doesn't work there because it needs the type to instantiate the template and it needs the template instatiation to select the type and thus the overload. Using a temporary variable is one way to resolve this, another is to explicitly specify the template function (like e.g. max<float>(0, x) where otherwise 0 is considered of integer type).

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • The second option is want I want to do (explicitly specifiy the template function). The question is how the syntax is in my specific case because I don't know where to put the template datatype. –  Aug 03 '14 at 17:05
  • You'd have to provide a bit more code to help you in that. Anyhow, it's like with the std::max function I mentioned. This has the form `template T max(T const& t1, T const& t2)`. Calling it with two different types doesn't allow the compiler to deduce the type `T`, which is why I explicitly put the type in angled brackets like `max`. – Ulrich Eckhardt Aug 03 '14 at 17:13