c++17

With std::byte standardized, when do we use a void* and when a byte*?

て烟熏妆下的殇ゞ 提交于 2019-12-05 18:37:30
问题 C++17 will include std::byte, a type for one atomically-addressable unit of memory, having 8 bits on typical computers. Before this standardization, there is already a bit of dilemma when pointing into "raw" memory - between using char* / unsigned char* on one hand or void * on the other. Now, one of reasons for prefering void * is removed - std::byte does not have the same connotations as a char ; it's about raw memory, not characters. So, my question is: What is a good rule of thumb, for

Is the constexpr specifier required on the declaration of a constexpr static member initialized outside of the class?

六眼飞鱼酱① 提交于 2019-12-05 17:21:19
问题 C++17 §10.1.5/1 states: The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (10.1.6). If any declaration of a function or function template has a constexpr specifier, then all its declarations shall contain the constexpr specifier. A similar paragraph has existed in the standard

Associativity of fold-expressions

假如想象 提交于 2019-12-05 16:44:51
问题 N4191 proposed fold-expressions to C++. The definition there was that (args + ...) is a left-fold (i.e. (((a0 + a1) + a2) + ...) , and that (... + args) is a right-fold (i.e. (... + (a8 + (a9 + a10))) . However, the revised paper N4295 reversed the definitions of left and right unary folds. Question : what is the rationale? It seems more intuitive (at least when you are used to left-to-right alphabets) to evaluate (args + ...) from left-to-right. 回答1: From the comment by @cpplearner, here's

Union of layout-compatible types

匆匆过客 提交于 2019-12-05 16:34:30
问题 Look at this code: struct A { short s; int i; }; struct B { short s; int i; }; union U { A a; B b; }; int fn() { U u; u.a.i = 1; return u.b.i; } Is it guaranteed that fn() returns 1 ? Note: this is a follow-up question to this. 回答1: Yes, this is defined behavior. First lets see what the standard has to say about A and B . [class.prop]/3 has A class S is a standard-layout class if it: has no non-static data members of type non-standard-layout class (or array of such types) or reference, has no

Passing arguments to another variadic function

走远了吗. 提交于 2019-12-05 16:19:25
Is there any way at all for this code to compile and work as intended without resorting to va_list stuff ? #include <iostream> void fct(void) { std::cout << std::endl; } void fct(int index, int indexes...) { std::cout << index << ' '; fct(indexes); //or fct(indexes...); ? } int main(void) { fct(1, 2, 3, 4, 5, 6, 7); return 0; } I suspect you have misunderstood the meaning of the signature void fct (int index, int indexes...) I suspect you think that fct() expect a int single value ( index ) and a variadic list of int 's ( indexex... ) with C++11 style of parameter pack expansion. No: it's the

Can constexpr-if-else bodies return different types in constexpr auto function?

戏子无情 提交于 2019-12-05 15:34:05
I'm trying to write a function that maps an enumeration of values to a set of types based on the runtime value of the enumeration. I realize that you cannot return different types based on the runtime value of an enumeration because the compiler wouldn't know how much stack space to allocate. However I'm trying to write this as a constexpr function, using the new if-constexpr functionality to implement this. I'm getting an error from clang complaining that I'm using an illegally specified template parameter. Does anyone see how to implement this? edit: Here is an easier to grok version

Syntax issue when populating an array with a fold expression

删除回忆录丶 提交于 2019-12-05 15:21:13
问题 Yes, I can use std::initializer_list . Yes, even easier, I can do aggregate initialization. But how does this work? I can't seem to fold my head around C++17's fold expressions. There aren't enough examples out there. Here's what I came up with: template<class T, std::size_t N> struct foo { T arr[N]; template<typename... Args> constexpr foo(Args&&... pack) { static_assert(sizeof...(pack) <= N, "Too many args"); std::size_t i = 0; (arr[i++] = ...); } }; int main() { foo<int, 5> a(1, 2, 3, 4, 5

Why is there no std::move_if algorithm?

邮差的信 提交于 2019-12-05 15:18:38
I've seen a few places on the internet where they describe using std::copy_if with std::make_move_iterator , but if the iterator were to be a forward iterator, that would result in having valid but unspecified (VBU) objects scattered around the source container. Wouldn't it be better to have a std::move_if algorithm such that if an object is moved, then it would move the resulting VBU object to the end of the range, like that which is done in the std::remove_if algorithm, consolidating all of the VBU objects together so that they can be erased or reassigned? If move_if existed as an algorithm,

How to enable C++17 support in VSCode C++ Extension

杀马特。学长 韩版系。学妹 提交于 2019-12-05 14:41:39
I keep on getting error squiggles on std::string_view, but I am able to build just fine. Is there a way to tell intellisense or the C++ linter to use C++17? The specific error I get is: namespace "std" has no member "string_view" There's a posting in their GitHub issue tracker about this: std::string_view intellisense missing (CMake, VC++ 2017) . In another issue, it is said that the extension defaults to C++17, but does not yet support all of C++17 features: Setting C++ standard . This is confirmed by c_cpp_properties.json Reference Guide , where an option is listed cppStandard which defaults

C++17 sequencing in assignment: still not implemented in GCC?

眉间皱痕 提交于 2019-12-05 14:32:35
I tried the following code as a naive attempt to implement swapping of R and B bytes in an ABGR word #include <stdio.h> #include <stdint.h> uint32_t ABGR_to_ARGB(uint32_t abgr) { return ((abgr ^= (abgr >> 16) & 0xFF) ^= (abgr & 0xFF) << 16) ^= (abgr >> 16) & 0xFF; } int main() { uint32_t tmp = 0x11223344; printf("%x %x\n", tmp, ABGR_to_ARGB(tmp)); } To my surprise this code "worked" in GCC in C++17 mode - the bytes were swapped http://coliru.stacked-crooked.com/a/43d0fc47f5539746 But it is not supposed to swap bytes! C++17 clearly states that the RHS of assignment is supposed to be [fully]