__func__

Jarrett Billingsley jarrett.billingsley at gmail.com
Sun Oct 4 14:37:05 PDT 2009


On Sun, Oct 4, 2009 at 5:12 PM, Zarathustra <adam.chrapkowski at gmail.com> wrote:
> Does in D exist something like __func__?
> For example in gcc:
> int main(){ printf("%s", __func__); ...}
> returns "main".
>

No.

There is a ridiculous workaround though.

// Parsing mangles for fun and profit.
char[] _getJustName(char[] mangle)
{
	size_t idx = 1;
	size_t start = idx;
	size_t len = 0;

	while(idx < mangle.length && mangle[idx] >= '0' &&
		mangle[idx] <= '9')
	{
		int size = mangle[idx++] - '0';

		while(mangle[idx] >= '0' && mangle[idx] <= '9')
			size = (size * 10) + (mangle[idx++] - '0');

		start = idx;
		len = size;
		idx += size;
	}

	if(start < mangle.length)
		return mangle[start .. start + len];
	else
		return "";
}

const char[] FuncNameMix = "static if(!is(typeof(__FUNCTION__))) {"
"struct __FUNCTION {} const char[] __FUNCTION__ = _getJustName("
"__FUNCTION.mangleof); }";

Now you can use:

import std.stdio;
void main()
{
    mixin(FuncNameMix);
    writefln("%s", __FUNCTION__);
}

You just mix FuncNameMix into any function where you want to use __FUNCTION__.


More information about the Digitalmars-d-learn mailing list