c++17

Ok to use std::getline() with a moved-from std::string?

天大地大妈咪最大 提交于 2020-02-21 10:27:07
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

how to convert filesystem path to string

半城伤御伤魂 提交于 2020-02-20 06:46:06
问题 I am iterating through all the files in a folder and just want their names in a string. I want to get a string from a std::filesystem::path . How do I do that? My code: #include <string> #include <iostream> #include <filesystem> namespace fs = std::experimental::filesystem; int main() { std::string path = "C:/Users/user1/Desktop"; for (auto & p : fs::directory_iterator(path)) std::string fileName = p.path; } However I get the following error: non-standard syntax; use '&' to create a pointer

How to parse json file with type composition of std::optional and std::variant

谁都会走 提交于 2020-02-16 12:28:30
问题 How can I parse the input json inside this file, especially for the accuracy , secondary and flags properties? https://github.com/smogon/pokemon-showdown/blob/master/data/moves.js They are type composition of optional and variant . Though only the full example can surface the problem in a clearer way, a minimal starting point example would be this one: [ {}, { "secondary": false }, { "secondary": { "chance": 10, "boosts": { "spd": -1 } } }, { "secondary": { "chance": 30, "volatileStatus":

Variadic base class using declaration fails to compile in MSVC

夙愿已清 提交于 2020-02-04 20:57:19
问题 I'm trying to implement a variadic visitor class. template<typename T> class VisitorBaseFor { protected: virtual ~VisitorBaseFor() = default; public: virtual void visit(T &t) = 0; }; template<typename... Ts> class VisitorBase : public VisitorBaseFor<Ts>... { public: using VisitorBaseFor<Ts>::visit...; }; I know from that overload trick that variadic using declarations should be possible, but MSVC does not compile my code saying I need to expand Ts while both GCC and Clang compile my code

Visual Studio 2017 can't find std::variant

不羁岁月 提交于 2020-02-03 08:26:33
问题 I am using Visual Studio 2017 - 15.9.7 for C++ development, and it looks like I have the necessary modules installed. But the code below gives the error - "namespace "std" has no member "variant"" std::variant is supported in C++17 and looks like it was introduced to Visual Studio in 15.0 #include <iostream> #include <variant> int main() { std::variant<int, double, std::string> value; } It looks like someone asked the same thing before, but the question appears to have been deleted. 回答1:

Why isn't argument deduction allowed in function return type?

青春壹個敷衍的年華 提交于 2020-02-01 04:12:05
问题 The most obvious answer could be - because the standard says so . That's fine, but I'm wrapping my head around it to understand the reasons behind this choice. Consider the following example: template<typename T> struct S { S(T) {} }; S f() { return 0; } int main() { auto s = f(); (void)s; } It fails to compile with errors like: error: use of class template 'S' requires template arguments; argument deduction not allowed in function return type Quite easy to fix, it isn't a problem, something

Should I compare a std::string to “string” or “string”s?

一世执手 提交于 2020-01-30 14:15:52
问题 Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first sight, it looks like comparing against a const char* needs less assembly instructions 1 , as using a string literal will lead to an in-place construction of the std::string . ( EDIT: As pointed out in the answers, I forgot about the fact that effectively s

Should I compare a std::string to “string” or “string”s?

烈酒焚心 提交于 2020-01-30 14:15:39
问题 Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first sight, it looks like comparing against a const char* needs less assembly instructions 1 , as using a string literal will lead to an in-place construction of the std::string . ( EDIT: As pointed out in the answers, I forgot about the fact that effectively s

Should I compare a std::string to “string” or “string”s?

与世无争的帅哥 提交于 2020-01-30 14:15:27
问题 Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first sight, it looks like comparing against a const char* needs less assembly instructions 1 , as using a string literal will lead to an in-place construction of the std::string . ( EDIT: As pointed out in the answers, I forgot about the fact that effectively s

C++ Library & Self registering classes: Factory map empty in client application

狂风中的少年 提交于 2020-01-30 10:49:37
问题 Let there be a C++ library (let's call it lib ) which gets included as a static library in an application (let's call it app ). Within the lib there's a base class node . Each subclass of a node is identified by a UUID. I employ a self registering pattern to ensure that new classes register themselves at the factory. The factory allows to build a node subclass object based on the provided UUID. The app builds objects through the lib 's factory::build() function. My factory is based on the