c++17

std::function with noexcept in C++17

对着背影说爱祢 提交于 2019-12-03 22:14:45
In C++17 noexcept has been added to the type system : void r1( void (*f)() noexcept ) { f(); } void foo() { throw 1; } int main() { r1(foo); } The latest versions of GCC and Clang in C++17 mode reject the call r1(foo) , because void (*)() cannot be implicitly converted to void (*)() noexcept . But with std::function instead: #include <functional> void r2( std::function<void() noexcept> f ) { f(); } void foo() { throw 1; } int main() { r2(foo); } Clang accepts the program, apparently ignoring the noexcept specifier; and g++ gives a strange error regarding std::function<void() noexcept> . What

Why can't decomposition declarations be constexpr?

好久不见. 提交于 2019-12-03 22:03:15
Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings) #include <cassert> #include <utility> constexpr auto divmod(int n, int d) { return std::make_pair(n / d, n % d); // in g++7, also just std::pair{n/d, n%d} } int main() { constexpr auto [q, r] = divmod(10, 3); static_assert(q == 3 && r ==1); } This fails on both g++7-SVN and clang-4.0-SVN with the message: decomposition declaration cannot be declared 'constexpr' Dropping the constexpr definition and changing to a regular assert() works on both compilers. None of

Where to use std::variant over union?

对着背影说爱祢 提交于 2019-12-03 22:02:22
Please explain what is the difference between union and std::variant and why std::variant was introduced into the standard? In what situations should we use std::variant over the old-school union ? Generally speaking, you should prefer variant unless one of the following comes up: You're cheating. You're doing type-punning or other things that are UB but you're hoping your compiler won't break your code. You're doing some of the pseudo-punnery that C++ union s are allowed to do: conversion between layout-compatible types or between common initial sequences. You explicitly need trivial

Construct an empty object without the default constructor

江枫思渺然 提交于 2019-12-03 20:40:19
Suppose I have a type F . I know that F is empty, but F has no default constructor, so I can't use F() to construct it. Is there a way to obtain a valid object of type F anyway? I seem to recall a mention that there was such a way with arcane usage of unions. Ideally, it would be constexpr friendly. This can be useful because captureless lambdas only gained a default constructor in C++20. In C++17, if I want to "pass a lambda to a template" and call that lambda without having an instance of it, I need to be able to reconstruct it from the type. auto const f = [](int x) { return x; }; using F =

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

落花浮王杯 提交于 2019-12-03 19:56:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . 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

How do I build gcc with C++ concepts (“concepts lite”) support?

半世苍凉 提交于 2019-12-03 19:46:14
问题 The C++ standards committee is working on a TS (Technical Specification) for Concepts extension: "Programming Languages - C++ Extensions for Concepts". N4377 is the latest version of this document. For inclusion into the C++ standard features are asked to be implemented, ideally for a publicly accessible system. I'm aware of concept-gcc but the concepts proposal above (colloquially referred to as Concepts Lite ) is different. I heard that there is a concepts branch and I have tried the origin

Is std::memcpy between different trivially copyable types undefined behavior?

空扰寡人 提交于 2019-12-03 18:36:02
问题 I've been using std::memcpy to circumvent strict aliasing for a long time. For example, inspecting a float , like this: float f = ...; uint32_t i; static_assert(sizeof(f)==sizeof(i)); std::memcpy(&i, &f, sizeof(i)); // use i to extract f's sign, exponent & significand However, this time, I've checked the standard, I haven't found anything that validates this. All I found is this: For any object (other than a potentially-overlapping subobject) of trivially copyable type T, whether or not the

What's the difference between static constexpr and static inline variables in C++17?

拥有回忆 提交于 2019-12-03 18:28:10
问题 With C++17 we get inline variables. One of use for them is to define constant fields in classes. So what's the difference between these two constant definitions: class MyClass { static constexpr int myFirstVar = 10; static const inline int mySecondVar = 100; }; Of course constexpr makes myFirstVar implicitly inline. What's the better choice here, to use constexpr or inline ? Note: when you don't need constness, then inline makes it easier. With constexpr you don't have that choice. 回答1: You

Deprecation of std::allocator<void>

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:41:29
问题 Related: Why do standard containers require allocator_type::value_type to be the element type? It is said that the following has been deprecated since C++17: template<> struct allocator<void>; I wonder whether it is deprecated because the primary template alone is now able to accommodate allocator<void> , or the use-case of allocator<void> is deprecated. If latter, I wonder why. I think allocator<void> is useful in specifying an allocator not bound to a specific type (so just some schema

T declval() instead of T && declval() for common_type

旧时模样 提交于 2019-12-03 17:27:08
Isn't it better to use std::declval declared in form: template< class T > T declval(); // (1) then current one: template< class T > T && declval(); // (2) for std::common_type (possibly with different name only for this current purpose)? Behaviour of common_type using (1) is closer to the behaviour of the ternary operator (but not using std::decay_t ) than the behaviour when using (2) : template< typename T > T declval(); template <class ...T> struct common_type; template< class... T > using common_type_t = typename common_type<T...>::type; template <class T> struct common_type<T> { typedef T