c++17

C++17 optional tree, error: invalid use of incomplete type

一笑奈何 提交于 2021-02-16 15:15:36
问题 When I compile a binary tree containing optional types: #include <optional> class BinaryTree { public: BinaryTree(); int value; std::optional<BinaryTree> left,right; }; int main() { return 0; } via g++ -std=c++17 -Wfatal-errors main.cpp I face with this error In file included from /usr/include/c++/7/bits/move.h:54:0, from /usr/include/c++/7/bits/stl_pair.h:59, from /usr/include/c++/7/utility:70, from /usr/include/c++/7/optional:36, from main.cpp:1: /usr/include/c++/7/type_traits: In

c++ how to elegantly use c++17 parallel execution with for loop that counts an integer?

泄露秘密 提交于 2021-02-16 10:48:49
问题 I can do std::vector<int> a; a.reserve(1000); for(int i=0; i<1000; i++) a.push_back(i); std::for_each(std::execution::par_unseq, std::begin(a), std::end(a), [&](int i) { ... do something based on i ... }); but is there a more elegant way of creating a parallelized version of for(int i=0; i<n; i++) that does not require me to first fill a vector with ascending ints? 回答1: You could use std::generate to create a vector {0, 1, ..., 999} std::vector<int> v(1000); std::generate(v.begin(), v.end(),

c++ how to elegantly use c++17 parallel execution with for loop that counts an integer?

回眸只為那壹抹淺笑 提交于 2021-02-16 10:48:23
问题 I can do std::vector<int> a; a.reserve(1000); for(int i=0; i<1000; i++) a.push_back(i); std::for_each(std::execution::par_unseq, std::begin(a), std::end(a), [&](int i) { ... do something based on i ... }); but is there a more elegant way of creating a parallelized version of for(int i=0; i<n; i++) that does not require me to first fill a vector with ascending ints? 回答1: You could use std::generate to create a vector {0, 1, ..., 999} std::vector<int> v(1000); std::generate(v.begin(), v.end(),

Why can I evaluate a function receiving a std::pair at compile-time, but not assert it? [duplicate]

依然范特西╮ 提交于 2021-02-11 14:13:59
问题 This question already has answers here : constexpr function parameters as template arguments (4 answers) C++11 constexpr function pass parameter (3 answers) Closed 12 months ago . I wrote a function that receives a variadic number of std::pairs. It takes the pairs, subtracts the 2nd element against the 1st element of each pair, and returns a tuple of the newly generated values like this: #include <tuple> #include <utility> template<typename... pairs> inline constexpr auto foo(pairs&& ...p)

Inheriting from a template class set of operators using CRTP

社会主义新天地 提交于 2021-02-11 12:40:35
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

Inheriting from a template class set of operators using CRTP

家住魔仙堡 提交于 2021-02-11 12:39:12
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

Webdriverxx won't start browser

时光总嘲笑我的痴心妄想 提交于 2021-02-11 12:33:37
问题 So i just got setup with webdriver++ - https://github.com/durdyev/webdriverxx and the I just ran the sample code to see if it works. For some reason it is not and i have no idea what is happening or how to fix it. #include <webdriverxx.h> #include <webdriverxx/browsers/chrome.h> WebDriver browser = Start(Chrome()); browser .Navigate("https://google.com") .FindElement(ByCss("input[name=q]")) .SendKeys("Hello, world!") .Submit(); and it calls a break point on WEBDRIVERXX_FUNCTION_CONTEXT_END()

Send type into function using templates

北战南征 提交于 2021-02-11 06:29:31
问题 I have this function for finding key in map and return the right value with type: template< typename T > T Foo::findInMap(std::string key) { std::map<std::string, std::any>::iterator it = options.find(key); if (it != options.end()) { return std::any_cast<T>(it->second); } return nullptr; } the map looks like this: std::map<std::string, std::any> options; How can I call and modify the function to get the best out of C++17? 回答1: Returning a nullptr is not always possible. Modern C++ way of

How do I make a functor out of an arbitrary function?

为君一笑 提交于 2021-02-10 16:21:16
问题 I have a bunch of functions which I want to use as functors (that is, use types instead of pass pointers to the functions, or any other kind of data). Is there an elegant/idiomatic/standard way of doing this with the standard library, or the standard library + Boost? Perhaps using bind() somehow? Or should I go with something simplistic (well, kind of simplistic) such as: template<typename Function, Function& F, typename... Parameters> struct functor { using function_type = Function; using

Does the following code structure or design implementation have an Idiomatic Name?

女生的网名这么多〃 提交于 2021-02-10 05:33:30
问题 Within the C++ language, many will come across various design patterns that have a designated name in which we would call an Idiom, such as SFINAE , RAII , CRTP , Polymorphism , etc... Consider this following code snippet in its most basic form... template<typename T> auto make_object = [](T val) { class Foo { public: Foo(T val) : value_{val} {} T value() const { return value_; } private: T value_; }; Foo foo(val); return foo; }; // in some other code block, scope, or translation int main() {