Graznarak
Graznarak

Reputation: 3740

How to resolve function overload ambiguity

I have an overloaded pair of functions that take a single argument. One takes a uint64_t. The other takes an int64_t. When I pass a uint32_t to the function it is correctly flagged as ambiguous.

How do I resolve this ambiguity without resorting to templates? I can write a shallow wrapper that uses type traits to resolve it, but I would rather not. I would prefer for signed types to resolve to the signed overload, and unsigned types to resolve to the unsigned overload.

Also, I would rather not have an overload for each integer type.

Upvotes: 0

Views: 245

Answers (1)

Barry
Barry

Reputation: 303890

You have several options at your disposal:

  1. Require the caller to explicitly cast to either int64_t or uint64_t.
  2. Provide an overload for every integer type
  3. Use templates, and either SFINAE or tag-dispatch on signed-ness.

(3) would be (in my opinion) the clearest, (2) would be the most verbose, and (1) would require the most work on your users.

Upvotes: 2

Related Questions