c++17

Explicit destructor call with decltype

回眸只為那壹抹淺笑 提交于 2019-12-22 03:15:26
问题 Consider the following snippet: struct Foo {}; int main() { Foo f; f.~decltype(f)(); // fine with clang, error with gcc f.~decltype(auto)(); // error with both clang and gcc } The rules for an explicit destructor call are handled by the standard grammar with pseudo-destructor-name which is defined as follows: pseudo-destructor-name: nested-name-specifier opt type-name :: ~ type-name nested-name-specifier template simple-template-id :: ~type-name ~ type-name ~ decltype-specifier And: decltype

Class template argument deduction failed with derived class

浪子不回头ぞ 提交于 2019-12-22 01:53:47
问题 #include <utility> template<class T1, class T2> struct mypair : std::pair<T1, T2> { using std::pair<T1, T2>::pair; }; int main() { (void)std::pair(2, 3); // It works (void)mypair(2, 3); // It doesn't work } Is the above well formed? Is it possible deduce the class template arguments in the second case if the constructors are being inherited? Are the constructors of std::pair participating in the creation of implicit deduction guides for mypair ? My compiler is g++ 7.2.0. 回答1: I think this is

Why does std::visit take a variable number of variants?

依然范特西╮ 提交于 2019-12-22 01:26:44
问题 Trying to get more familiar with C++17, I've just noticed std::visit: template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple

How you convert a std::string_view to a const char*?

感情迁移 提交于 2019-12-22 01:24:39
问题 Compiling with gcc-7.1 with the flag -std=c++17 , the following program raises an error: #include <string_view> void foo(const char* cstr) {} void bar(std::string_view str){ foo(str); } The error message is In function 'void bar(std::string_view)': error: cannot convert 'std::string_view {aka std::basic_string_view<char>}' to 'const char*' for argument '1' to 'void foo(const char*)' foo(str); I'm surprised there is no conversion to const char* because other libraries (abseil, bde), provide

How to swap two pointers in multi-threaded c++ 17 program?

扶醉桌前 提交于 2019-12-22 01:18:23
问题 I have two pointers: pA and pB. They points to two big hash map objects. when the hash map pointed by pB is updated completely, I want to swap pB and pA. In C++ 17, how to swap them fast and thread safe? Atomic? I am new to c++ 17. 回答1: Atomic wait-free exchange of 2 pointers can be implemented in the following manner: #include <atomic> #include <cstdint> #include <cassert> template<class T> class Pointers2 { uintptr_t const ab_; std::atomic<uintptr_t> a_; public: Pointers2(T* a, T* b) : ab_

template type deduction failing (std::empty as a predicate)

痴心易碎 提交于 2019-12-21 17:52:47
问题 I have a vector of vectors, and I want to check if all of them are empty. Using the standard library, I tried: #include <algorithm> #include <vector> int main() { std::vector<std::vector<int>> vv; std::all_of(std::begin(vv), std::end(vv), std::empty); } This result in the following error in clang 7.0: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_algo.h:508:5: note: candidate template ignored: couldn't infer template argument '_Predicate' I guess it's a

Does guaranteed copy elision work with function parameters?

六眼飞鱼酱① 提交于 2019-12-21 13:12:13
问题 If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the following code? Foo myfunc(Foo foo) { return foo; } auto foo = myfunc(Foo()); // will there be copies? 回答1: In C++17, prvalues ("anonymous temporaries") are no longer objects. Instead, they are instructions on how to construct an object. They can

Does guaranteed copy elision work with function parameters?

隐身守侯 提交于 2019-12-21 13:10:04
问题 If I understood correctly, starting from C++17, this code now requires that no copy will be done: Foo myfunc(void) { return Foo(); } auto foo = myfunc(); // no copy Is it also true for function arguments? Will copies be optimized away in the following code? Foo myfunc(Foo foo) { return foo; } auto foo = myfunc(Foo()); // will there be copies? 回答1: In C++17, prvalues ("anonymous temporaries") are no longer objects. Instead, they are instructions on how to construct an object. They can

std::launder and strict aliasing rule

倖福魔咒の 提交于 2019-12-21 12:19:46
问题 Consider this code: void f(char * ptr) { auto int_ptr = reinterpret_cast<int*>(ptr); // <---- line of interest // use int_ptr ... } void example_1() { int i = 10; f(reinterpret_cast<char*>(&i)); } void example_2() { alignas(alignof(int)) char storage[sizeof(int)]; new (&storage) int; f(storage); } line of interest with call from example_1: Q1: On the callside the char pointer is aliasing our integer pointer. This is valid. But is it also valid to just cast it back to an int? We know an int is

Forward a template auto

痴心易碎 提交于 2019-12-21 11:02:09
问题 Context (1) One can extract the return type and the argument types of a callable with the following trait: #include <tuple> template<class T> struct callable_trait {}; template<class R, class... Args> struct callable_trait<R(Args...)> { using return_type = R; using argument_types = std::tuple<Args...>; }; (2) Since C++17, one can define a template with template<auto>: If a template parameter is declared auto , its type is deduced from the corresponding argument. Question (3) I should then be