c++17

Using std::variant with recursion, without using boost::recursive_wrapper

可紊 提交于 2019-11-28 08:13:14
I'd like to replace boost::variant s with C++17 std::variant and get rid of boost::recursive_wrapper , to remove dependency on boost completely in following code. How may I do that? #include <boost/variant.hpp> #include <type_traits> using v = boost::variant<int, boost::recursive_wrapper<struct s> >; struct s { v val; }; template<template <typename...> class R, typename T, typename ... Ts> auto reduce(T t, Ts ... /*ts*/) { return R<T, Ts...>{t}; } template<typename T, typename F> T adapt(F f) { static_assert(std::is_convertible_v<F, T>, ""); return f; } int main() { int val1 = 42; s val2; auto

Has a std::byte pointer the same aliasing implications as char*?

北慕城南 提交于 2019-11-28 08:08:58
C++ (and C) strict aliasing rules include that a char* and unsigned char* may alias any other pointer. AFAIK there is no analogous rule for uint8_t* . Thus my question: What are the aliasing rules for a std::byte pointer? The C++ reference currently just specifies : Like the character types (char, unsigned char, signed char) it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type. DeiDei From the current Standard draft ([basic.types]/2): For any object (other than a base-class

Why GCC rejects std::optional for references?

戏子无情 提交于 2019-11-28 08:01:14
问题 std::optional<int&> xx; just doesn't compile for the latest gcc-7.0.0 snapshot. Does the C++17 standard include std::optional for references? And why if it doesn't? (The implementation with pointers in a dedicated specialization whould cause no problems i guess.) 回答1: Because optional , as standardized in C++17, does not permit reference types. This was excluded by design. There are two reasons for this. The first is that, structurally speaking, an optional<T&> is equivalent to a T* . They

How to include C++ 17 headers with g++ 6.2.0 with -std=c++17 (optional, any, string_view, variant)

我与影子孤独终老i 提交于 2019-11-28 07:43:08
问题 std::optional is in C++ 17, where it was std::experimental::optional before. I tried compiling a file which included <optional> , with the command: g++ -std=c++17 <filename>.cpp (in the Bash terminal). I get the following error: <filename>.cpp:5:20 fatal error: optional: No such file or directory #include <optional> ^ compilation terminated But I can #include <experimental/optional> just fine. Am I missing some header files? How can I include the optional header? I also can't include <any> ,

construction helper make_XYZ allowing RVO and type deduction even if XZY has noncopy constraint

╄→尐↘猪︶ㄣ 提交于 2019-11-28 07:38:59
UPDATE1: C++17 added type deduction for constructors - which does not imply that the free function is an inferior solution. UPDATE2: C++17 added guaranteed copy elision (the copy does not even take place conceptually). So with C++17 my code actually works and with optimal performance. But Martinho's code using brace initialisation for the return value is still the cleaner solution I believe. But checkout this answer from Barry and the comment from T.C. OLD POST: Type deduction does not work for constructors (at least until and including C++11). The common solution is to rely on RVO (Return

Is auto an optional keyword in ranged based for loops?

泄露秘密 提交于 2019-11-28 07:23:54
问题 I recall someone once telling me, "there is no need for auto inside range-based for loops. It would not be ambiguous in the language if we were to remove it." Is that a true statement? Is the folowing code valid C++ syntax? for (elem : range){...} I had assumed this was already valid syntax, but when I went to compile with clang++ --std=c++1z , I was shown the following error: range-based for loop requires type for loop variable for (elem: range){ The compiler still recognizes this as a range

Constructor conditionally marked explicit

馋奶兔 提交于 2019-11-28 07:14:28
Update: conditional explicit has made it into the C++20 draft. more on cppreference The cppreference std::tuple constructor page has a bunch of C++17 notes saying things like: This constructor is explicit if and only if std::is_convertible<const Ti&, Ti>::value is false for at least one i How can one write a constructor that is conditionally explicit? The first possibility that came to mind was explicit(true) but that's not legal syntax. An attempt with enable_if was unsuccessful: // constructor is explicit if T is not integral struct S { template <typename T, typename = typename std::enable

Can a non-type template parameter be of type “void*”?

怎甘沉沦 提交于 2019-11-28 07:14:11
问题 Yakk - Adam Nevraumont said: Non-type template parameters of type void* are not allowed in at least some versions of the standard. Is this true? If it is true, in which versions of the standard are non-type template parameters of type void* not allowed? (Note: as noted in a comment to answer another comment, this is about non-type template parameters , not template type arguments , which can be any valid type-id per [temp.arg.type], including void* . 回答1: TL;DR Template parameters of type

Use of observer_ptr

こ雲淡風輕ζ 提交于 2019-11-28 07:10:23
What exactly is the point of the construct std::observer_ptr in the library fundamentals technical specification V2? It seems to me that all it does is wrap a bare T* , which seems like a superfluous step if it adds no dynamic memory safety. In all of my code I use std::unique_ptr where I need to take explicit ownership of an object and std::shared_ptr where I can share ownership of an object. This works very well and prevents accidental dereferencing of an already destroyed object. std::observer_ptr makes no guarantee about the lifetime of the object observed, of course. If it were to be

get<string> for variants fail under clang++ but not g++

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:57:06
问题 The following code: variant<string> x = "abc"; cout << get<string>(x) << "\n"; works fine under g++ (version 7.2). However, when compiled under clang++ (version 5.0) using libstdc++, I get the following error in the get method: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../include/c++/7.2.0/variant:238:46: fatal error: cannot cast 'std::variant<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to its private base class 'std::__detail::__variant::