0

for Debugging purposes I'd like to extract the name of the function from a template argument. However I'm only getting the functions signature not an actual name.

namespace internal
{
static const unsigned int FRONT_SIZE = sizeof("internal::GetTypeNameHelper<") - 1u;
static const unsigned int BACK_SIZE = sizeof(">::GetTypeName") - 1u;

template<typename T>
struct GetTypeNameHelper
{
    static const char* GetTypeName(void)
    {
#ifdef __GNUC__
        static const size_t size = sizeof(__PRETTY_FUNCTION__);
        static char typeName[size] = { };
        memcpy(typeName, __PRETTY_FUNCTION__, size - 1u);
#else
        static const size_t size = sizeof(__FUNCTION__) - FRONT_SIZE - BACK_SIZE;
        static char typeName[size] =
        {};
        memcpy(typeName, __FUNCTION__ + FRONT_SIZE, size - 1u);
#endif //__GNUC__

        return typeName;
    }
};
} //namespace internal

template<typename T>
const char* GetTypeName(void)
{
    return internal::GetTypeNameHelper<T>::GetTypeName();
}


Calling this from an own make function

template<typename Func_T, typename ... Args>
CExtended_Function<Args...> Make_Extended_Function(Func_T f)
{
    std::function<void(Args...)> func(f);
    const char* pFunc_Name = NCommonFunctions::GetTypeName<Func_T>();

    CExtended_Function<Args...> res(f, func_name);

    return res;
}

with

void Test_Func();

void foo()
{
   Make_Extended_Function(Test_Func);
}

Gives me only the function signature.

... [with T = void (*)()]...

However I'd like to get the function name (in this case "Test_Func")

I thought about using makros but I'm not sure how to implement the Args... Part in Makros. Do you have an idea on how to solve this? I'd like to avoid using RTTI.

Dr.Death
  • 93
  • 9
  • debuggers are typically able to show name of the function from pointer without any additional moves – user7860670 Jul 26 '19 at 20:22
  • Possible duplicate of [Is there a way to get function name inside a C++ function?](https://stackoverflow.com/questions/733056/is-there-a-way-to-get-function-name-inside-a-c-function) – deon cagadoes Jul 26 '19 at 20:23

2 Answers2

1

Functions aren't valid template arguments - your template argument here is the type of a pointer to the function, not the function itself - so this is completely impossible. There is also no portable way to get the name of a particular function at compile time either, at least at the moment (it's possible that this will be possible in the future through compile time reflection, but that's going to be C++2y (23?) at the earliest).

Cubic
  • 14,902
  • 5
  • 47
  • 92
0

With Macro, you can do (I also use CTAD from C++17)

template<typename F>
auto Make_Extended_Function_Impl(F f, const std::string& name)
{
    std::function func(f);
    CExtended_Function res(f, name);

    return res;
}

#define Make_Extended_Function(f) Make_Extended_Function(f, #f)
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Does the std::function object force it to deduce that F is a function object? I don't understand the purpose of std::function in the OP or your answer. – Demolishun Jul 26 '19 at 22:21
  • 1
    @Demolishun: OP already asked question about `CExtended_Function` which takes `std::function` (I think it should just take functor BTW). `std::function func(f)`, thanks to CTAD, allows to deduce type of `std::function` (when possible). Else we would need some function traits to know parameter types and return type. – Jarod42 Jul 26 '19 at 22:29
  • @cubic: i'm using c++17 as well. Do i have to enable CTAD manually (GCC 8) ```auto res = MAKE_EXTENDED_FUNCTION(Callback);``` gives me ```class template argument deduction failed:``` in line ```CExtended_Function res(f, name);``` – Dr.Death Jul 27 '19 at 09:46
  • You probably need User-defined deduction guides for `CExtended_Function` – Jarod42 Jul 27 '19 at 10:08
  • how would I do this? I read something about modifying the constructor to help the compiler?! – Dr.Death Jul 27 '19 at 14:58
  • You might ask that in another question. – Jarod42 Jul 27 '19 at 15:22