Link to home
Start Free TrialLog in
Avatar of jrheisey
jrheisey

asked on

GCC C++: Defining unicode string const in macro

This macro is intended to define a string as both ASICII and unicode.

#define DEFINE_XML_TOKEN_STRING(n, s) const char n##a[] = #s; const wchar_t n##w[] = L#s;

DEFINE_XML_TOKEN_STRING(CONFIG_ELM, config)

Show result in this code:

const char CONFIG_ELMa[] = "config"; const wchar CONFIG_ELMw[] = L"config";

Works with fine Visual Studio. The preprocessor in GCC seems to have a problem reporting error:
error: 'L' was not declared in this scope
note: in expansion of macro 'DEFINE_XML_TOKEN_STRING'
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

I would try compiling with compiler flag -std=c++11. Will try it when I can.
It works for me without any special flags. I have gcc 4.8.2 - what do you have?
Also I had to use wchar_t rather than wchar
14:53:04$ cat t.cpp
using namespace std;
int main (int argc, char **argv)
{
  const char CONFIG_ELMa[] = "config"; const wchar_t CONFIG_ELMw[] = L"config";
  return 0;
}                                  /* int main (int argc, char **argv) */
15:02:10$ cat Makefile 
t: t.cpp
        g++ $^ -o $@
15:02:14$ make
g++ t.cpp -o t

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This macro works for g++
#define DEFINE_XML_TOKEN_STRING(n, s) const char n##a[] = #s; const wchar_t n##w[] = L###s;

Open in new window

You will have to test whether Visual Studio accepts it
Avatar of jrheisey
jrheisey

ASKER

Then the pre-processor is adding the space. It is not in the macro definition.
Fine. But you now have a solution. Does Visual Studio accept the amended macro? If not, you will have to conditionally define the macro according to platform.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60492

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You could most likely also use L"" #s too due to string concatenating rules.

Open in new window