Template Variable Specialization Error: "variable xx has already been initialized"
Type: LanguageService
Describe the bug
- OS and Version: macOS 10.14.12
- VS Code Version: 1.30.2
- C/C++ Extension Version: 0.21.0
- Other extensions you installed (and if the issue persists after disabling them):
- C#
- C# XML Documentation
- CMake
- Debugger for Unity
- LaTeX Workshop
- One Dark Pro
- Pola Code
- Verilog HDL/SystemVerilog
- vscode-icons
- x86 and x86_64 Assembly
- A clear and concise description of what the bug is.
In the json parser that I am working on, I am using template variables as a part of my parser so that my parser would work with all types of chars in C++. The file that's causing the problem looks like the following snippet, but with a lot more template variables.
template<typename CharT>
inline constexpr CharT beginObject{};
template<>
inline constexpr char beginObject<char>{ '{' };
The clang++ compiler that I have on my mac (Apple LLVM version 10.0.0 (clang-1000.11.45.5)) compiles the code just fine, but vs code produces the following error:
variable "beginObject [with CharT=char]" has already been initialized
To Reproduce
- Set
settings.jsonas follows
{
"C_Cpp.default.cppStandard": "c++17"
}
- Create a main.cpp file;
- Copy and paste the following into the main file
// #include "gtest/gtest.h"
#include <iostream>
template<typename CharT>
CharT letter{};
template<>
char letter<char>{ 'a' };
int main(int argc, char **argv)
{
std::cout << letter<char> << std::endl;
// testing::InitGoogleTest(&argc, argv);
// return RUN_ALL_TESTS();
return 0;
}
- After the red flame goes off, the error should occur;
- To compile, use the following compiler flags (clang)
-std=c++17
(The actual project file that's causing the error looks very similar to this, but with a lot more template variables used in the json parser and does not have the main function. I am happy to provide the source code if needed.)
Expected behavior
There should be no errors from the plugin, as the compiler compiles the code just fine.
Screenshots
Additional context
is there any issue
Yes, the plugin is throwing a syntax error when the compiler does not.
@sean-mcmanus this looks like a bug we had in the past. Did we get a fix for it yet?
It got resolved as Won't Fix by the VS team -- we'll try to get it re-opened.
https://developercommunity.visualstudio.com/content/problem/281377/intellisense-problem-1.html https://developercommunity.visualstudio.com/content/problem/368977/intellisence-error-when-using-variable-template-sp.html https://developercommunity.visualstudio.com/content/problem/398349/intellisense-error-when-using-variable-template-sp.html
They re-opened https://developercommunity.visualstudio.com/content/problem/281377/intellisense-problem-1.html so you can upvote that.
The fix for this incorrect IntelliSense error is in the latest Insiders version of the extension: https://github.com/Microsoft/vscode-cpptools/releases/tag/0.22.2-insiders
Thanks!