2

I am trying to concatinate __ func__ and literal string in C++ and I am getting an error on GCC (with or without of C++11 standart). Like, for example, here:

#include <cstdio>

int main(void)
{
        printf("%s", __func__ " try #1\n");
        printf(__func__ " try #2\n");
        return 0;
}

Looks like __ func__ is not actually a string literal macros. Does anyone knows why that is happening and, maybe, way to solve it?

Lapshin Dmitry
  • 1,084
  • 9
  • 28
  • 3
    Of course `__func__` is not a dumb string macro. What's wrong with just "%s%s"? Also what's wrong with using proper C++ tools, like `std::string` and `std::cout`? – Griwes Dec 04 '14 at 14:00
  • 2
    @Griwes "Of course"? Considering `__FILE__` and `__LINE__` *are* macros, I can understand why the OP might have thought `__func__` would be too. –  Dec 04 '14 at 14:01
  • What's wrong with `printf("%s try #1\n", __func__ )`? The type of compile-time concatenation that you are trying to perform is applicable only for string literals (i.e., constants strings). – barak manos Dec 04 '14 at 14:02
  • 2
    @hvd Of course "of course". Preprocessor can't possibly know in what function it currently is (but it does know what line or file it is processing). – Griwes Dec 04 '14 at 14:03
  • @Griwes Many compilers have integrated preprocessors, and some do provide something similar to `__func__`, except as a macro. On at least one compiler, `printf("We're now in " __FUNCTION__ "\n");` compiles without any issues. –  Dec 04 '14 at 14:06
  • @Griwes That's just an example, where the error itself is shown. I thought about using concating where the literals are used. – Lapshin Dmitry Dec 04 '14 at 14:10
  • @hvd, examples please. GCC's `__FUNCTION__`, for example, is implemented the exact same way `__func__` is. Also standards define exactly the order of phases of translation; I can hardly see how a preprocessor could know what function it's in. – Griwes Dec 04 '14 at 14:10
  • @Griwes I had edited my previous comment shortly before your last one to give an example. The compiler that I tested that accepts that is MSVC. –  Dec 04 '14 at 14:16

2 Answers2

5

__func__ is indeed neither a string literal nor a macro. Its specification from C++11 8.4.1/8 is

The function-local predefined variable __func__ is defined as if a definition of the form

static const char __func__[] = "function-name ";

had been provided

So you'll have to treat it like a static array, not a string literal; in particular, you can't concatenate it with other literals.

printf("%s%s", __func__, " try #1\n");
printf("%s try #2\n", __func__);
std::string message = __func__ + std::string(" try #3");
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
4

Technically, __func__ is a predefined identifier. It's never implemented as a macro.

It has the type static const char __func__[] and so therefore you can't concatenate it the way you have. Of course, you can add it to a std::string though.

Use

printf("%s try #1\n", __func__);

instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thank you for explanation about __ func__. I really thought about using __ func__ as a string literal, now I see that's impossible. – Lapshin Dmitry Dec 04 '14 at 14:13