How can I select a specific function overload?

前端 未结 2 1375
刺人心
刺人心 2021-01-27 19:03

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(ch         


        
2条回答
  •  暖寄归人
    2021-01-27 19:44

    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(0, x) where otherwise 0 is considered of integer type).

提交回复
热议问题