stdoptional

std::optional<T> call by reference

旧街凉风 提交于 2021-01-29 07:11:36
问题 I am implementing the following function: bool DenStream::_try_merge(const double sample, const double weight, const std::optional<MicroCluster>& micro_cluster) const { if (micro_cluster) { MicroCluster micro_cluster_copy(*micro_cluster); micro_cluster_copy.insert_sample(sample, weight); if (micro_cluster_copy.get_radius() <= this->eps) { micro_cluster.insert_sample(sample, weight); // THIS DOES NOT WORK return true; } } return false; }; The compiler says error: 'const class std::optional'

Is '_HAS_CXX17' marco usable in custom project headers to enable C++17 language set features?

老子叫甜甜 提交于 2020-12-05 07:32:43
问题 I want to create headers which use 'optional' from standard C++. However, My headers will be referred from Visual Studio 2015 as well as Visual Studio 2017 projects. I would like to have something, such that for Visual Studio 2017 ( with C++ 17 lang feature set) , std::optional is used and with Visual Studio 2015, boost::optional gets used. I am thinking of something like this: #include <yvals.h> #if _HAS_CXX17 #include <optional> template <typename T> using Optional = std::optional<T>; #else

Is '_HAS_CXX17' marco usable in custom project headers to enable C++17 language set features?

為{幸葍}努か 提交于 2020-12-05 07:32:09
问题 I want to create headers which use 'optional' from standard C++. However, My headers will be referred from Visual Studio 2015 as well as Visual Studio 2017 projects. I would like to have something, such that for Visual Studio 2017 ( with C++ 17 lang feature set) , std::optional is used and with Visual Studio 2015, boost::optional gets used. I am thinking of something like this: #include <yvals.h> #if _HAS_CXX17 #include <optional> template <typename T> using Optional = std::optional<T>; #else

clang 5: std::optional instantiation screws std::is_constructible trait of the parameter type

本小妞迷上赌 提交于 2020-08-22 08:18:47
问题 A really strange and unexpected behaviour of clang 5 was detected when switching to c++17 and replacing custom std::optional solution with the standard one. For some reason, emplace() was being disabled due to faulty evaluation of a std::is_constructible trait of the parameter class. Some specific preconditions must be satisfied before it reproduces: #include <optional> /// Precondition #1: T must be a nested struct struct Foo { struct Victim { /// Precondition #2: T must have an aggregate

What's the advantage of `std::optional` over `std::shared_ptr` and `std::unique_ptr`?

*爱你&永不变心* 提交于 2020-06-25 01:28:10
问题 The reasoning of std::optional is made by saying that it may or may not contain a value. Hence, it saves us the effort of constructing a, probably, big object, if we don't need it. For example, a factory here, will not construt the object if some condition is not met: #include <string> #include <iostream> #include <optional> std::optional<std::string> create(bool b) { if(b) return "Godzilla"; //string is constructed else return {}; //no construction of the string required } But then how is

std optional: No such file or directory

混江龙づ霸主 提交于 2020-05-14 17:35:11
问题 I tried to compile the following program with different compilers (including gcc 6.1) : #include <optional> int main() { std::optional<int> o1; } Output is main.cpp:1:20: fatal error: optional: No such file or directory #include optional This is even true for the examples given here: http://en.cppreference.com/w/cpp/utility/optional/optional Any clues why? 回答1: std::optional will be part of the C++17 standard, but if you want to use before then you will have to use std::experimental::optional

Why std::optional::value() &&; return &&?

喜你入骨 提交于 2019-12-23 22:25:35
问题 I have had runtime error, when replaced some code by using std::optional: Old code: T getValue(); ... const auto& value = getValue(); value.get(); New code: std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash It was unpredictable for me. The reason of crash is that the method returns T&& . My question is in what cases T&& can be useful, why the method does not return a T . Complete code: #include <experimental/optional> #include

std::optional implemented as union vs char[]/aligned_storage

时间秒杀一切 提交于 2019-12-09 09:46:24
问题 While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template <typename T> class optional { // ... private: bool has_value_; aligned_storage<T, /* ... */> storage_; } But then both libstdc++ and libc++ (and Abseil ) implement their optional types like this: template <typename T> class optional { // ... private: struct empty_byte {}; union { empty_byte empty_; T value_; }; bool has_value_; } They look to

C++17 std::optional error: expected primary-expression before 'auto'

柔情痞子 提交于 2019-11-27 08:51:08
问题 I was experimenting with C++17 feature std::optional The optional return type is std::optional<std::pair<int, int>> . I call the sum_pair function in print_answer function and wanted a optional print. In print_answer function I wanted to check whether the required pair holds something to show. like in the example given in: optional-returning factory functions are usable as conditions of while and if Following is the code: here is it live with error #include <iostream> #include <vector>