c++17

What are use cases for structured bindings?

霸气de小男生 提交于 2019-11-30 08:17:48
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); or int i; std::string s; std::tie(i, s) = t; References to tuple elements can also be obtained

structured bindings with std::minmax and rvalues

回眸只為那壹抹淺笑 提交于 2019-11-30 08:14:46
I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the same with a literal integer. #include <algorithm> #include <cstdio> #include <tuple> int main() { auto [amin, amax] = std::minmax(3, 6); printf("%d,%d\n", amin, amax); // undefined,undefined int bmin, bmax; std::tie(bmin, bmax) = std::minmax(3, 6); printf("%d,%d\n", bmin, bmax); // 3,6 } Using GCC 8.1.1 with -O1 -Wuninitialized will result in 0,0

Is void{} legal or not?

痴心易碎 提交于 2019-11-30 08:09:35
This is a follow-up up of this question. In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression . That was fine, it made sense and that was all. Then I came through [7.1.7.4.1/2] ( placeholder type deduction ) of the working draft. There it is said that: [...] - for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type, T is the declared return type and e is the operand of the return statement. If the return statement has no operand, then e is void{} ; [...] So,

What does std::includes actually do?

旧街凉风 提交于 2019-11-30 08:07:48
From the standard, std::includes : Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1) . Returns false otherwise. Note: as this is under [alg.set.operations] , the ranges must be sorted Taking this literally, if we let R1=[first1, last1) and R2=[first2, last2) , this is evaluating: ∀a∈R2 a∈R1 However, this is not what is actually being evaluated. For R1={1} and R2={1,1,1} , std::includes(R1, R2) returns false: #include <algorithm> #include <iomanip> #include <iostream> #include <vector> int main() { std::vector

When should I use [[maybe_unused]]?

[亡魂溺海] 提交于 2019-11-30 08:01:07
What is good about using [[maybe_unused]] ? Consider int winmain(int instance, int /*prevInstance*/, const char */*cmdline*/, int show); int winmain(int instance, [[maybe_unused]] int prevInstance, [[maybe_unused]] const char *cmdline, int show); Some might insist that using comments is ugly, because this keyword was made and intended to be used under these circumstances, and I totally agree with it, but the maybe_unused keywords seems a bit too long to me, making the code slightly harder to read. I would like to follow the standard as "strictly" as I can, but is it worth using? If the

Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr?

霸气de小男生 提交于 2019-11-30 07:56:31
问题 Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr ? For example, see is_integral_v: template< class T > inline constexpr bool is_integral_v = is_integral<T>::value; Is this just a matter of style or is there some difference in behavior? As far as I know constexpr variables are implicitly inline . Edit: Looking at the draft of the latest standard, it also uses inline constexpr . The question actually applies to the standard, then. 回答1: [basic

How can I emulate destructuring in C++?

ぐ巨炮叔叔 提交于 2019-11-30 07:52:40
In JavaScript ES6, there is a language feature known as destructuring . It exists across many other languages as well. In JavaScript ES6, it looks like this: var animal = { species: 'dog', weight: 23, sound: 'woof' } //Destructuring var {species, sound} = animal //The dog says woof! console.log('The ' + species + ' says ' + sound + '!') What can I do in C++ to get a similar syntax and emulate this kind of functionality? For the specific case of std::tuple (or std::pair ) objects, C++ offers the std::tie function which looks similar: std::tuple<int, bool, double> my_obj {1, false, 2.0}; //

Are C++17 Parallel Algorithms implemented already?

浪子不回头ぞ 提交于 2019-11-30 06:55:32
问题 I was trying to play around with the new parallel library features proposed in the C++17 standard, but I couldn't get it to work. I tried compiling with the up-to-date versions of g++ 8.1.1 and clang++-6.0 and -std=c++17 , but neither seemed to support #include <execution> , std::execution::par or anything similar. When looking at the cppreference for parallel algorithms there is a long list of algorithms, claiming Technical specification provides parallelized versions of the following 69

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

我的未来我决定 提交于 2019-11-30 06:17: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 const wchar_t* . And std::ifstream can only take paths with a const char* type. Now it turns out that

How to assert that a constexpr if else clause never happen?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 06:15:40
I want to raise a compile time error when non of the constexpr if conditions is true eg: if constexpr(condition1){ ... } else if constexpr (condition2) { .... } else if constexpr (condition3) { .... } else { // I want the else clause never taken. But I heard the code below is not allowed static_assert(false); } // I'd rather not repeat the conditions again like this: static_assert(condition1 || condition2 || condition3); You have to make the discarded statement dependent of the template parameters template <class...> constexpr std::false_type always_false{}; if constexpr(condition1){ ... }