Re: macro to print filename

"Michal Nazarewicz" <[email protected]> Wed, 01 Jun 2011 21:08:51 +0200
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <op.vwevg1xb3l0zgt@mnazarewicz-glaptop>
On Wed, 01 Jun 2011 20:45:06 +0200, Zhongye Jia <[email protected]>  
wrote:
> I do not think this can be done in *compiling* time, neither macro in
> c nor template in c++, so would you be so kind to offer the solution
> by using template or just give  some tips? Since I'm not that good at
> c++ :)

I was thinking along the lines of:

template<unsigned offset>
struct _filename {
	static const char *const val = __FILE__[offset] == '/'
		? __FILE__ + offset + 1
		: _filename<offset-1>::val;
};

template<>
struct _filename<0> {
	static const char *const val = __FILE__;
};

static const char *const filename = _filename<sizeof(__FILE__) - 1>;

I'm a bit rusty so I dunno if that'll work in this format.

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--