I have:
class C
{
C(long){...};
C(double){...};
:
}
Unfortunately,
C c{5}; // error! ambiguous overload
(This is rather horrible, is it not? An integral type should surely favour a constructor taking an integral argument of higher precision.)
How to correctly have integral arguments and floatingpoint arguments correctly forward to their respective constructors?
EDIT: Maybe I have oversimplified the question. It comes originally from this enquiry. I am wrapping Python primitives such as Float Long String, and it is important that initialisations get forwarded to the correct primitive. At the same time, as this is intended to be a general use wrapper, I don't want the consumer to have to worry about typecasting in order to avoid internal pitfalls.
As Mike Seymour points out, SFINAE gives a technique for handling this.
Thanks massively to the doug64k on the FreeNode C++ channel for the following solutions:
http://ideone.com/QLUpu2 http://ideone.com/TCigR3 http://ideone.com/oDOSLH
I will attempt to work these into an answer when I pick up the trail tomorrow.
You can do a template constructor to redirect an undeclared types to a constructor of your choosing.
Lets say you want
long
to be your default. Using SFINAE you can check if a typeT
is able to be casted as along
and then pass it to the long constructor!This outputs: