1

Using defines I am able to declare a wide string that uses the value of regular string as follows:

#define MY_STRING "my value"
#define MY_WIDE_STRING L"" MY_STRING

How can I achieve the same but with member variables and a initialization list - i.e. something like this...

class MyClass
{
    private:
         const std::string MY_STRING
         const std::wstring MY_WIDE_STRING
    public:
         MyClass():MY_STRING("my value"), MY_WIDE_STRING(L"" MY_STRING)
         {
         }

Thanks

fxfuture
  • 1,910
  • 3
  • 26
  • 40

3 Answers3

1

A possible solution:

#define MY_VALUE "my value"

class MyClass
{
  private:
    const std::string MY_STRING
    const std::wstring MY_WIDE_STRING
  public:
    MyClass():MY_STRING(MY_VALUE), MY_WIDE_STRING(L"" MY_VALUE)
    {}
  // ...
};

I assume the reason you need both the char and wchar_t versions of the same string are because you're calling different libraries that you don't control. Rather than keeping two copies of the string in sync, it's probably better to do a run-time conversion from one to the other when you must have the other version.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • When the code is macro expanded, the compiler will see `MY_WIDE_STRING(L"" "my value")`. `L""` and `"my value` are separate literals of different data types. It is not guaranteed that the compiler will concatenate the quotes together before applying the `L` prefix to the final concatenated result. To ensure concatenation, you have to use `L"my value"` instead. – Remy Lebeau May 04 '13 at 18:26
  • @RemyLebeau: I'm not a language lawyer, but the standard says, "In translation phase 6 (2.2), adjacent string literals are concatenated. ... If one string literal has no _encoding-prefix_, it is treated as a string literal of the same _encoding-prefix_ as the other operand." [S2.14.5 P13] That seems to suggest that this should work as intended. – Adrian McCarthy May 08 '13 at 16:21
0

You're not. MY_STRING is not a macro in this case, so you cannot use preprocessor concatenation.

If you think you need to store both strings, I recommend revisiting your design.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • ok thanks. in that case how could I convert MY_STRING to MY_WIDE_STRING and keep it as a constant. Can this be done in the initialization list? – fxfuture May 02 '13 at 09:46
  • @fxfuture: No. Why do you think you need that? – Lightness Races in Orbit May 02 '13 at 09:47
  • i want to specify a single constant string value and have it available as a wide string to functions as soon as the class is instantiated so I don't have convert it each time – fxfuture May 02 '13 at 09:49
  • @fxfuture: Whydo you ever need to "convert it"? Why do you not stick with one or the other? – Lightness Races in Orbit May 02 '13 at 09:57
  • I have two different functions. one which processes strings and one which processes wide strings. i need "my value" available to use in both functions – fxfuture May 02 '13 at 10:01
  • @fxfuture: Fix your code so that the functions accept _consistent_ input. – Lightness Races in Orbit May 02 '13 at 10:01
  • i have to be able to process both a string variable and a wide string variable. i have no control over this. i was looking at this but it doesn't seem to help matters much -http://stackoverflow.com/questions/9846318/is-it-possible-to-have-a-c-method-accept-either-const-char-and-const-wchar-t – fxfuture May 02 '13 at 10:12
  • @fxfuture: _Why_ do you have to? – Lightness Races in Orbit May 02 '13 at 10:17
  • because i have both an ascii and a unicode variable that require processing - that's what i start with. i am currently using an overloaded function to handle them individually. those functions either contain regular functions such as std::string newString(oldString) or wide functions such as std::wstring newString(oldString) and they require my constant values in either regular or wide string format. – fxfuture May 02 '13 at 10:31
  • @fxfuture: You're not answering my question. I'm trying to determine how you have arrived at this unfortunate and smelly constraint. – Lightness Races in Orbit May 02 '13 at 10:54
0

Try something like this:

#define MY_VALUE "my value"

#define MAKE_WIDE_STRING_NX(S) L ## S
#define MAKE_WIDE_STRING(S) MAKE_WIDE_STRING_NX(S)

class MyClass
{
    private:
         const std::string _str;
         const std::wstring _wstr;
    public:
         MyClass(): _str(MY_VALUE), _wstr(MAKE_WIDE_STRING(MY_VALUE))
         {
         }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770