c++17

Iterating regex submatches represented as std::basic_string_view

只谈情不闲聊 提交于 2020-01-15 10:32:14
问题 Is there a direct efficient way to convert std::sub_match to std::basic_string_view (without constructing an intermediate std::basic_string and without intermediate heap allocation)? Or one abstraction level further, is there an alternative to std::regex_token_iterator for iterating regex submatches represented as std::basic_string_view instead of std::sub_match using the std (C++17)? The reasons why I rather like to use std::basic_string_view over std::sub_match are: std::basic_string_view

Modern C++ way to repeat code for set number of times

十年热恋 提交于 2020-01-15 03:24:11
问题 Very simply, is there a simpler way to repeat a block for a certain number of times, where the block inside does not need the counter variable? The trivial solution is of course for (int i = 0; i < repetitions; ++i) { //do your thing, i is not used here } However, now that we have ranged for, standard algorithms and other fancy constructs for iterating over containers, in comparison this is actually starting to feel like a lot of boilerplate and details for what should be an even simpler case

Parameter induction with std::variant

ⅰ亾dé卋堺 提交于 2020-01-15 03:15:09
问题 Recenty I am working on an ORM which accepts registration of functions by doing the following: orm->register_func("NAME", &User::set_name); So basically when the database returns the column NAME , the ORM will use the function set_name in User . During the development I learned more about std::variant and here is a little example of how I use it: template<typename RET, typename T, typename ...Args> using FPTR = RET(T::*)(Args...); template<typename T> using VARIANT_FPTR = std::variant<FPTR

Is there any practical use for ContiguousIterator?

笑着哭i 提交于 2020-01-14 22:46:08
问题 C++17 introduced ContiguousIterator, but there's no corresponding contiguous_iterator_tag. Is there any practical use for ContiguousIterator if it can't be checked/enforced via std::iterator_traits? This question is different from contiguous iterator detection as it's not "why it doesn't have a tag", but "how it can be used if it doesn't have a tag". 回答1: Named requirements in C++17 are first and foremost notation. They only correlate to detectable things in the language if they impose

Constexpr member function

大城市里の小女人 提交于 2020-01-14 10:28:08
问题 Suppose I have a struct template S that is parametrized by an engine: template<class Engine> struct S; I have two engines: a "static" one with a constexpr member function size() , and a "dynamic" one with a non- constexpr member function size() : struct Static_engine { static constexpr std::size_t size() { return 11; } }; struct Dynamic_engine { std::size_t size() const { return size_; } std::size_t size_ = 22; }; I want to define size() member function in S that can be used as a constexpr if

Reference initialization and constant expressions

牧云@^-^@ 提交于 2020-01-14 08:13:29
问题 As a followup to this question, gcc and clang both consider this program ill-formed: int main() { const int& ri = 0; constexpr int i = ri; } The error is about the value of ri being unusable in a constant expression. 0 is certainly a core constant expression, and as a prvalue core constant expression seems to satisfy these constraints (trivially, since int isn't of class, pointer, or array type). So shouldn't ri satisfy this criteria? The same is true if I use a prvalue literal of class type:

How does guaranteed copy elision work in list-initialization in C++1z?

与世无争的帅哥 提交于 2020-01-14 07:27:14
问题 There is a paragraph about guaranteed copy elision in c++ draft n4606 [dcl.init] 17.6: If the destination type is a (possibly cv-qualified) class type: If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object. [ Example : T x = T(T(T())); calls the T default constructor to initialize x . — end example ] [...] There is also a Q&A talks

Can you use a subexpression within fold expressions?

可紊 提交于 2020-01-14 07:26:08
问题 Is the following a legal fold expression? template <std::size_t N, std::size_t... Ix> bool in_range(std::index_sequence<Ix...>) { return ((Ix < N) && ...); } It compiles with clang but not gcc 回答1: Clang is doing the correct thing, the grammar from the Folding expressions Proposal is as follows: fold-expression: ( cast-expression fold-operator ... ) ( ... fold-operator cast-expression ) ( cast-expression fold-operator ... fold-operator cast-expression ) and contains the following wording that

C++ Shorten subsequent function calls

六月ゝ 毕业季﹏ 提交于 2020-01-13 17:56:40
问题 I try to find a way in C++17 to shorten following function calls: GetCurrentContext()->GetEntityManager()->CreateEntity(); maybe to something like: EM()->CreateEntity(); I tried a function pointer, but this is only valid for static functions: constexpr auto &EM = GetContext()->GetEntityManager; // error: reference to non-static member function must be called; Is there a better solution than using a Macro? #define EM GetContext()->GetEntityManager When using an inline function call, i'm afraid

Switch in constexpr function

血红的双手。 提交于 2020-01-13 12:16:12
问题 Found the following statement in Wiki: C++11 introduced the concept of a constexpr-declared function; a function which could be executed at compile time. Their return values could be consumed by operations that require constant expressions, such as an integer template argument. However, C++11 constexpr functions could only contain a single expression that is returned (as well as static_asserts and a small number of other declarations). C++14 relaxes these restrictions. Constexpr-declared