Boost logo

Boost :

Subject: Re: [boost] String literals concatenation issue when using BOOST_CURRENT_FUNCTION
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2010-01-16 11:00:18


vicente.botet wrote:
> Hi,
>
> I want to define a macro as
> #define BOOST_CHRONO_DIGITAL_TIME_FORMAT(F) "\n" F " tokes %d day(s) %h:%m:%s.%n\n"
> #define BOOST_CHRONO_DIGITAL_TIME_FUNCTION_FORMAT BOOST_CHRONO_DIGITAL_TIME_FORMAT(BOOST_CURRENT_FUNCTION)
>
> I have a compile error with gcc(3.4.4 or 4.4.0) (Boost v1.41) in the following sentence
>
> const char* c= "\n" BOOST_CURRENT_FUNCTION " %d day(s) %h:%m:%s.%n\n";
>
> C:\cygwin\boost_1_41_0\libs\chrono\example\digital_time_example.cpp: In function `int f1(long int)':
> C:\cygwin\boost_1_41_0\libs\chrono\example\digital_time_example.cpp:24: error: expected `;' before "__PRETTY_FUNCTION__"
>
> as well as
> const char* c= "\n" __PRETTY_FUNCTION__ " %d day(s) %h:%m:%s.%n\n";
>
> The same code works as I expected with MSVC.
>
> The following code works well with gcc
> const char* c= "\n" "f" " %d day(s) %h:%m:%s.%n\n";
> const char* c2= BOOST_CURRENT_FUNCTION;
>
> as well as this one
>
> const char* c= "\n" __FILE__" %d day(s) %h:%m:%s.%n\n";
>
> Is there something wrong? Does gcc manage __PRETTY_FUNCTION__ and __FILE__ differently? Does someone knows a workaround?

Yes. __PRETTY_FUNCTION__ is not a string literal. From the gcc manual:

"These identifiers are not preprocessor macros. In GCC 3.3 and
earlier, in C only, `__FUNCTION__' and `__PRETTY_FUNCTION__' were
treated as string literals; they could be used to initialize `char'
arrays, and they could be concatenated with other string literals. GCC
3.4 and later treat them as variables, like `__func__'. In C++,
`__FUNCTION__' and `__PRETTY_FUNCTION__' have always been variables."

John Bytheway


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk