c++17

Does C++11, 14, 17 or 20 introduce a standard constant for pi?

痴心易碎 提交于 2019-11-28 19:02:27
There is a rather silly problem with the number pi in C and C++. As far as I know M_PI defined in math.h is not required by any standard. New C++ standards introduced a lot of complicated math in the standard library - hyperbolic functions, std::hermite and std::cyl_bessel_i , different random number generators and so on and so forth. Did any of the 'new' standards bring in a constant for pi? If not - why? How does all this complicated math work without it? I am aware of similar questions about pi in C++ (they are several years and standards old); I would like to know the current state of the

How to order types at compile-time?

社会主义新天地 提交于 2019-11-28 18:53:33
Consider the following program: #include <tuple> #include <vector> #include <iostream> #include <type_traits> template <class T> struct ordered {}; template <class... T> struct ordered<std::tuple<T...>> { using type = /* a reordered tuple */; }; template <class T> using ordered_t = typename ordered<T>::type; int main(int argc, char* argv[]) { using type1 = std::tuple<char, std::vector<int>, double>; using type2 = std::tuple<std::vector<int>, double, char>; std::cout << std::is_same_v<type1, type2> << "\n"; // 0 std::cout << std::is_same_v<ordered_t<type1>, ordered_t<type2>> << "\n"; // 1

Why wasn't yield added to C++0x?

风流意气都作罢 提交于 2019-11-28 18:38:50
I have been using yield in many of my Python programs, and it really clears up the code in many cases. I blogged about it and it is one of my site's popular pages. C# also offers yield – it is implemented via state-keeping in the caller side, done through an automatically generated class that keeps the state, local variables of the function, etc. I am currently reading about C++0x and its additions; and while reading about the implementation of lambdas in C++0x, I find out that it was done via automatically generated classes too, equipped with operator() storing the lambda code. The natural

Why is S::x not odr-used?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 18:25:25
问题 Consider this example from cppreference: struct S { static const int x = 1; }; void f() { &S::x; } // discarded-value expression does not odr-use S::x I agree that &S::x is a discarded-value expression , since the standard says (9.2, paragraph 1 [stmt.expr] from n4700) Expression statements have the form expression-statement: expression_opt ; The expression is a discarded-value expression (Clause 8)... However, is that enough for S::x to not be odr-used ? 6.2, paragraph 3 [basic.def.odr]

Simplest way to determine return type of function

北城余情 提交于 2019-11-28 18:08:23
Given a very simple, but lengthy function, such as: int foo(int a, int b, int c, int d) { return 1; } // using ReturnTypeOfFoo = ??? What is the most simple and concise way to determine the function's return type ( ReturnTypeOfFoo , in this example: int ) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)? NathanOliver You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument

Why are are std::allocator's construct and destroy functions deprecated in c++17?

南笙酒味 提交于 2019-11-28 17:04:38
问题 The c++17 specification deprecates the construct and destroy members of the std::allocator object. The working group provided rationale for deprecating other member functions here, under the heading "Deprecate the redundant members of std::allocator". However they don't mention specifically why those two members are deprecated or what the recommendation is for replacing that functionality. I'm assuming the implication is to use std::allocator_traits::construct instead. I'm a bit confused

How the new range-based for loop in C++17 helps Ranges TS?

╄→гoц情女王★ 提交于 2019-11-28 16:37:36
The committee changed the range-based for loop from: C++11: { auto && __range = range_expression ; for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) { range_declaration = *__begin; loop_statement } } to C++17 : { auto && __range = range_expression ; auto __begin = begin_expr ; auto __end = end_expr ; for ( ; __begin != __end; ++__begin) { range_declaration = *__begin; loop_statement } } And people said that this will make implementing Ranges TS easier. Can you give me some examples? TemplateRex C++11/14 range- for was overconstrained... The WG21 paper for this is

std::lock_guard or std::scoped_lock?

人盡茶涼 提交于 2019-11-28 16:16:52
C++17 introduced a new lock class called std::scoped_lock . Judging from the documentation it looks similar to the already existing std::lock_guard class. What's the difference and when should I use it? The scoped_lock is a strictly superior version of lock_guard that locks an arbitrary number of mutexes all at once (using the same deadlock-avoidance algorithm as std::lock ). In new code, you should only ever use scoped_lock . The only reason lock_guard still exists is for compatibility. It could not just be deleted, because it is used in current code. Moreover, it proved undesirable to change

Merit of inline-ASM rounding via putting float into int variable

99封情书 提交于 2019-11-28 14:14:41
I have inherited a pretty interesting piece of code: inline int round(float a) { int i; __asm { fld a fistp i } return i; } My first impulse was to discard it and replace calls with (int)std::round (pre-C++11, would use std::lround if it happened today), but after a while I started to wonder if it might have some merit after all... The use case for this function are all values in [-100, 100] , so even int8_t would be wide enough to hold the result. fistp requires at least a 32 bit memory variable, however, so less than int32_t is just as wasted as more. Now, quite obviously casting the float

Why doesn't void{} exist?

。_饼干妹妹 提交于 2019-11-28 13:32:31
I am wondering why void() is a prvalue of void but void{} does not exist...? See the following answer: https://stackoverflow.com/a/37708167/293195 For example in the context: template<typename R> R foo(){ return R{}; } CWG 2351 , resolved in June, has made void{} legal. 来源: https://stackoverflow.com/questions/53263974/why-doesnt-void-exist