PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] How to fix compilation errors in MSVC related to declaration of anonymous struct inside a 'for' loop?

 June 26, 2022     anonymous-struct, c++, c++14, compiler-errors, visual-c++     No comments   

Issue

We can declare anonymous struct inside for loop as below (g++):

for(struct { bool OK = true; } s; s.OK; s.OK = false)
  std::cout << "Hello, world!\n";

But, this code results in compilation error in MSVC as:

source_file.cpp(7): error C2332: 'struct': missing tag name
source_file.cpp(7): error C2062: type 'bool' unexpected
source_file.cpp(7): error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int

How to fix it?


Version:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26430 for x86
Copyright (C) Microsoft Corporation. All rights reserved.


Solution

Please not that MSVC 19.14 shows the error C2062: type 'bool' unexpected even for a named struct S as in this example:

  for(struct S { bool OK = true; } s; s.OK; s.OK = false)
    std::cout << "Hello, world!\n";

Demo: https://godbolt.org/z/Ev1GMGMYd

I think the easiest fix would be to upgrade to the next compiler version MSVC 19.15 where both issues are fixed. Demo: https://godbolt.org/z/W14dvW5eW



Answered By - Fedor
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing