c++17

Is “enum class” a class type in C++?

两盒软妹~` 提交于 2019-12-03 04:12:34
I read about enumeration declaration in C++ using cppreference . Then I have made Enum class and check whether it is a class type or not using std::is_class . #include <iostream> enum class Enum { red = 1, blue, green }; int main() { std::cout << std::boolalpha; std::cout << std::is_class<Enum>::value << '\n'; } Then I compiled and ran in G++ compiler on Linux platform, it prints false value. So Is enum class type or not? If enum is a class type, then why I'm getting false value? enum class is not a class definition - the combination of keywords is used to define a scoped enumeration , which

How to efficiently get a `string_view` for a substring of `std::string`

☆樱花仙子☆ 提交于 2019-12-03 04:02:21
问题 Using http://en.cppreference.com/w/cpp/string/basic_string_view as a reference, I see no way to do this more elegantly: std::string s = "hello world!"; std::string_view v = s; v = v.substr(6, 5); // "world" Worse, the naive approach is a pitfall and leaves v a dangling reference to a temporary: std::string s = "hello world!"; std::string_view v(s.substr(6, 5)); // OOPS! I seem to remember something like there might be an addition to the standard library to return a substring as a view: auto v

How to write constexpr swap function to change endianess of an integer?

送分小仙女□ 提交于 2019-12-03 03:47:12
问题 How to write a constexpr function to swap endianess of an integer, without relying on compiler extensions and can you give an example on how to do it? 回答1: Yes, it's pretty easy; here's a recursive (C++11-compatible) implementation (unsigned integral types only): #include <climits> #include <cstdint> #include <type_traits> template<class T> constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type bswap(T i, T j = 0u, std::size_t n = 0u) { return n == sizeof(T) ? j : bswap<T>(i >

What are contracts (as proposed for C++17)?

半城伤御伤魂 提交于 2019-12-03 03:34:28
问题 I was reading about contracts in Thoughts about C++17 by B. Stroustrup and assisted a small presentation talking about them but I am not sure I have understood them really. So I have a some interrogations and if it is possible to illustrate them with some examples : Are contracts just a better replacement of the classic assert() and should they be used together ? What contracts really are put in simple terms for a software dev ? Would contracts have an impact on how we handle exceptions ? If

Why use std::make_unique in C++17?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 03:23:38
问题 As far as I understand, C++14 introduced std::make_unique because, as a result of the parameter evaluation order not being specified, this was unsafe: f(std::unique_ptr<MyClass>(new MyClass(param)), g()); // Syntax A (Explanation: if the evaluation first allocates the memory for the raw pointer, then calls g() and an exception is thrown before the std::unique_ptr construction, then the memory is leaked.) Calling std::make_unique was a way to constrain the call order, thus making things safe:

How to get the address of a C++ lambda function within the lambda itself?

无人久伴 提交于 2019-12-03 02:13:22
问题 I'm trying to figure out how to get the address of a lambda function within itself. Here is a sample code: []() { std::cout << "Address of this lambda function is => " << ???? }(); I know that I can capture the lambda in a variable and print the address, but I want to do it in place when this anonymous function is executing. Is there a simpler way to do so? 回答1: It is not directly possible. However, lambda captures are classes and the address of an object coincides with the address of its

Using const std::unique_ptr for pimpl idiom

我与影子孤独终老i 提交于 2019-12-03 01:50:21
In Herb Sutter's talk at CppCon16 he suggested writing pimpl idiom with const std::unique_ptr (roughly 10 minutes in). How is this supposed to work with move constructors/assignments? Is there something in c++17? I couldn't find anything. If your class is supposed to be never-empty, a non-const unique ptr (with default move/assigns) is not appropriate. The move ctor and move assign will both empty the rhs. A const unique ptr will disable these automatic methods, and if you want move you will have to write it within the impl (and a bit of glue outside). I would personally write a value ptr with

Why does std::string_view create a dangling view in a ternary expression?

て烟熏妆下的殇ゞ 提交于 2019-12-03 01:35:13
Consider a method that returns a std::string_view either from a method that returns a const std::string& or from an empty string. To my surprise, writing the method this way results in a dangling string view: const std::string& otherMethod(); std::string_view myMethod(bool bla) { return bla ? otherMethod() : ""; // Dangling view! } https://godbolt.org/z/1Hu_p2 It seems that the compiler first puts a temporary std::string copy of the result of otherMethod() on the stack and then returns a view of this temporary copy instead of just returning a view of the reference. First I thought about a

Using std::launder to “validate” non “pointer to object” pointer value since C++17

∥☆過路亽.° 提交于 2019-12-03 01:34:50
According to this answer , since C++17, even if a pointer has the right address and the right type dereferencing it can cause undefined behaviour . alignas(int) unsigned char buffer[2*sizeof(int)]; auto p1=new(buffer) int{}; auto p2=new(p1+1) int{}; *(p1+1)=10; // UB since c++17 The reason is that the pointer value of p1+1 is a pointer past-the-end of an object. Can this example be brought back to defined behavior using std::launder : *std::launder(p1+1)=10; // still UB? Secondly, would it also be useful in this following case? alignas(int) unsigned char buffer[3*sizeof(int)]; auto pi = new

How will C++17 exception specifier type system work?

安稳与你 提交于 2019-12-03 01:32:06
Studying about "noexcept specifier(and operator)", I wrote a simple code. And I am surprised that this piece of code: void asdf() noexcept {} int main() { auto f = asdf; std::cout << std::boolalpha << noexcept(f()) << std::endl; } prints false , even function "asdf" is noexcept-specified. So while searching why this mysterious phenomenon is happening, I found C++17's "exception specifier type system"- P0012R1 . According to this (accepted) proposal, since C++17; as noexcept is part of function type, will the code above print true ? And one more, in this question's one line: std::function<void(