• Create Account

We're offering banner ads on our site from just \$5!

### #ActualCornstalks

Posted 28 December 2012 - 03:15 PM

It would be cool if you could define casting operators for opaque typedefs too*:

// Obviously this would require a few changes to the Standard
Inches operator Centimeters() (Centimeters cm)
{
// Estimate
return (cm * 5) / 2;
// Note that the above is merely example code; I realize that a) the above isn't necessarily the
// the right data type, and b) casting it to the right data type could cause recursion without some
// changes or facilities added to the Standard.
}

inline constexpr Centimeters operator"" _cm (int cm)
{
return (Centimeters)cm;
}

Inches i = (Inches)(12_cm); // The point of this code is to show this line

Also, what would be the implications with promotions? What if you multiplied Inches (which is typedefd as an int) by a float? Or multiplied it by an int?

*This is, I would say, related to opaque typedefs, but is also a bit of a separate issue (because declaring a casting operator makes implicit conversions possible, and AFAIK you can't make a casting operator that requires explicit casting). I suppose you could also say it could work for non-opaque typedefs/implicit conversions, but I don't like the ambiguity of Inches i = 12_cm;

### #3Cornstalks

Posted 28 December 2012 - 03:15 PM

It would be cool if you could define casting operators for opaque typedefs too*:

// Obviously this would require a few changes to the Standard
Inches operator Centimeters() (Centimeters cm)
{
// Estimate
return (cm * 5) / 2;
// Note that the above is merely example code; I realize that a) the above isn't necessarily the
// the right data type, and b) casting it to the right data type could cause recursion without some
// changes or facilities added to the Standard.
}

inline constexpr Centimeters operator"" _cm (int cm)
{
return (Centimeters)cm;
}

Inches i = (Inches)(12_cm); // The point of this code is to show this line

Also, what would be the implications with promotions? What if you multiplied Inches (which is typedefd as an int) by a float? Or multiplied it by an int?

*This is, I would say, related to opaque typedefs, but is also a bit of a separate issue (because declaring a casting operator makes implicit conversions possible, and AFAIK you can't make a casting operator that requires explicit casting). I suppose you could also say it could work for non-opaque typedefs, but I don't like the ambiguity of Inches i = 12_cm;

### #2Cornstalks

Posted 28 December 2012 - 03:07 PM

It would be cool if you could define casting operators for opaque types too:

// Obviously this would require a few changes to the Standard
Inches operator Centimeters() (Centimeters cm)
{
// Estimate
return (cm * 5) / 2;
// Note that the above is merely example code; I realize that a) the above isn't necessarily the
// the right data type, and b) casting it to the right data type could cause recursion without some
// changes or facilities added to the Standard.
}

inline constexpr Centimeters operator"" _cm (int cm)
{
return (Centimeters)cm;
}

Inches i = (Inches)(12_cm); // The point of this code is to show this line

Also, what would be the implications with promotions? What if you multiplied Inches (which is typedefd as an int) by a float? Or multiplied it by an int?

### #1Cornstalks

Posted 28 December 2012 - 03:06 PM

It would be cool if you could define casting operators for opaque types too:

// Obviously this would require a few changes to the Standard
Inches operator Centimeters() (Centimeters cm)
{
// Estimate
return (cm * 5) / 2;
// Note that the above is merely example code; I realize that a) the above isn't necessarily the
// the right data type, and b) casting it to the right data type could cause recursion without some
// changes or facilities added to the Standard.
}

inline constexpr Centimeters operator"" _cm (int cm)
{
return (Centimeters)cm;
}

Inches i = (Inches)(12_cm); // The point of this code is to show this line

Also, what would be the implications with promotions? What if you multiplied Inches (which is typedefd as an int) by a float? Or multiplied it by an int?

PARTNERS