Return type in function name name demangled

What is the reason that g ++ function abi::__cxa_demangle

does not return return value for member functions?

Here is a working example of this behavior

#include <execinfo.h>
#include <cxxabi.h>
#include <iostream>

struct Foo {
    void operator()() const
    {
        constexpr int buf_size = 100;
        static void *buffer[buf_size];
        int nptrs = backtrace(buffer, buf_size);
        char **strings = backtrace_symbols(buffer, nptrs);
        for(int i = 0; i < nptrs; ++i) {

            auto str = std::string(strings[i]);
            auto first = str.find_last_of('(') + 1;
            auto last = str.find_last_of(')');
            auto mas = str.find_last_of('+');

            int status;
            char* result = abi::__cxa_demangle(str.substr(first, mas-first).c_str(), nullptr, nullptr, &status);
            if (status == 0) std::cout << result << std::endl;
        }
    }
};

int main () {
    Foo f;
    f();
}

      

and after compilation with g++ foo.cpp -std=c++11 -rdynamic

exit Foo::operator()() const

Is there a way to get consistent behavior, i.e. get also the return value for member functions?

+3


source to share


1 answer


Mangling / demangling function names is outside the scope of the C ++ standard. It is purely compiler's responsibility and the need to manipulate functions in such a way that two different functions will have different malformed representations (symbols).

At the same time, since the declaration of two functions that differ only in their return type is prohibited by the standard, there is no need for the compiler to distort the return type, as there will be no benefit.



What you see is a consequence of the compiler being lazy enough.

Also, here's a rextester with a more complete example (including a global function in the backtrace).

+3


source







All Articles