c++17

Are nested structured bindings possible?

房东的猫 提交于 2019-12-01 13:41:43
问题 Assume I have an object of type std::map<std::string, std::tuple<int, float>> data; Is it possible to access the element types in a nested way (i.e. when used in ranged for loop) like this for (auto [str, [my_int, my_float]] : data) /* do something */ 回答1: No, it is not possible. I distinctly remember reading somewhere that nested structured bindings are not allowed for C++17, but they are considering allowing it in a future standard. Can't find the source though. 回答2: No, they aren't

Mapping an integral template parameter value onto a primitive type

假如想象 提交于 2019-12-01 11:57:38
I want to map a number to a type. For this example I'll make a function that maps a sizeof() result onto a signed primitive type. I am wondering if there is a better way of doing what I did below in modern C++, which is to take a templated value and convert it into a type. Right now this works in converting a size to a known type, but I can't seem to find anything in the standard library that does what I want. Have I missed something? If not, is there a better way of doing this or cleaning up this code? For example if somehow in the future we end up having 128 bit types, this wouldn't support

Persist C++ type info to file for use across program invocations

孤街醉人 提交于 2019-12-01 11:55:20
问题 Edit: highlighting the actual question with more context available if desired. I want to implement the following method: template <typename T> <unspecified> type_identification(); For a generic type T, it must return a (relatively) unique identification that is stable over multiple invocations of the same program and may be used for inter-process communication (so no pointer-based solutions). Compiler-specific macros/extensions/intrinsics may be used, preferably available for both MSVC and

Mapping an integral template parameter value onto a primitive type

微笑、不失礼 提交于 2019-12-01 10:29:58
问题 I want to map a number to a type. For this example I'll make a function that maps a sizeof() result onto a signed primitive type. I am wondering if there is a better way of doing what I did below in modern C++, which is to take a templated value and convert it into a type. Right now this works in converting a size to a known type, but I can't seem to find anything in the standard library that does what I want. Have I missed something? If not, is there a better way of doing this or cleaning up

Is there a safe navigation operator for C++?

喜你入骨 提交于 2019-12-01 07:40:15
In Modern C++, is there a way to do safe navigation? For example, instead of doing... if (p && p->q && p->q->r) p->q->r->DoSomething(); ...having a succinct syntax by using some sort of short-circuiting smart pointer, or some other kind of syntax leveraging operator overloading, or something in the Standard C++ Library, or in Boost. p?->q?->r?->DoSomething(); // C++ pseudo-code. Context is C++17 in particular. The best you can do is collapse all the member accesses into one function. This assumes without checking that everything is a pointer: template <class C, class PM, class... PMs> auto

Template argument deduction for class templates in C++17: am I doing it wrong?

我的梦境 提交于 2019-12-01 07:33:33
According to https://gcc.gnu.org/projects/cxx-status.html , version 7 of g++, used with flag -std=c++1z , supports template argument deduction for class templates. I would expect the following code to compile, especially as Base is an abstract class, therefore: 1. the compiler knows no instance of Base can be created; 2. the pointer to base pt_base points to a clearly defined instance (i.e. Derived<int>{42} ) where the type ( int ) is explicit. template<typename ValueType> class Base { public: virtual ValueType getValue() = 0; }; template<typename ValueType> class Derived : public Base

How to filter a std::integer_sequence

耗尽温柔 提交于 2019-12-01 07:00:45
If I theoretically have a sequence of integers like std::integer_sequence<int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9> How can I filter it with some compile-time predicate to get a potentially smaller std::integer_sequence<int, ...> ? For the sake of argument, let's say that I want only the even values, which leads to the question of "How can I make the following static_assert (or something close) pass?" static_assert(std::is_same_v<std::integer_sequence<int, 0, 2, 4, 6, 8>, decltype(FilterEvens(std::integer_sequence<int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9>{}))>, "Integer sequences should be equal"); This

What are the changes, if any, to the memcpy lifetime initalization rules in the new standard?

好久不见. 提交于 2019-12-01 06:53:47
问题 As far as I am aware, memcpy into uninitialized storage cannot safely be used to create an copy of the source object. However, in this thread from last year on the open-std WG21 "ub" list, a participant refers to the new memcpy lifetime-initiation rules : This doesn’t seem to rise to the level of a bug report, but it might be relevant to the new memcpy lifetime-initiation rules. Will they ascribe some static type to the source and destination bytes? Based on the context of the question and

C++17 fold expression in cout

戏子无情 提交于 2019-12-01 05:25:57
I am learning the new c++17 fold expression and I saw this code from c++17 fold expression . I would like to know why this code work : template<typename ...Args> void printer(Args&&... args) { (std::cout << ... << args) << '\n'; } but not this one : template<typename ...Args> void printer(Args&&... args) { (std::cout << args << ...) << '\n'; } which could seems logic too and would reverse the print order in my opinion. As seen on cppreference , binary folds can have the following two forms: Where E is the pack expression and I is the initialization expression . There is no binary fold that

How to filter a std::integer_sequence

我与影子孤独终老i 提交于 2019-12-01 04:06:23
问题 If I theoretically have a sequence of integers like std::integer_sequence<int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9> How can I filter it with some compile-time predicate to get a potentially smaller std::integer_sequence<int, ...> ? For the sake of argument, let's say that I want only the even values, which leads to the question of "How can I make the following static_assert (or something close) pass?" static_assert(std::is_same_v<std::integer_sequence<int, 0, 2, 4, 6, 8>, decltype(FilterEvens(std: