c++17

What are use cases for structured bindings?

天大地大妈咪最大 提交于 2019-12-18 12:52:19
问题 C++17 standard introduces a new structured bindings feature, which was initially proposed in 2015 and whose syntactic appearance was widely discussed later. Some uses for them come to mind as soon as you look through documentation. Aggregates decomposition Let's declare a tuple: std::tuple<int, std::string> t(42, "foo"); Named elementwise copies may be easily obtained with structured bindings in one line: auto [i, s] = t; which is equivalent to: auto i = std::get<0>(t); auto s = std::get<1>(t

What are use cases for structured bindings?

萝らか妹 提交于 2019-12-18 12:50:08
问题 C++17 standard introduces a new structured bindings feature, which was initially proposed in 2015 and whose syntactic appearance was widely discussed later. Some uses for them come to mind as soon as you look through documentation. Aggregates decomposition Let's declare a tuple: std::tuple<int, std::string> t(42, "foo"); Named elementwise copies may be easily obtained with structured bindings in one line: auto [i, s] = t; which is equivalent to: auto i = std::get<0>(t); auto s = std::get<1>(t

Xcode 10 call to unavailable function std::visit

為{幸葍}努か 提交于 2019-12-18 12:15:05
问题 When compiling following program with Xcode 10 GM: #include <iostream> #include <string> #include <variant> void hello(int) { std::cout << "hello, int" << std::endl; } void hello(std::string const & msg) { std::cout << "hello, " << msg << std::endl; } int main(int argc, const char * argv[]) { // insert code here... std::variant< int, std::string > var; std::visit ( []( auto parameter ) { hello( parameter ); }, var ); return 0; } I get the following error: main.cpp:27:5: Call to unavailable

Using a filesystem::path, how do you open a file in a cross-platform way?

假装没事ソ 提交于 2019-12-18 12:11:46
问题 Let's say you have used the new std::filesystem (or std::experimental::filesystem ) code to hunt down a file. You have a path variable that contains the full pathname to this variable. How do you open that file? That may sound silly, but consider the obvious answer: std::filesystem::path my_path = ...; std::ifstream stream(my_path.c_str(), std::ios::binary); This is not guaranteed to work. Why? Because on Windows for example, path::string_type is std::wstring . So path::c_str will return a

Using a filesystem::path, how do you open a file in a cross-platform way?

你。 提交于 2019-12-18 12:11:29
问题 Let's say you have used the new std::filesystem (or std::experimental::filesystem ) code to hunt down a file. You have a path variable that contains the full pathname to this variable. How do you open that file? That may sound silly, but consider the obvious answer: std::filesystem::path my_path = ...; std::ifstream stream(my_path.c_str(), std::ios::binary); This is not guaranteed to work. Why? Because on Windows for example, path::string_type is std::wstring . So path::c_str will return a

What is std::invoke in c++? [closed]

拟墨画扇 提交于 2019-12-18 11:22:21
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I've just been reading about std::thread and std::bind which I've faced with the Callable concept and std::invoke . I read about std::invoke on cppreference but I didn't understand what it said. Here is my question: What is std::invoke , std::function , std::bind and the Callable

Usefulness of std::make_pair and std::make_tuple in C++1z

℡╲_俬逩灬. 提交于 2019-12-18 11:20:13
问题 In my understanding, the sole reason for the existence of std::make_pair and std::make_tuple is that you don't have to write the types by yourself as they are automatically deduced. In C++1z, we have template argument deduction for class templates, which allows us to simply write std::pair p(1, 2.5); // C++1z instead of auto p = std::make_pair(1, 2.5); // C++11/14 The situation for std::tuple is analogous. This leads to the following question: In C++1z, is there a situation in which it is

C++17: Keep only some members when tuple unpacking

[亡魂溺海] 提交于 2019-12-18 10:59:11
问题 Let's imagine you need to call the following method: std::tuple<int, int, int> foo(); In C++17, you can call the function and unpack the tuple in a single line: auto [a, b, c] = foo(); Now, how can I proceed to store only b and c and to discard a ? Currently, I'm only aware of two options: 1 - I can use a dummy variable when auto-unpacking However, the dummy variable will be unused and it will issue a warning, so if I want to silent that warning the code will be quite unpleasant to see:

Variables marked as const using structured bindings are not const

坚强是说给别人听的谎言 提交于 2019-12-18 10:58:20
问题 I have been writing a set of classes to allow for a simple python-like zip -function. The following snippet works (almost) just as expected. However, the two variables a and b are not const . std::vector<double> v1{0.0, 1.1, 2.2, 3.3}; std::vector<int> v2{0, 1, 2}; for (auto const& [a, b] : zip(v1, v2)) { std::cout << a << '\t' << b << std::endl; a = 3; // I expected this to give a compiler error, but it does not std::cout << a << '\t' << b << std::endl; } I have been using gcc 7.3.0. Here is

Can I generate a function without providing arguments?

ぐ巨炮叔叔 提交于 2019-12-18 09:36:15
问题 So c++17 has std::function Deduction Guides so given: int foo(); I can do: std::function bar(foo); But I'm stuck on a c++14 compiler. There I have to do something more like: function<int()> bar(foo) . I was wondering if there was a way to create a std::function without passing the function pointer and explicitly providing the function signature? So for example make_pair will deduce the type of it's return from it's arguments. I was wondering if I could write something similar for function s