c++17

Do we need to use std::launder when doing pointer arithmetic within a standard-layout object (e.g., with offsetof)?

社会主义新天地 提交于 2019-12-03 05:38:36
问题 This question is a follow-up to: Is adding to a "char *" pointer UB, when it doesn't actually point to a char array? In CWG 1314, CWG affirmed that it is legal to perform pointer arithmetic within a standard-layout object using an unsigned char pointer. This would appear to imply that some code similar to that in the linked question should work as intended: struct Foo { float x, y, z; }; Foo f; unsigned char *p = reinterpret_cast<unsigned char*>(&f) + offsetof(Foo, z); // (*) *reinterpret

Exact moment of “return” in a C++-function

我是研究僧i 提交于 2019-12-03 05:23:41
问题 It seems like a silly question, but is the exact moment at which return xxx; is "executed" in a function unambiguously defined? Please see the following example to see what I mean (here live): #include <iostream> #include <string> #include <utility> //changes the value of the underlying buffer //when destructed class Writer{ public: std::string &s; Writer(std::string &s_):s(s_){} ~Writer(){ s+="B"; } }; std::string make_string_ok(){ std::string res("A"); Writer w(res); return res; } int main(

What is the value of __cplusplus for C++17?

安稳与你 提交于 2019-12-03 05:23:21
We are trying to test some code under C++17 and its change to std::uncaught_exception . I can't seem to get GCC to provide the value of __cplusplus : $ /opt/local/bin/g++ -std=c++17 -dM -E - </dev/null | grep __cplusplus cc1: warning: command line option '-std=c++1z' is valid for C++/ObjC++ but not for C $ And: $ /opt/local/bin/g++ --version g++-mp-6 (MacPorts gcc6 6.1.0_0) 6.1.0 Copyright (C) 2016 Free Software Foundation, Inc. What is the value of __cplusplus when using C++17? What is the value of __cplusplus when using C++17? According to the draft standard N4594 §16.8/p1 Predefined macro

why declare constrexpr constructors for classes with non-trivial destructors (e.g. unique_ptr, std::variant)

旧时模样 提交于 2019-12-03 05:07:45
As far as I understand (at least for c++14 ), a destructor cannot be constexpr if it is not trivial (implicit generated or =default ). What is the point of declaring constexpr constructors for structures with non-trivial destructors? struct X { int a_; constexpr X(int a) : a_{a} {} // constexpr ~X(){}; // Error dtor cannot be marked constexpr // ~X(){}; // causes error at y declaration: temporary of non-literal type ‘X’ // in a constant expression . }; template <int N> struct Y {}; int main() { Y<X{3}.a_> y; // OK only if the destructor is trivial (void)y; } // tested with c++14 g++-5.1.0 and

Determine whether a constructor of an abstract base class is noexcept?

偶尔善良 提交于 2019-12-03 04:58:53
In C++11 and later, how to determine whether a constructor of an abstract base class is noexcept ? The following methods don't work: #include <new> #include <type_traits> #include <utility> struct Base { Base() noexcept; virtual int f() = 0; }; // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_constructible<Base>::value, ""); // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_default_constructible<Base>::value, ""); // invalid cast to abstract class type 'Base': static_assert(noexcept(Base())

Modern C++: initialize constexpr tables

元气小坏坏 提交于 2019-12-03 04:47:38
Suppose I have a class X , which functionality requires a lot of constant table values, say an array A[1024] . I have a recurrent function f that computes its values, smth like A[x] = f(A[x - 1]); Suppose that A[0] is a known constant, therefore the rest of the array is constant too. What is the best way to calculate these values beforehand, using features of modern C++, and without storaging file with hardcoded values of this array? My workaround was a const static dummy variable: const bool X::dummy = X::SetupTables(); bool X::SetupTables() { A[0] = 1; for (size_t i = 1; i <= A.size(); ++i)

Is it possible to create a lambda on the heap in one step? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-03 04:36:54
This question already has answers here : Lambda with dynamic storage duration (4 answers) We can create a lambda like this: auto x = [](){}; I can create a copy of this on the heap like this: auto y = new decltype(x)(x); The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps? You can use auto in a new-expression: new auto ([](){}); 来源: https://stackoverflow.com/questions/51839698/is-it-possible-to-create-a-lambda-on-the-heap-in-one-step

std::make_shared() change in C++17

巧了我就是萌 提交于 2019-12-03 04:25:13
In cppref , the following holds until C++17: code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g gets called after new int(42) and throws an exception, while f(std::make_shared<int>(42), g()) is safe, since two function calls are never interleaved. I'm wondering which change introduced in C++17 renders this no longer applicable. The evaluation order of function arguments are changed by P0400R0 . Before the change, evaluation of function arguments are unsequenced relative to one another. This means evaluation of g() may be inserted into the evaluation of std:

Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

无人久伴 提交于 2019-12-03 04:19:17
For example, #include <iostream> int main() { unsigned n{}; std::cin >> n; std::cout << n << ' ' << (bool)std::cin << std::endl; } When input -1 , clang 6.0.0 outputs 0 0 while gcc 7.2.0 outputs 4294967295 1 . I'm wondering who is correct. Or maybe both are correct for the standard does not specify this? By fail, I take to mean (bool)std::cin be evaluated false. clang 6.0.0 fails input -0 too. As of Clang 9.0.0 and GCC 9.2.0, both compilers, using either libstdc++ or libc++ in the case of Clang, agree on the result of the program above, independent of the C++ version (>= C++11) used, and print

c_str() vs. data() when it comes to return type

半腔热情 提交于 2019-12-03 04:13:48
After C++11, I thought of c_str() and data() equivalently . C++17 introduces an overload for the latter, that returns a non-constant pointer ( reference , which I am not sure if it's updated completely w.r.t. C++17): const CharT* data() const; (1) CharT* data(); (2) (since C++17) c_str() does only return a constant pointer: const CharT* c_str() const; Why the differentiation of these two methods in C++17, especially when C++11 was the one that made them homogeneous? In other words, why only the one method got an overload, while the other didn't? Kerrek SB The new overload was added by P0272R1