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:
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 usingnamespace 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.
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.
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.
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.
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.
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.:
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.