Initializing const members of structs in C/C++… compiler dependent?

后端 未结 4 1783
春和景丽
春和景丽 2020-12-19 08:35

Recently I ran into a compiler error in a legacy environment using Borland C++ 5.2. I had a .cpp file which included a header from some C source which I don\'t control. The

4条回答
  •  孤城傲影
    2020-12-19 09:03

    On g++ 4.6.1 on the same Linux machine, -Wall -ansi -pedantic does not bring up any warnings.

    This point might not have been so high on compiler writers' agenda. It looks so to me, looking at VS2003's and VS2008's behaviours, and, looking at g++ 3.3.3's behaviour you posted and g++ 4.6.1's behaviour I observed.

    Can you consider changing the const to a private:, non const? That way, you will still have some control over who writes to it by not exporting a setter for it while, not generating compiler errors.

提交回复
热议问题