Concatenate the __FUNCTION__ macro processor with a string
It should be trivial, but I can't figure out how to concatenate __FUNCTION__
with strings, especially on GCC, even though it works on VC ++ (I'm porting some code to Linux)
#include <iostream>
#include <string>
#define KLASS_NAME "Global"
int main()
{
std::string msg = KLASS_NAME "::" __FUNCTION__;
std::cout << msg << std::endl;
}
GCC error message
Test.cpp:9:36: error: expected ‘,’ or ‘;’ before ‘__FUNCTION__’
std::string msg = KLASS_NAME "::" __FUNCTION__;
Update
Thanks to Chris, it seems that contiguous string literals are concatenated [reference] . So VC ++ might be correct in this case, as long as you don't think it __FUNCTION__
is non-standard.
+3
source to share
1 answer
You need a concatenation operator and explicitly create a string to find the correct concatenation operator:
#include <iostream>
#include <string>
#define KLASS_NAME "Global"
int main()
{
std::string msg = std::string(KLASS_NAME) + "::" + __FUNCTION__;
std::cout << msg << std::endl;
}
Live example: http://ideone.com/vn4yra
+1
source to share