c++17

C++17 fold expression in cout

爱⌒轻易说出口 提交于 2019-12-01 03:42:47
问题 I am learning the new c++17 fold expression and I saw this code from c++17 fold expression. I would like to know why this code work : template<typename ...Args> void printer(Args&&... args) { (std::cout << ... << args) << '\n'; } but not this one : template<typename ...Args> void printer(Args&&... args) { (std::cout << args << ...) << '\n'; } which could seems logic too and would reverse the print order in my opinion. 回答1: As seen on cppreference, binary folds can have the following two forms

What's the Difference Between floor and duration_cast?

为君一笑 提交于 2019-12-01 03:35:00
So in c++11 the Chrono Library provides, duration_cast : Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished And c++17 's floor : Returns the greatest duration t representable in ToDuration that is less or equal to d So for all x will the result of these 2 calls be equal: chrono::duration_cast<chrono::seconds>(x) chrono::floor<chrono::seconds>(x) As far as I can tell, same as the difference between static_cast and std::floor : Negatives are rounded down instead of truncated toward zero. #include <iostream> #include

static_cast'd pointer value

孤街醉人 提交于 2019-12-01 03:33:26
In the current draft standard (and C++17), this is written about static_casting a void * : A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a , and there is an object b of type T (ignoring cv

Using std::launder to get a pointer to an active object member from a pointer to an inactive object?

时光怂恿深爱的人放手 提交于 2019-12-01 03:18:24
This question followes this one Let's consider this example code: struct sso { union{ struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; bool is_short_str() const{ return *std::launder(short_str+15)=='\0'; //UB? } }; If short_str is not the active member dereferencing the pointer without std::launder would be UB. Let's consider that the ABI is well specified and that we know that size_r[7] is at the same address as short_str[15]. Does std::launder(short_str+15) return a pointer to size_r[7] when short_str is not the active member of the union? Nota: I think this is the

Cast lambda to std::function with parameter pack

大城市里の小女人 提交于 2019-12-01 03:14:22
There are several questions on SO that relate to casting lambdas to std::function s, but I have yet to see one that uses a parameter pack for the argument list. This seems broken on my version of g++ (7.1.1-4), and possibly it's just not supported. So is this legal c++17 (by the standard)? If not, why? #include <functional> template <typename TReturn, typename ... TArgs> void Functor(std::function<TReturn (TArgs...)> f) {} int main(int argc, char * argv[]) { auto x = [] (int a, int b) { return a * b; }; Functor<int, int, int>(x); return 0; } The code above won't compile because it fails type

How to force class template argument deduction when constructing a class in its own member functions?

折月煮酒 提交于 2019-12-01 03:10:49
Consider following code: struct A {}; template <typename T> struct B { B(T) {} auto foo() {return B(A{});} // error: no matching function for call to 'B<int>::B(A)' }; auto foo() {return B(A{});} // compiles int main() { foo(); B b(0); b.foo(); } Try it live I understand why B::foo() doesn't compile: Inside of struct B<T> , B (as an injected-class-name) means B<T> unless it's explicitly used as a template. Which in this case prevents class template argument deduction. Let's say I can't do auto foo() {return B<A>(A{});} since my actual code relies on slightly elaborate user-provided deduction

Why string_view instead of generalized container_view<T>?

别说谁变了你拦得住时间么 提交于 2019-12-01 02:36:32
I've found string_view from new C++17 standard a bit redundant. We've got a quite verbose collection of simple mechanisms for passing data to callee , without much overhead and now there is one another which is also specific only to one container type. I don't understand why providing this machinery only for string and not some more generalized type for other containers. One sensible answer is that we've already had these kinds of solutions. For example in C++17 and beyond presentation string_view is explained as observer_ptr<T> (or T*) for string . Please state the arguments against a more

Why should optional<T&> rebind on assignment?

孤者浪人 提交于 2019-12-01 02:26:09
There is an ongoing debate about what optional and variant should do with reference types, particularly with regards to assignment. I would like to better understand the debate around this issue. optional<T&> opt; opt = i; opt = j; // should this rebind or do i=j? Currently, the decision is to make optional<T&> ill-formed and make variant::operator= ill-formed if any of the types is a reference type - to sidestep the argument and still give us most of the functionality. What is the argument that opt = j should rebind the underlying reference? In other words, why should we implement optional

Lambda as default argument fails

∥☆過路亽.° 提交于 2019-12-01 02:25:17
I get an error with the latest versions of clang and gcc with this code: int main() { auto lambda = [] (auto = [] {}) {}; lambda(); } Clang gives the error: prog.cc: In function 'int main()': prog.cc:3:12: error: no match for call to '(main()::<lambda(auto:1)>) ()' lambda(); ^ prog.cc:2:35: note: candidate: template<class auto:1> main()::<lambda(auto:1)> auto lambda = [] (auto = [] {}) {}; ^ prog.cc:2:35: note: template argument deduction/substitution failed: prog.cc:3:12: note: couldn't deduce template parameter 'auto:1' lambda(); ^ Why does this fail? Type deduction for auto does not

int a=1, is a || 1 a constant expression?

一曲冷凌霜 提交于 2019-12-01 02:05:15
N4527 5.20[expr.const]p5 A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and its subobjects: — each non-static data member of reference type refers to an entity that is a permitted result of a constant expression, and — if the object or subobject is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the