user23163
user23163

Reputation: 525

Getting the name of the current method in c++

Is there a (standardized) way to get the name of the current method using c++?

Using GNU GCC you can do this by using the macro __FUNCTION__ and __PRETTY_FUNCTION__ (surrounded by 2 underscores), however, this is of course non portable. Is there a way to do this in standard c++ or a way to make it portable?

Upvotes: 14

Views: 7720

Answers (4)

Michael Burr
Michael Burr

Reputation: 340208

The __func__ identifier is part of the C99 standard and is in the C++0x draft.

The identifier __func__ is implicitly declared by the compiler as if the following were at the very start of each function body:

static const char __func__[] = "function-name";

where function-name is an implementation-defined string automatically provided by the compiler.

Until support for those standards is available (or more widely available) you're stuck with compiler-specific gunk.

Upvotes: 26

Keith Nicholas
Keith Nicholas

Reputation: 44288

No...... but some things end up being non standard standards!

see http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx which outlines the ANSI standard stuff, and then microsofs which includes FUNCTION

Upvotes: 1

andy.gurin
andy.gurin

Reputation: 3914

It's called "introspection" and there is no such a thing in c++ standards. You should use an external library (boost I think supports it somehow). But try figure out if the reason for using it is really a good one.

Upvotes: 0

Marko
Marko

Reputation: 31385

No, it is not supported by c++ standard.

Upvotes: 0

Related Questions