Reputation: 6587
Using the latest versions of Visual Studio 2019 and Visual Studio. In both I am trying to use this header-only class to display a vector:
#pragma once
#include <cstring>
#include <iterator>
#include <vector>
// Header-only helper class for using strings
class stringHelper
{
public:
// Constructor / destructor
stringHelper() {}
~stringHelper() {}
template <class T>
void PrintVector(std::vector<T> input, const char cSeparator = ' ', bool bNewLine = false)
{
// Output an array (std::vector) to the console eg. {1,11,21} => 1 11 21.
// Example usage: stringHelper::PrintVector<int>(vecOfInts);
if (input.size() > 0)
{
for (std::vector<T>::iterator it = input.begin(); it != input.end();)
{
std::cout << *it;
if (++it != input.end())
std::cout << cSeparator;
}
if (bNewLine)
std::cout << std::endl;
}
}
};
Visual Studio Code has been configured to use the cl.exe
compiler from Visual Studio 2019 as follows:
{
// Use MSVC cl.exe (eg. Visual Studio 2019) on Windows
"type": "shell",
"label": "cl.exe: Build active file (DEBUG)",
"command": "cl.exe",
"args": [
"/std:c++17",
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
}
}
A simple test program might be:
#include <vector>
#include "stringHelper.h"
int main()
{
// Create and print out a vector
stringHelper helper;
std::vector<int> vec = {1, 2, 17};
helper.PrintVector<int>(vec); // Expecting: 1 2 17
return 0;
}
In Visual Studio Code, the program compiles and produces the correct result. The Visual Studio project is configured to use C++17 (I tried C++14 as well). When I compile in Visual Studio 2019 Professional, multiple C2760
compile errors are generated:
Error C2760 syntax error: unexpected token 'identifier', expected ';'
When I select this line in the Error List
, the token it
is highlighted.
Change the internal loop in PrintVector
to the following, which is subtly different and not what I need. Now everything compiles and works fine in both Visual Studio Code and 2019:
for (auto a : input)
{
std::cout << a << cSeparator;
}
The Microsoft help on C2760 does not appear to apply. Is this a compiler or VS 2019 bug? Why is this failing in Visual Studio 2019 but not Visual Studio Code?
Upvotes: 1
Views: 2087
Reputation: 6587
This problem was reported to Microsoft who responded (in under 48 hours!) with an explanation of the underlying issue. This is not a compiler bug, but relates to type deduction when using templates. Read the full discussion here.
One of the comments provided a useful link to this StackOverflow answer with similar details.
std::vector<T>::iterator
is a dependent name which creates ambiguity issuesstd::vector<T>::iterator
is a type or use auto
(code examples below)cl.exe
when using the /permissive- option. This is enabled for Visual Studio 2019
but not (by default) for Visual Studio Code
or the Visual Studio 2019 Developer Command Prompt
.To specify as a type:
typename std::vector<T>::iterator it = input.begin();
for (; it != input.end();)
To use auto
:
for (auto it = input.begin(); it != input.end();)
Both solutions have been tested and resolve the compile problem.
Upvotes: 2