When using braced initialisation in C++, you may encounter the error expected ‘;’ at end of declaration. An example image of this from the VS Code terminal is shown below:
If you see this or any other error that you expect is valid C++, you need to check the compiler version to see what C++ version you are compiling to.
I used Clang 14.0 on macOS inside VS Code in the case above. The fix to resolve the bug was to set the C++ STD version in the compiler command line flags.
In VS Code, these flags are set in the task settings. To access your task settings, go to the .vscode folder and open tasks.json
In the tasks array “tasks”: [ you will see the args array. In this array, we can add a flag for the compiler to specify the C++ version.
In my case, I was able to add support for C++ 11 by adding the following arg: ”-std=c++11” as shown in the next image:
Running the build tasks after recompiling shows no more errors.
In conclusion, if you expect that your code is correct and that IntelliSense or a linter is providing erroneous error messages, double-check your individual project environment to ensure that you are compiling against the correct C++ standard.