c++17

Unsafe, `noexcept` and no-overhead way of accessing `std::variant`

[亡魂溺海] 提交于 2019-12-04 17:50:03
问题 std::variant provides the following access functions: std::get_if: take pointer to variant , return pointer to alternative. template <std::size_t I, typename... Ts> auto* std::get_if(std::variant<Ts...>* pv) noexcept; If pv is not a null pointer and pv->index() == I , returns a pointer to the value stored in the variant pointed to by pv . Otherwise, returns a null pointer value. This means that get_if 's implementation roughly looks like this: template <std::size_t I, typename... Ts> auto*

difference between std::mutex and std::shared_mutex

巧了我就是萌 提交于 2019-12-04 17:37:36
问题 I came across an std::shared_mutex in C++17 . what exactly is std::shared_mutex and how it is different from std::mutex ? 回答1: As noted in the documentation The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In contrast to other mutex types which facilitate exclusive access, a shared_mutex has two levels of access: shared - several threads can share ownership of the same mutex . exclusive - only

Why is gcc failing when using lambda for non-type template parameter?

给你一囗甜甜゛ 提交于 2019-12-04 17:37:34
问题 The following snippet compiles with no error with Clang 4.0 but GCC 7.0 produces errors (note the use of -std=c++1z flag). using FuncT = int (*)(double); template <FuncT FUNC> int temp_foo(double a) { return FUNC(a); } int foo(double a) { return 42; } void func() { auto lambda = [](double a) { return 5; }; struct MyStruct { static int foo(double a) { return 42; } }; temp_foo<foo>(3); temp_foo<static_cast<FuncT>(lambda)>(3); temp_foo<MyStruct::foo>(3); } Specifically, GCC complains that both

Copy/assignment of fundamental types

落爺英雄遲暮 提交于 2019-12-04 17:16:41
What does the standard say about copy/assignment of fundamental types? For class types, we have copy constructor, assignment operator, which takes the right hand side as a reference (it must be a reference, otherwise we had infinite recursion): struct Foo { Foo(const Foo &); }; How does this defined for fundamental types? Look at this example: const Foo foo; Foo f = foo; const int a = 2; int b = a; Here, f = foo; odr-uses foo , as copy-constructor takes a reference, right?. If copy of fundamental types had a reference parameter, then b = a would odr-use a as well. Is it the case? If not, how

Is this a bug? Constexpr constructor silently becomes non-constexpr

纵饮孤独 提交于 2019-12-04 17:10:03
问题 Look at this code: struct NonConstexpr { NonConstexpr() { } }; template <typename T> struct Bar { NonConstexpr nonConstexpr; constexpr Bar() { } }; struct Foo { Bar<void> bar; constexpr Foo() { } }; Foo has a member, Foo::bar::nonConstexpr , which has a non-constexpr constructor. So, my expectation is that this should not compile. But it compiles with gcc, clang and msvc. Is this a compiler bug, or some rule allows this code to compile? If I add a NonConstexpr member into Foo directly, the

Using strings in switch statements - where do we stand with C++17?

笑着哭i 提交于 2019-12-04 17:09:36
问题 Every one of us has (probably) had the childhood dream of writing: switch(my_std_string) { case "foo": do_stuff(); break; case "bar": do_other_stuff(); break; default: just_give_up(); } but this is not possible, as is explained in the answers to this question from the olden days (2009): Why switch statement cannot be applied on strings? Since then we've seen the advent of C++11, which lets us go as far as: switch (my_hash::hash(my_std_string)) { case "foo"_hash: do_stuff(); break; case "bar"

Return Optional value with ?: operator

最后都变了- 提交于 2019-12-04 17:07:36
问题 I often need to use optional type for functions: std::optional<int32_t> get(const std::string& field) { auto it = map.find(field); if (it != map.end()) return it->second; return {}; } Is there a way to return optional value in one line? e.g. this: std::optional<int32_t> get(const std::string& field) { auto it = map.find(field); return it != map.end() ? it->second : {}; } results in the error error: expected primary-expression before '{' token return it != map.end() ? it->second : {}; ^ 回答1:

Using std::visit on a class inheriting from std::variant - libstdc++ vs libc++

ⅰ亾dé卋堺 提交于 2019-12-04 16:50:21
问题 Consider the following code snippet: struct v : std::variant<int, std::vector<v>> { }; int main() { std::visit([](auto){ }, v{0}); } clang++ 7 with -stdlib=libc++ -std=c++2a compiles the code; g++ 9 with -std=c++2a fails to compile the code, with the following error: /opt/compiler-explorer/gcc-trunk-20180711/include/c++/9.0.0/variant:94:29: error: incomplete type 'std::variant_size' used in nested name specifier inline constexpr size_t variant_size_v = variant_size<_Variant>::value; ^~~~~~~~~

What is the auto Bracketed List Syntax?

左心房为你撑大大i 提交于 2019-12-04 16:48:28
W.F. gave a now-deleted answer to my question here which used the line: auto [x, y] = div_t{ 1, 0 }; From the code in the answer it looks like that's like a tie for the div_t struct. I was hoping that someone could explain what was going on here. The complete function code was as follows: constexpr bool first_quot() { auto [x, y] = std::div_t{1, 0}; (void)y; return x; } In the most most recent draft of the C++17 specification it's called "Decomposition declarations" and is defined in section 8.5 [dcl.decomp]. 来源: https://stackoverflow.com/questions/41569419/what-is-the-auto-bracketed-list

Class template argument deduction and default template parameters

馋奶兔 提交于 2019-12-04 16:47:33
问题 The following stripped down code doesn't work with the latest clang++5 but is accepted by g++7: template<typename Wrapped, typename U> struct wrapper; template<typename Wrapped, typename U=int> struct wrapper { wrapper() = default; // Automatic deduction guide constexpr explicit wrapper(Wrapped) noexcept {} }; int main() { struct {} dummy; constexpr auto wrapped = wrapper(dummy); } It fails with the following error messages: <source>:18:30: error: no viable constructor or deduction guide for