Get variable symbol name that was passed to a paramater?
Rob T
alanb at ucora.com
Sat Nov 9 01:12:19 PST 2013
I have a template function called "inspect" that takes two
variables as arguments,
void inspect(T)( string symbol, T value )
{
writeln(symbol, " = ", value);
}
int y = 100;
inspect( y.stringof, y );
writes to console
y = 100
I am wondering if there's a way to pass only the variable and
have the inspect function determine the variable's symbol name as
passed rather than have to pass both the name and value
separately?
void inspect(T)( T value )
{
writeln( ? , " = ", value);
}
I've tried a template with alias parameter
void inspect(alias value)()
{
writeln( value.stringof , " = ", value);
}
It works except when passing a variable contained inside a struct
or class due to a missing "this" during evaluation, I'm also
worried about template bloat.
I figure mixins may help, but not if it's same or less convenient
to use as the double entry method.
Any suggestions, or is it just impossible or not worth trying?
--rt
More information about the Digitalmars-d-learn
mailing list