VS2017 & std::string_view - Undefined Identifier

I don't know if this forum is the correct place for this. Apologies if it's isn't. This seems to be more about the compiler than the language itself.

I have VS2017 v15.8.4 and a very simpler header file declaring a simple class. I have set the project option for C++17 (/std:c++17) in the IDE. I have included the string_view header (#include <string_view>) in the .h file. I also have using namespace std; in the header.

However, I get an undeclared identifier error on compilation and the IDE flags the line in the header:

virtual void setWind(string_view, string_view);

with undefined identifier "string_view".

What am I doing wrong?
Last edited on
Try std::string_view
Yeah, sorry about that. I realized that I missed that critical piece of info in my OP.

I have using namespace std; in the header. I've updated the OP.
I have using namespace std; in the header.

Never ever put using namespace std; in a header file.

Does it actually compile? The IDE and the compiler are different things.
Last edited on
Never ever put using namespace std; in a header file.

So, use std::vector and std::string everywhere?

No, it doesn't compile.
error C2061: syntax error: identifier 'string_view'
So, use std::vector and std::string everywhere?

In header files, yes. To understand why, consider what happens if someone one day includes a new header file at the top of their code that has using namespace std; in it. All their variables and functions that were fine before suddenly start colliding with all the existing variables and functions and classes in the std namespace that happen to have the same name.

https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio
https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio

I had already set that option in the project's settings and even tried 'experimental'. For some reason, it isn't working.

I opened the string_view header, and one of the guards, _HAS_CXX17, is still evaluating to 0 and thus skipping the rest of the header but I have no idea why given the project option is set to allow it. I have no idea how to investigate further.

I guess I'll have to go back to const string& until I find an answer.
I don't see how a string can replace a string_view, why in the world are you using a string_view in a place where you don't need its functionality.

A string view is only good for cases where you want to snip a section of a string from another string without copying or using 2 iterators. Using string_view for no reason will confuse people trying to understand your intentions with your code.

Other than that, not being able to control your compiler with simple options is a big deal and not something you should ignore. I would recommend you to start a new test project with the most simplest example of string_view and if it still fails, I would like to see the project file with all the compiler options, and maybe a log.
I have set the project option for C++17 (/std:c++17) in the IDE.
Under Project->Properties->C++/Language->C++ Language Standard ?

Try this snippet:
1
2
3
4
5
6
7
8
#include <iostream>

#include <string_view>

int main() 
{
  std::string_view sv("HEllo");
}
why in the world are you using a string_view in a place where you don't need its functionality.

std::string_view is a useful replacement for const std::string& parameters because it avoids the overhead of creating a std::string object when a string literal is passed as argument.
closed account (E0p9LyTq)
@SimpleCoder, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <string_view>

void display(std::string_view);

int main()
{
   std::string str("Hello");

   display(str);
   display("Goodbye");
}

void display(std::string_view sv)
{
   std::cout << sv << '\n';
}


Compiled successfully with the latest VS2017 Community.
closed account (E0p9LyTq)
poteto wrote:
A string view is only good for cases where you want to snip a section of a string from another string without copying or using 2 iterators.

Consider my previous code snippet.

If I had used std::string& as display's parameter line 12 would have failed to compile.

C2664 'void display(std::string &)': cannot convert argument 1 from 'const char [8]' to 'std::string &'


std::string_view is more useful than you think.

I know it should have been const std::string&, which compiles without error. Easier IMO is using a std::string_view.
Thomas1965 & FurryGuy. First, thanks for your help so far.

In the IDE, Project->Properties->C++/Language->C++ Language Standard set to ISO C++17 Standard (/std:c++17) and the compile of Thomas1965's code snippet results in:
1>------ Build started: Project: StringViewTest, Configuration: Debug Win32 ------
1>StringViewTest.cpp
1>string_view is only available with C++17 or later.
1>d:\dallen\vs 2017 code\stringviewtest\stringviewtest\stringviewtest.cpp(11): error C2039: 'string_view': is not a member of 'std'
1>d:\programs\microsoft visual studio\2017\vc\tools\msvc\14.15.26726\include\string(20): note: see declaration of 'std'
1>d:\dallen\vs 2017 code\stringviewtest\stringviewtest\stringviewtest.cpp(11): error C2065: 'string_view': undeclared identifier
1>d:\dallen\vs 2017 code\stringviewtest\stringviewtest\stringviewtest.cpp(11): error C2146: syntax error: missing ';' before identifier 'sv'
1>d:\dallen\vs 2017 code\stringviewtest\stringviewtest\stringviewtest.cpp(11): error C3861: 'sv': identifier not found
1>Done building project "StringViewTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

FurryGuy, yours compiles basically the same way...after complaining about std there's a cascade of errors, which makes sense, I guess.
Have you maybe changed any other settings in your project?
Have a look if you can run this project
https://www.dropbox.com/sh/0ppjj7icccjdnh1/AACFSLHhqrBmDqG-4D0e-cARa?dl=0
Your project compiles and runs. What the...?

I use pre-complied headers (and so does your project) but I cannot recall explicitly changing any other settings (and there are lots of them) but I'll review them to see. Still, each project I create seems to revert to a standard setting (such as the Language standard).

I also run VAssistX and CodeMaid (from the VS2017 site) but I don't think they have a compile-time impact.
string_view is only available with C++17 or later.

That does imply heavily that your compiler is not in C++17 mode; that your project is not telling the compiler to use C++17.
Ok, even worse (well, sort of because I really dislike not understanding, see below).

After opening your project and seeing it compile, I reopened my original solution, and reverted the change from string_view to const string& so that the function is now how I originally wanted it, i.e.:

virtual void setWind(std::string_view, std::string_view);

And...now, it compiles and runs. 2 x What the...? I changed nothing. Unless your project altered something global, I'm at a loss to explain this. I compared your project properties with mine and there were only two difference that I could find: the name of the pre-compiled header file (yours "stdafx.h", mine "pch.h") and my use of the C++17 standard option.

Many thanks for the help, everyone. Much appreciated. The problem seems to be solved but I have no idea why :-).

I have another question but that'll have to go in another topic.
Last edited on
closed account (E0p9LyTq)
And...now, it compiles and runs..... The problem seems to be solved but I have no idea why

Welcome to the world of computers and programming. When all else fails do a "from the bottom up" rebuild of your source code.

Using pre-compiled headers is very often the cause of transient problems.

What little time you "save" with pre-compiled headers is IMO vastly offset by the frustration of changes to source code not being updated properly.

I don't like that particular feature, I don't use it.

I frequently rebuild a project when I tinker around with the project's source files.
Last edited on
Topic archived. No new replies allowed.