This program:
yields this:
I'm using typedef as a poor-man's Enum. Or class. Or something.
I did make function lookup favor a function with a typedef argument over one with the base type, which is why my to_string was called in preference to to_string( int) - which is not actually what it does for coercion; it is done by Operation coercion, not function call.
Obviously, however, value coercion to a string does not use to_string(). Perhaps it should, if given a typedef.
Code:
typedef int organ;
organ LIVER = 1;
organ STOMACH = 2;
organ SPLEEN = 3;
string to_string( organ o )
{
switch ( o ) {
case LIVER:
return "liver";
case STOMACH:
return "stomach";
case SPLEEN:
return "spleen";
}
return "none";
}
print( "organ 'LIVER' with implicit string conversion = " + LIVER );
print( "organ 'LIVER' with explicit string conversion = " + LIVER.to_string() );
string str = SPLEEN;
print( "organ 'SPLEEN' coerced via assignment to a string = " + str );
Code:
[color=green]> concat.ash[/color]
organ 'LIVER' with implicit string conversion = 1
organ 'LIVER' with explicit string conversion = liver
organ 'SPLEEN' coerced via assignment to a string = 3
I did make function lookup favor a function with a typedef argument over one with the base type, which is why my to_string was called in preference to to_string( int) - which is not actually what it does for coercion; it is done by Operation coercion, not function call.
Obviously, however, value coercion to a string does not use to_string(). Perhaps it should, if given a typedef.