vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Template Variable Specialization Error: "variable xx has already been initialized"

Open zehuac2 opened this issue 7 years ago • 7 comments

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

  1. Set settings.json as follows
{
    "C_Cpp.default.cppStandard": "c++17"
}
  1. Create a main.cpp file;
  2. 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;
}
  1. After the red flame goes off, the error should occur;
  2. 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

screen shot 2019-01-29 at 00 31 59 screen shot 2019-01-29 at 00 32 50

Additional context

zehuac2 avatar Jan 29 '19 06:01 zehuac2

is there any issue

YassinMahmoudEl-Zouhery avatar Jan 29 '19 17:01 YassinMahmoudEl-Zouhery

Yes, the plugin is throwing a syntax error when the compiler does not.

zehuac2 avatar Jan 29 '19 18:01 zehuac2

@sean-mcmanus this looks like a bug we had in the past. Did we get a fix for it yet?

bobbrow avatar Jan 29 '19 18:01 bobbrow

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

sean-mcmanus avatar Jan 29 '19 21:01 sean-mcmanus

They re-opened https://developercommunity.visualstudio.com/content/problem/281377/intellisense-problem-1.html so you can upvote that.

sean-mcmanus avatar Jan 30 '19 01:01 sean-mcmanus

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

sean-mcmanus avatar Apr 10 '19 08:04 sean-mcmanus

Thanks!

zehuac2 avatar Apr 11 '19 14:04 zehuac2