c++17

How do we test if an expression of a certain type can be invoked with a prvalue?

泪湿孤枕 提交于 2019-12-03 11:35:39
问题 With c++17 we have fancy new is_invocable and fancy new prvalues that aren't really values. This permits you to create an object without having to first logically construct it, then elide the construction. I have run into a problem where using std::is_invocable to test if you can call something, and prvalue rules, seem to collide: struct no_move { no_move(no_move&&)=delete; explicit no_move(int) {} }; void f( no_move ) {} now can we ask if f can be invoked using a prvalue of type no_move ? f(

Using const std::unique_ptr for pimpl idiom

好久不见. 提交于 2019-12-03 11:24:03
问题 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. 回答1: 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

How do I use the new C++17 execution policies? [duplicate]

跟風遠走 提交于 2019-12-03 11:20:13
This question already has answers here : Are C++17 Parallel Algorithms implemented already? (4 answers) I was reading through the std::algorithm documentation at cppreference.com and I noticed a C++17 tag on a lot of cool things I haven't used yet. What got my attention most was the new execution policies. What I gathered from reading about them is that I can make any for_each loop I want multi-threaded just by specifying an execution policy. For example, I have a program which outputs an image with a 2D graphic on it. int main(){ std::for_each( img.buffer().begin(), img.buffer().end(),

Ambiguous partial specializations with Clang in C++17

浪子不回头ぞ 提交于 2019-12-03 11:19:15
template <typename Foo, Foo Part> struct TSelect {}; enum What { The }; template <typename Foo> struct AnotherOneSelector { static constexpr Foo Id = Foo::The; }; template <typename Foo, typename SelectPartType> struct THelper; template <typename Foo> struct THelper<Foo, TSelect<Foo, AnotherOneSelector<Foo>::Id>> {}; template <typename Foo, Foo PartId> struct THelper<Foo, TSelect<Foo, PartId>> {}; int main() { THelper<What, TSelect<What, What::The>> t; } This code compiles with gcc8.1 with each of the standard option (c++11, c++14, c++17), but clang trunk does not with c++17 (although with c+

What does the standard say about char arrays as template arguments?

时间秒杀一切 提交于 2019-12-03 11:18:58
During my research for an answer for this question I found (I did not know that before) that gcc and clang allow char arrays to be template arguments if they are declared static . E.g. this code compiles with gcc and clang: #include <type_traits> template <int N, const char (&string)[N]> auto foo() { if constexpr (string[0] == 'i') return 0; else return 3.14f; } void bar() { static constexpr char string1[] = "int"; static constexpr char string2[] = "float"; auto i = foo<sizeof(string1), string1>(); auto f = foo<sizeof(string2), string2>(); static_assert(std::is_same_v<decltype(i), int>);

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

こ雲淡風輕ζ 提交于 2019-12-03 11:15:15
问题 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

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

雨燕双飞 提交于 2019-12-03 11:15:12
问题 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

Is it legal to check whether the address of a subobject lies within the bounds of a containing object

纵然是瞬间 提交于 2019-12-03 11:11:51
2 Questions: Is the following code well formed with defined behaviour? Is there any possible c++ implementation in which it could assert? Code (c++11 and higher): #include <cassert> #include <utility> #include <ciso646> template<class T> auto to_address(T* p) { return reinterpret_cast<unsigned char const*>(p); } /// Test whether part is a sub-object of object template<class Object, class Part> bool is_within_object(Object& object, Part& part) { auto first = to_address(std::addressof(object)), last = first + sizeof(Object); auto p = to_address(std::addressof(part)); return (first <= p) and (p <

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

一笑奈何 提交于 2019-12-03 11:07:12
问题 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

Clang and GCC disagree in auto specifier for non-type template parameter in a casting C++17

六月ゝ 毕业季﹏ 提交于 2019-12-03 11:00:45
I basically have a class that depends on a non-type template parameter. I defined a casting so an object of non-type template parameter N can convert to another of M . I have a minimal example that can reproduce the situation: template<auto Integral> class Test{ public: typedef decltype(Integral) value_type; static constexpr value_type N = Integral; constexpr Test (const value_type& x = 0); template<auto Integral2> constexpr explicit operator Test<Integral2>() const; private: value_type n; }; template<auto Integral> constexpr Test<Integral>::Test (const value_type& x){ if (x < 0){ n = N - (-x)