tags:

views:

160

answers:

3

I have some legacy code built with c++ compiler giving the error in the subject line

typedef struct foo {
    char const * const str;
 } Foo;

and a lot of places in the code (meaning I cannot change all of them) use it in a C style initialization:

 Foo arr[] ={
     {"death"},
     {"turture"},
     {"kill"}
  }

What is the good workaround to remove the stupid warning?

+1  A: 

You can't modify the const pointer after construction. You need a constructor initializer to initialize 'str'.

struct Foo
{
  const char* const str;
  explicit Foo(const char* str_) : str(str_) {}
};

Note that 'typedef' is not required in C++ for a case like this.

EDIT:

If you can't use a constructor, then you must make your pointer non-const:

struct Foo
{
  const char* str;
};

ANOTHER EDIT:

After litb's comment, I tried the original code and it does indeed compile (g++ 4.1.2 and XL C/C++ 8.0) with no warnings. Perhaps the compiler in question is doing a construction followed by assignment in this case? I used less violent strings, but I doubt that would make a difference. ;v)

Fred Larson
it doesnt address the problem, cuz I cannot define a constructor as it will break the rest of the code.
vehomzzz
what does the explicit do?
dicroce
'explicit' prevents implicit conversion. For example, if you had a function that accepted a const reference to Foo, you couldn't just pass a char* to it and have a Foo implicitly created.
Fred Larson
However, why is his original code not valid or bad (thus giving a warning?)? I don't see him changing the pointer afterwards.
Johannes Schaub - litb
@litb: Well, you're more knowledgeable about the standards than I am. How is the initialization handled in a case like this? Are the array member constructed using the initial values, or are they default-construct (since there's no constructor defined) and then assigned? If it's the latter case, my answer is right. If it's the former, maybe I'm not right.
Fred Larson
This is aggregate initialization which, as the name implies, is initialization, not assignment. The warning does seem a lot like one that simply arises because the compiler is too old.
coppro
@coppro: I believe you're right. However, my answer may be a usable workaround for the OP's compiler issue. I'll leave the answer up for now.
Fred Larson
@Andrei - how does adding an explicit constructor break the rest of the code?
Bill
@Bill, It's going to forbid initializing the class with braces like `{ "hello" }`
Johannes Schaub - litb
+1  A: 

Check the compiler's documentation to see if there's a workaround. Your code is perfectly compliant, so without any more information, it's impossible to help.

coppro
A: 

If it works anyway, you might as well disable the warning in the command line for those files.

With gcc I think it's something like -wd411.

Not so elegant, but if it works and if the code appears to be compliant to the standard (in this order) there's no point sweating over it!

Matthieu M.