c++17

Alternative id generators for types

孤人 提交于 2019-12-04 16:29:28
问题 In a project of mine, I have an ID generator for types that looks similar to this: class Family { static std::size_t identifier; template<typename...> static std::size_t family() { static const std::size_t value = identifier++; return value; } public: template<typename... Type> inline static std::size_t type() { return family<std::decay_t<Type>...>(); } }; std::size_t Family::identifier{}; Usage: const auto id = Family::type<FooBar>(); It works just fine for my purposes, but it has some

How do I use the new C++17 execution policies? [duplicate]

倖福魔咒の 提交于 2019-12-04 16:18:48
问题 This question already has answers here : Are C++17 Parallel Algorithms implemented already? (4 answers) Closed last year . I was reading through the std::algorithm documentation at cppreference.com and I noticed a C++17 tag on a lot of cool things I haven't used yet. What got my attention most was the new execution policies. What I gathered from reading about them is that I can make any for_each loop I want multi-threaded just by specifying an execution policy. For example, I have a program

(v) is actually (*&v) since when?

ぃ、小莉子 提交于 2019-12-04 16:09:10
问题 Could C++ standards gurus please enlighten me: Since which C++ standard version has this statement failed because (v) seems to be equivalent to (*&v) ? I.e. for example the code: #define DEC(V) ( ((V)>0)? ((V)-=1) : 0 ) ...{... register int v=1; int r = DEC(v) ; ...}... This now produces warnings under -std=c++17 like: cannot take address of register variable left hand side of operand must be lvalue Many C macros enclose ALL macro parameters in parentheses, of which the above is meant only to

Stripping all qualifiers from a function type

左心房为你撑大大i 提交于 2019-12-04 16:01:28
问题 Given a possibly varargs function type with possibly a cv-qualifier-seq and possibly a ref-qualifier , is it possible to write a type trait that strips all the qualifiers without writing 4 * 3 * 2 = 24 partial specializations? template<class T> struct strip_function_qualifiers; template<class R, class... Args> struct strip_function_qualifiers<R(Args...)> { using type = R(Args...); }; template<class R, class... Args> struct strip_function_qualifiers<R(Args..., ...)> { using type = R(Args..., .

Multiple SFINAE class template specialisations using void_t

笑着哭i 提交于 2019-12-04 15:39:13
问题 Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member typedef called "type". Here, a single specialisation is employed. This could be extended to identify say whether a type has either a member typedef called "type1", or one called "type2". The C++1z code below compiles with GCC, but not Clang. Is it

Equivalent ternary operator for constexpr if?

孤者浪人 提交于 2019-12-04 15:15:37
问题 Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if? template<typename Mode> class BusAddress { public: explicit constexpr BusAddress(Address device) : mAddress(Mode::write ? (device.mDevice << 1) : (device.mDevice << 1) | 0x01) {} private: uint8_t mAddress = 0; }; 回答1: No, there is no constexepr conditional operator. But you could wrap the whole thing in a lambda and immediately evaluate it (an IIFE): template

Fold expressions with arbitrary callable?

守給你的承諾、 提交于 2019-12-04 15:02:15
问题 Looking over the C++17 paper on folds, (and on cppreference), I'm confused as to why the choice was made to only work with operators? At first glance it seems like it would make it easier to expand (... + args) by just shoving a + token between the elements of args , but I'm unconvinced this is a great decision. Why can't a binary lambda expression work just as well and follow the same expansion as the latter above? It's jarring to me that a fold syntax would be added to a language without

How to define a variant<x,y,z> extracting subtypes of a template parameter

笑着哭i 提交于 2019-12-04 13:21:16
I am building a state-machine where state transitions are described as a variant, i.e.: using table = std::variant< /* state event followup-state */ transition<start, success<sock>, connecting>, transition<start, exception, failed>, transition<connecting, success<>, connected>, transition<connecting, exception, failed>, transition<connected, exception, failed> >; and transition being a simple type: template <typename ENTRY_STATE, typename EVENT, typename NEXT_STATE> struct transition { using entry_state = ENTRY_STATE; using event = EVENT; using next_state = NEXT_STATE; }; The state classes are

Perfect forwarding with class template argument deduction

孤街浪徒 提交于 2019-12-04 12:26:22
问题 I would like to understand how deductions guides work with universal references and std::forward , in particular to create perfectly forwarding wrappers. The code below provides a code to experiment with a functor wrapper in two cases: one with an implicit deduction guide and one with an explicit deduction guide. I have put a lot of && and std::forward in comments, because I do not know where they are needed to achieve perfect forwarding. I would like to know where to put them, and where they

Constexpr find for array using c++17

守給你的承諾、 提交于 2019-12-04 11:59:47
I am trying to write a constexpr find function that will return the index of a std::array containing a certain value. The function below seems to work OK except when the contained type is const char* : #include <array> constexpr auto name1() { return "name1"; } constexpr auto name2() { return "name2"; } template <class X, class V> constexpr auto find(X& x, V key) { std::size_t i = 0; while(i < x.size()) { if(x[i] == key) return i; ++i; } return i; } int main() { constexpr std::array<const char*, 2> x{{name1(), name2()}}; constexpr auto f1 = find(x, name1()); // this compiles constexpr auto f2