Is there a __CLASS__
macro in C++ which gives the class name similar to __FUNCTION__
macro which gives the function name
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Macro expansion in elixir: how to define 2 macros
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
If you need something that will actually produce the class name at compile time, you can use C++11 to do this:
I recognize that this is not the same thing as
__FUNCTION__
but I found this post while looking for an answer like this. :DThis works quite nicely if you are willing to pay the cost of a pointer.
The problem with using
typeid(*this).name()
is that there is nothis
pointer in a static method call. The macro__PRETTY_FUNCTION__
reports a class name in static functions as well as method calls. However, this will only work with gcc.Here's an example of extracting the information through a macro style interface.
The macro
__METHOD_NAME__
will return a string of the form<class>::<method>()
, trimming the return type, modifiers and arguments from what__PRETTY_FUNCTION__
gives you.For something which extracts just the class name, some care must be taken to trap situations where there is no class:
Not yet. (I think
__class__
is proposed somewhere). You can also try to extract class part from__PRETTY_FUNCTION__
.You can get the function name including class name. This can process C-type funcitons.
Works with msvc and gcc too