c++17

GCC and Clang disagree about C++17 constexpr lambda captures

眉间皱痕 提交于 2019-11-30 17:25:32
Consider this example which declares a variable as constexpr, captures it by copy in a lambda, and declares another constexpr variable which is the result of a constexpr function unwrapping a non-type template parameter from the original variable. #include <utility> template<int I> constexpr auto unwrap(std::integral_constant<int, I>) { return I; } int main() { constexpr auto i = std::integral_constant<int, 42>{}; constexpr auto l = [i]() { constexpr int x = unwrap(i); }; } Clang (trunk) accepts this code. ( wandbox ) GCC (trunk) fails with the following error message ( wandbox ): lambda

Preventing users from creating unnamed instances of a class [duplicate]

无人久伴 提交于 2019-11-30 17:17:17
This question already has an answer here: How to avoid C++ anonymous objects 6 answers For many RAII "guard" classes, being instantiated as anonymous variables does not make sense at all: { std::lock_guard<std::mutex>{some_mutex}; // Does not protect the scope! // The unnamed instance is immediately destroyed. } { scope_guard{[]{ cleanup(); }}; // `cleanup()` is executed immediately! // The unnamed instance is immediately destroyed. } From this article : Anonymous variables in C++ have “expression scope”, meaning they are destroyed at the end of the expression in which they are created. Is

fatal error: filesystem: No such file or directory [closed]

六眼飞鱼酱① 提交于 2019-11-30 14:32:56
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . Using, CentOs 7.1, gcc version 6.1.0 (GCC) I receive this error: fatal error: filesystem: No such file or directory on this line #include <filesystem> compiling with g++ main.cpp -o main -std=c++17 where is the problem? 回答1: It seems you have to include <filesystem> like this: #include

Structured binding to replace std::tie abuse

*爱你&永不变心* 提交于 2019-11-30 13:56:05
In reading this summary of the c++17 final features I was a bit surprised by the section on structured bindings (emphasis mine): structured bindings Until now, there was a known trick to abuse std::tie to assign a tuple or pair to different variables directly, instead of having to deal with the result type manually. This was a hack , and also the variables had to exist, now you can declare the variables and initialize them in one line: auto [a , b , c] = getvalues(); The braces are needed, getvalues returns a tuple. std::pair is not mentioned in the proposal, so its unclear if this works with

Check if a type is passed in variadic template parameter pack

南笙酒味 提交于 2019-11-30 13:50:10
I've heard somewhere, that using new C++1z syntax, it is really easy to check if a type is passed in variadic template parameter pack - apparently you can do this with code that is near one-line long. Is this true? What are those relevant features? (I tried looking through fold expressions but I can't see how to use them in that problem...) Here's how I solved the problem in C++11 for reference: #include <type_traits> template<typename T, typename ...Ts> struct contains; template<typename T> struct contains<T> { static constexpr bool value = false; }; template<typename T1, typename T2,

structured bindings with std::minmax and rvalues

孤人 提交于 2019-11-30 13:48:20
问题 I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the same with a literal integer. #include <algorithm> #include <cstdio> #include <tuple> int main() { auto [amin, amax] = std::minmax(3, 6); printf("%d,%d\n", amin, amax); // undefined,undefined int bmin, bmax; std::tie(bmin, bmax) = std::minmax(3, 6)

How does std::visit work with std::variant?

孤者浪人 提交于 2019-11-30 12:52:16
I'm looking at std:variant/std::visit doc here: http://en.cppreference.com/w/cpp/utility/variant/visit and also googled a lot trying to understand the magic behind std::visit and std::variant . So my question is the following. In the provided example, both in the polymorphic lambda and the "overloaded" there is some "magic" happening that makes it possible to extract the correct type from std::variant . So looking at this: for (auto& v: vec) { std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg)

Status of ranges for C++1z? [closed]

隐身守侯 提交于 2019-11-30 12:36:16
There is a study group on ranges in the C++ committee: but I have not followed the history of this study group and I am not sure of what kind of delivery is expected for C++1z (furthermore I do not use boost.range so I do not have a clear view of existing practices). Will we have: ranges as a pair of first/last iterators? union and other set operations on ranges(for example [v.begin()+5, v.begin()+7[ U [v.begin()+10, v.begin()+15[ U [v.begin()+21, v.begin()+42[ ), namely: union, intersection, disjoint union, complement? iterator filters (in order to execute a for_each where a condition is

Can I initialize an array using the std::initializer_list instead of brace-enclosed initializer?

只愿长相守 提交于 2019-11-30 12:35:02
Can I initialize an array using the std::initializer_list object instead of brace-enclosed initializer? As known, we can do this: http://en.cppreference.com/w/cpp/language/aggregate_initialization unsigned char b[5]{"abc"}; // equivalent to unsigned char b[5] = {'a', 'b', 'c', '\0', '\0'}; int ar[] = {1,2,3}; std::array<int, 3> std_ar2{ {1,2,3} }; // std::array is an aggregate std::array<int, 3> std_ar1 = {1, 2, 3}; But I can't initialize an array by std::initializer_list il; : http://ideone.com/f6aflX #include <iostream> #include <initializer_list> #include <array> int main() { int arr1[] = {

Is it undefined behavior to `reinterpret_cast` a `T*` to `T(*)[N]`?

随声附和 提交于 2019-11-30 12:33:43
问题 Consider the following scenario: std::array<int, 8> a; auto p = reinterpret_cast<int(*)[8]>(a.data()); (*p)[0] = 42; Is this undefined behavior ? I think it is. a.data() returns a int* , which is not the same as int(*)[8] The type aliasing rules on cppreference seem to suggest that the reinterpret_cast is not valid As a programmer, I know that the memory location pointed by a.data() is an array of 8 int objects Is there any rule I am missing that makes this reinterpret_cast valid? 回答1: An