c++17

Why is std::move not [[nodiscard]] in C++20?

早过忘川 提交于 2020-04-29 08:47:10
问题 I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet? 回答1: AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20.

Deprecated header <codecvt> replacement

戏子无情 提交于 2020-04-18 05:47:58
问题 A bit of foreground: my task required converting UTF-8 XML file to UTF-16 (with proper header, of course). And so I searched about usual ways of converting UTF-8 to UTF-16, and found out that one should use templates from <codecvt> . But now when it is deprecated, I wonder what is the new common way of doing the same task? (Don't mind using Boost at all, but other than that I prefer to stay as close to standard library as possible.) 回答1: std::codecvt template from <locale> itself isn't

Why can't I use a std::tuple in a constexpr lambda function

懵懂的女人 提交于 2020-04-17 03:12:32
问题 I have the following code: #include <string_view> #include <array> #include <tuple> struct Variable { size_t index; std::string_view name; std::tuple<float, float> bounds; }; constexpr std::array<Variable, 3> myarray = [](){ std::array<Variable, 3> res{}; std::array<std::string_view, 3> strings = {"myvar1", "myvar2", "myvar3"}; std::array<std::tuple<float, float>, 3> bounds = {{{0,1}, {1,2}, {2,3}}}; for (std::size_t i = 0; i != res.size(); ++i) { res[i] = {i, strings[i], bounds[i]}; } return

Why does link_libraries(stdc++fs) work but not -lstdc++fs? [duplicate]

给你一囗甜甜゛ 提交于 2020-04-16 06:07:28
问题 This question already has an answer here : Flag '-l' in CMAKE_CXX_FLAGS doesn't work (1 answer) Closed 8 days ago . I was trying to compile a C++17 program on Ubuntu using CMake/g++ 8.1 which contained #include <filesystem> When I used this set(CMAKE_CXX_FLAGS "-lstdc++fs") I got a weird linker error undefined reference to `std::filesystem::__cxx11::recursive_directory_iterator::~recursive_directory_iterator()' This error also appeared when I tried calling g++ manually with the -lstdc++fs

what are rules about when the return type is reference

为君一笑 提交于 2020-04-16 03:29:05
问题 #include <iostream> int func0(){ int a = 0; return a; } int&& func1(){ int a = 0; return a; } int main(){ int&& result0 = func0(); int&& result1 = func1(); } return statement rules are: A function returns to its caller by the return statement. [...] the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand. The rule about how to initialize the object of the function call is only #2. We know

What is the best way to output Unicode to console?

核能气质少年 提交于 2020-04-16 02:47:51
问题 The bounty expires in 6 days . Answers to this question are eligible for a +50 reputation bounty. Luismi98 is looking for a canonical answer . I am working with C++17 in Visual Studio 2019. I have read a fair bit about encodings but I am still not very comfortable with them. I want to output UNICODE characters to screen. For that, I am using the following code #include <iostream> #include <fcntl.h> #include <io.h> std::wstring symbol{ L"♚" }; _setmode(_fileno(stdout), _O_WTEXT); std::wcout <<

Safely convert std::string_view to int (like stoi or atoi)

笑着哭i 提交于 2020-04-13 03:24:14
问题 Is there a safe standard way to convert std::string_view to int ? Since C++11 std::string lets us use stoi to convert to int : std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2)); // Works, have i2 = 23 try { int i3 = stoi(std::string("abc")); } catch(const std::exception& e) { std::cout << e.what() << std::endl; // Correctly throws 'invalid stoi argument' } But stoi does not support std::string_view . So alternatively, we could use atoi ,

Safely convert std::string_view to int (like stoi or atoi)

心不动则不痛 提交于 2020-04-13 03:23:28
问题 Is there a safe standard way to convert std::string_view to int ? Since C++11 std::string lets us use stoi to convert to int : std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2)); // Works, have i2 = 23 try { int i3 = stoi(std::string("abc")); } catch(const std::exception& e) { std::cout << e.what() << std::endl; // Correctly throws 'invalid stoi argument' } But stoi does not support std::string_view . So alternatively, we could use atoi ,

[[maybe_unused]] and Constructors

生来就可爱ヽ(ⅴ<●) 提交于 2020-04-10 07:37:11
问题 Trying to compile the sqlpp17 codebase with gcc 8.2.1 and clang 6.0.1 have been a really strange experience. The code pushes the compilers to the limits and I hit probably a few compiler bugs in the meantime. From the GCC Docs, [[maybe_unused]] is implemented since version 7, but if used this way: struct foo { foo([[maybe_unused]] bool thing1) { } }; I hit this specific error: <source>:2:9: error: expected unqualified-id before '[' token foo([[maybe_unused]] bool thing1) ^ <source>:2:9: error

[[maybe_unused]] and Constructors

我们两清 提交于 2020-04-10 07:37:04
问题 Trying to compile the sqlpp17 codebase with gcc 8.2.1 and clang 6.0.1 have been a really strange experience. The code pushes the compilers to the limits and I hit probably a few compiler bugs in the meantime. From the GCC Docs, [[maybe_unused]] is implemented since version 7, but if used this way: struct foo { foo([[maybe_unused]] bool thing1) { } }; I hit this specific error: <source>:2:9: error: expected unqualified-id before '[' token foo([[maybe_unused]] bool thing1) ^ <source>:2:9: error