|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
"Barry" wrote:
> What's the point that the language constrains on this? The point is to avoid having an object in an indeterminate state which can't get out of it. What would the point of your code be? As the snippet from the standard clearly states the object will be in an indeterminate state because it's a POD-class, and since you declared it const you can't change it, so you'd simply have an unusable object. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
<code>
struct A {}; int main() { const A ca; } </code> <output> "ComeauTest.c", line 5: error: const variable "ca" requires an initializer -- class "A" has no explicitly declared default constructor const A ca; </output> I don't find any thing in standard to support Comeau. If it's a bug with Comeau, then it's quite silly. If I was wrong, then I was doing this wrong all the time. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Barry wrote:
> <code> > struct A {}; > > int main() > { > const A ca; > } > </code> > > <output> > "ComeauTest.c", line 5: error: const variable "ca" requires an > initializer -- class > "A" has no explicitly declared default constructor > const A ca; > </output> > > I don't find any thing in standard to support Comeau. Look in [dcl.init]/9. > If it's a bug with Comeau, then it's quite silly. > If I was wrong, then I was doing this wrong all the time. [dcl.init]/9: "If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user- declared default constructor. Otherwise, if no initializer is specified for a non-static object, the object and its subobjects, if any, have an indeterminate initial value92); if the object or any of its subobjects are of const-qualified type, the program is ill-formed." V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Victor Bazarov wrote:
Thanks V, what if I use C structs in cxx files? After the inclusion of header file the code looks like this: extern "C" { struct A {}; } int main() { const A ca; } But we can't change the original definition of struct A. So we just can't declare /ca/ of /const A/ ? What's the point that the language constrains on this? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Barry wrote:
> Victor Bazarov wrote: > > Thanks V, > > what if I use C structs in cxx files? There is no such thing. If you mean "what if I use POD structs in my C++ program", then the question is legitimate. But you're not gonna like the answer anyway, sorry. What do you mean "what if you use C structs"? > After the inclusion of header file > the code looks like this: > > extern "C" > { > struct A {}; > } > > int main() > { > const A ca; > } > > But we can't change the original definition of struct A. > So we just can't declare /ca/ of /const A/ ? > > What's the point that the language constrains on this? No, you just need to provide a proper initialiser: int main() { const A ca = {}; } V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
![]() |
| Outils de la discussion | |
|
|