In C++, when we use typeid
to get type name of an object or class, it will show a decorated(mangled) string. I use cxxabi
to demangle it:
#include <cxxabi.h> #include <typeinfo> namespace MyNamespace { class MyBaseClass { public: const std::string name() { int status; char *realname = abi::__cxa_demangle(typeid (*this).name(),0,0, &status); std::string n = realname; free(realname); return n; } }; } int main() { MyNamespace::MyBaseClass h; std::cout << h.name() << std::endl; }
The output in gcc
is:
MyNamespace::MyBaseClass
I need to remove MyNamespace::
from above string. i can remove them by string manipulating .
But is there a standard way with cxxabi
or other libraries to do this or a clear solution?(At least portable between gcc and Visual C++)
Thus, removing using namespace std; changes the meaning of those unqualified names, rather than just making the code fail to compile. These might be names from your code; or perhaps they are C library functions.
No you can't unuse a namespace. The only thing you can do is putting the using namespace -statement a block to limit it's scope.
Namespace is a feature added in C++ and is not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it. Multiple namespace blocks with the same name are allowed.
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
There is no standard way to do this, period, because there is no standard way to do name mangling. How to represent names was intentionally unspecified. There is no ABI in the C++ standard. The function you are using, abi::__cxa_demangle
, is a part of the Itanium C++ ABI. That function may or may not exist elsewhere.
As far as a way to do what you want using the Itanium C++ ABI, they intentionally do not provide such a capability.
I dont know if this would corresponds to your needs but i liked to mention about it.
There is something you can do to get the name of a class that you had write. And it may be considered as portable between gcc and Visual C++.
In GCC there is a magic variable named __FUNCTION__ as part of the gnu c language extensions, which treated as a variable, in C++. ( Treating it differs in C according to the GCC version. )
In Visual Studio there is a predefined macro which is in the same name and does the same job. Description is here.
You use __FUNCTION__ to get the name of the current function. So, to get the name of the class, may be you can use it in class constructor like this:
namespace MyNamespace { class MyBaseClass { public: MyBaseClass(): myName(__FUNCTION__){} string name() { return myName; } private: string myName; }; }
So, you get "MyBaseClass"
response if you call name()
function of an instance of this class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With