1

Is there a way to have a function pointer to the function that's getting it. It should work like this:

void foo()
{
        assert(<self-pointer> == &foo);
}

(moved form comment):
I want a macro that can log something and add the place where the log came from to it. For this I want to use an id, which I thought I could make the function's pointer.

Marek R
  • 32,568
  • 6
  • 55
  • 140
Nikita Demodov
  • 553
  • 5
  • 17
  • You mean without naming it explicitly? That is, "" would be the same in foo() and bar()? (Then, I don't think there is a way.) – Peter - Reinstate Monica Jul 12 '21 at 09:51
  • 2
    This is [XY problem](http://xyproblem.info/). Please explain why do you need this! – Marek R Jul 12 '21 at 09:51
  • 2
    You just did no? `&foo` – Hatted Rooster Jul 12 '21 at 09:52
  • 1
    I guess he needs something like `this` for class, but for function. – Afshin Jul 12 '21 at 09:56
  • I suppose in C++ you could create a functor, i.e. a class that has an `operator()` which contains the actual functionality. Because you are in a class you can use `this`. – Peter - Reinstate Monica Jul 12 '21 at 09:56
  • @MarekR I want a macro that can log something and add the place where the log came from to it. For this I want to use an id, which I thought I could make the function's pointer. – Nikita Demodov Jul 12 '21 at 09:58
  • Just use `__LINE__` and the like in the macro. – Hatted Rooster Jul 12 '21 at 09:59
  • `__function__` is the name of the function, though thats a string. There was a similar question recently about calling the function by using `__function__`, which is also not possible. Anyhow, for logging you do not need the function pointer. Out of curiosity, what would you do with the pointer if you could get it? – 463035818_is_not_an_ai Jul 12 '21 at 10:00
  • 3
    C++20 has [std::source_location::current()](https://en.cppreference.com/w/cpp/utility/source_location/current). – Marek R Jul 12 '21 at 10:01
  • 1
    @463035818_is_not_a_number AFAIK `__function__` in an old extension and one should use `__func__` nowadays. – Nikita Demodov Jul 12 '21 at 10:05
  • Thank you for giving me some input. I've reconsidered and decided to use `__func__` instead. – Nikita Demodov Jul 12 '21 at 10:06
  • You'll need the caller to pass the caller's function address into the `foo` function, or some token like a `char const*` which may be more useful. Otherwise, you might considering resorting to platform specific solutions. Boost [Stacktrace](https://www.boost.org/doc/libs/1_76_0/doc/html/stacktrace.html) abstracts platform specific solutions for several platforms. – Eljay Jul 12 '21 at 12:23

2 Answers2

3

Note OP comment that he creates a logger.

In C++20 std::source_location::current() has been introduced so macros can be avoided. Linked documentation has nice example.

Before C++20 to create a logger you have to use macros:

  • __LINE__ - current line number
  • __func__ - current function name
  • __FILE__ - current file name

https://stackoverflow.com/a/597081/1387438

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thank you. I've accepted the other answer as yours doesn't answer the question in the title, but I've decided to use `__func__`(standard version) instead(as I don't have true access to c++ 20 yet). +1. – Nikita Demodov Jul 12 '21 at 10:09
  • @NikitaDemodov So what you need is a string, which is just the name of the function, instead of a function pointer to itself. – Yves Jul 12 '21 at 10:10
  • @Yves No, I just needed a way to be able to identify where the log came from -- a sort of id. But now I've reconsidered to the string version instead. – Nikita Demodov Jul 12 '21 at 10:12
  • @NikitaDemodov If so, what you need is to locate an exception. You should make this clear in your question, otherwise your question is a XY problem. In fact, there has been many great log library, which can tell you where an exception comes from, when it is thown, etc, it can even tell you the line number of throwning the exception. – Yves Jul 12 '21 at 10:15
  • @NikitaDemodov https://stackoverflow.com/questions/6692238/better-logging-library-for-c – Yves Jul 12 '21 at 10:19
  • @NikitaDemodov If you want to create a logger by yourself to finish your homework, it's not hard. But if you want to create a logger that can be used in the real project, I would say it's very hard. You have to consider many things, such as thread-safe, performance etc. Basically, a real logger should at least be thread-safe and have a good performance, as logger always has lots of IO events, you have to use some OS-level technique, such as sharing memory to get a better performance... Anyway, it's hard... – Yves Jul 12 '21 at 10:24
2

There are no equivalent of this (about class instance) for function/method.

Jarod42
  • 203,559
  • 14
  • 181
  • 302