c++17

Is it possible to extract parameter types from a template <auto MEMFN>?

好久不见. 提交于 2019-12-13 08:46:26
问题 Is it possible to create a standalone template function which has a template parameter auto MEMFN (a member function pointer), and has the same return and parameter types as MEMFN has? So, if MEMFN's type is RETURN (OBJECT::*)(PARAMETERS...) then the desired function is this: template <auto MEMFN> RETURN foo(OBJECT &, PARAMETERS...); My problem is how to extract PARAMETERS... from MEMFN's type ( RETURN and OBJECT are easy to do). So I can call this function like this: Object o; foo<&Object:

How to build boost::asio::experimental

半腔热情 提交于 2019-12-13 03:58:10
问题 I want to build an example from the page: https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/example/cpp17/coroutines_ts/chat_server.cpp G++ version: 7.3 Boost version 1.69 #include <cstdlib> #include <deque> #include <iostream> #include <list> #include <memory> #include <set> #include <string> #include <utility> #include <boost/asio/experimental.hpp> #include <boost/asio/io_context.hpp> #include <boost/asio/ip/tcp.hpp> #include <boost/asio/read_until.hpp> #include <boost/asio/signal

Flattening a pack of types, where non-type values are part of the flattening

╄→尐↘猪︶ㄣ 提交于 2019-12-13 02:59:47
问题 If you look at the member type of template <typename Pack> struct flatten; template <typename T, typename U, std::size_t A, std::size_t B, std::size_t C, typename V, std::size_t D, std::size_t E, typename W> struct flatten<std::tuple<T,U, std::index_sequence<A,B,C>, V, std::index_sequence<D,E>, W>> { template <typename, typename, std::size_t, std::size_t, std::size_t, typename, std::size_t, std::size_t, typename> struct S; using type = S<T,U,A,B,C,V,D,E,W>; }; is it possible to do this

Is there a transparent way of using unique_ptr in std containers?

半城伤御伤魂 提交于 2019-12-12 20:38:03
问题 Is there a transparent way of using std::unique_ptr in containers? #include <iostream> #include <memory> #include <map> struct method { virtual ~method() { std::cout << "f\n"; }; }; typedef std::unique_ptr<method> MPTR; std::map<int, MPTR> tbl; void insert(int id, method *m) { tbl.insert({id,std::unique_ptr<method>(m)}); }; void set(int id, method *m) { tbl[id] = std::unique_ptr<method>(m); }; int main(int argc, char **argv) { insert(1,new method()); set(1,new method()); return 0; } I'd like

Gcc check whether the given class has operator+

◇◆丶佛笑我妖孽 提交于 2019-12-12 20:06:48
问题 I created a code below to test whether there is an operator+ overload for 2 classes: template<typename T, typename U> struct _has_plus_hlp { template<typename X, typename Y> static std::true_type _check(X&, Y&, decltype(std::declval<X>() + std::declval<Y>()) = {}); static std::false_type _check(...); using type = decltype(_check(std::declval<T>(), std::declval<U>())); }; template<typename X, typename Y> constexpr bool has_plus_v = _has_plus_hlp<X, Y>::type::value; int main() { std::cout <<

How do correctly use a callable passed through forwarding reference?

落爺英雄遲暮 提交于 2019-12-12 17:13:46
问题 I'm used to pass lambda functions (and other callables) to template functions -- and use them -- as follows template <typename F> auto foo (F && f) { // ... auto x = std::forward<F>(f)(/* some arguments */); // ... } I mean: I'm used to pass them through a forwarding reference and call them passing through std::forward . Another Stack Overflow user argue (see comments to this answer) that this, calling the functional two or more time, it's dangerous because it's semantically invalid and

Passing each element of a parsed sequence to a function that returns a rule's attribute type

牧云@^-^@ 提交于 2019-12-12 14:16:31
问题 I want to parse CSS color functions (for simplicity, all of the arguments are numbers between 0 and 255) rgb(r,g,b) rgba(r,g,b,a) hsl(h,s,l) hsla(h,s,l,a) into struct color { color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) : red{r}, green{g}, blue{b}, alpha{a} {} static color hsl(std::uint8_t h, std::uint8_t s, std::uint8_t l, std::uint8_t a) { ... } std::uint8_t red; std::uint8_t green; std::uint8_t blue; std::uint8_t alpha; } I have a working hsl function

Why is a convenience helper for the erase-remove-idiom not provided by the standard?

廉价感情. 提交于 2019-12-12 11:40:50
问题 Removing items from a collection in the STL requires a technique used so often that is has become an idiom: the erase-remove-idiom One of the most common usages of this idiom is to remove an item of type T from a vector<T> std::vector<Widget> widget_collection; Widget widget; widget_collection.erase( std::remove(widget_collection.begin(), widget_collection.end(), widget), widget_collection.end()); This is obviously very verbose, and violates the DRY principle - the vector in question is

Why is `std::invoke` not constexpr?

只愿长相守 提交于 2019-12-12 10:34:23
问题 Shouldn't std::invoke be constexpr especially after constexpr lambdas in C++17? Are there any obstacles that will prevent this? 回答1: Update: P1065 will make it constexpr . Keep original post for historical reason: From the proposal: Although there is possibility to implement standard conforming invoke function template as a constexpr function, the proposed wording does not require such implementation. The main reason is to left it consistent with existing standard function objects, that could

C++ standard: ODR and constexpr std::string_view

时光怂恿深爱的人放手 提交于 2019-12-12 09:55:16
问题 If I have a header foo.h which contains #ifndef FOO_H_ #define FOO_H_ namespace foo { constexpr std::string_view kSomeString = "blah"; } #endif // FOO_H_ then is it safe to include foo.h from within multiple .cc files in a single program, regardless of what they do with the symbol kSomeString , or are there some uses that could cause an ODR violation? Also, is it guaranteed that kSomeString.data() will return the same pointer across .cc files? I'd like specific references to wording in the C+