boost-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

How to disengage std::experimental::optional?

╄→гoц情女王★ 提交于 2020-01-02 04:55:13
问题 With Boost I can create an optional in-place with: boost::optional<boost::asio::io_service::work> work = boost::in_place(boost::ref(io_service)); And disengage it with: work = boost::none; With C++14 / experimental support, I can instead construct an optional in-place with: std::experimental::optional<boost::asio::io_service::work> work; work.emplace(boost::asio::io_service::work(io_service)); But I'm at a loss for how to disengage it... 回答1: work = std::experimental::nullopt; should

How to implement std::optional's copy constructor?

一曲冷凌霜 提交于 2019-12-25 02:33:11
问题 I am implementing std::optional, but have run into a snag with one of its copy constructors. Here is a sketch of my implementation: #include <type_traits> template<typename T> class optional { public: constexpr optional() : m_is_engaged(false) {} constexpr optional(const optional &other) : m_is_engaged(false) { operator=(other); } constexpr optional &operator=(const optional &other) { if(other.m_is_engaged) { return operator=(*other); } else if(m_is_engaged) { // destroy the contained object

Is it possible to move a boost::optional?

核能气质少年 提交于 2019-12-19 05:02:59
问题 I've been trying to define a defaulted move constructor in a class with a boost::optional member variable. #include <boost/optional.hpp> #include <utility> #include <vector> struct bar {std::vector<int> vec;}; struct foo { foo() = default; foo(foo&&) = default; boost::optional<bar> hello; }; int main() { foo a; foo b(std::move(a)); } My compiler supports both move semantics and defaulted move constructors, but I cannot get this to work. % clang++ foo.cc -std=c++11 -stdlib=libc++ foo.cc:15:7:

nlohmann json ambiguous overload for 'operator='

情到浓时终转凉″ 提交于 2019-12-11 05:48:54
问题 I'm getting this compilation error with the following code #include <iostream> #include <boost/optional.hpp> #include "nlohmann_json.hpp" namespace nlohmann { template <typename T> struct adl_serializer<boost::optional<T>> { static void to_json(json& j, const boost::optional<T>& opt) { if (opt == boost::none) j = nullptr; else j = *opt; } static void from_json(const json& j, boost::optional<T>& opt) { if (j.is_null()) opt = boost::none; else opt = j.get<T>(); } }; } int main(int argc, char*

C2143 syntax error when including boost/optional.hpp

痞子三分冷 提交于 2019-12-11 03:53:00
问题 I'm stuck with a compile-time error which I cannot understand. I try to use boost::optional in my code, and as soon as I include boost/optional.hpp I cannot build my project any longer. If I comment this include statement out, it works. I don't even have any actual usage of boost::optional in my code yet, just the include statement in the class header (see full header below). The compiler error is C2143 syntax error: missing ',' before '<' which happens in another Boost header boost/utility

how to divide boost::optional<double>?

烈酒焚心 提交于 2019-12-10 20:41:26
问题 I have such code: boost::optional<double> result = _ind1.Value() / _ind2.Value(); Each arg is boost::optional<double> too: boost::optional<double> Value() { return value; } Errors are: Error 1 error C2676: binary '/' : 'boost::optional<T>' does not define this operator or a conversion to a type acceptable to the predefined operator 2 IntelliSense: no operator "/" matches these operands operand types are: boost::optional<double> / boost::optional<double> I understand that it seems that

boost::optional<T&> vs T*

天大地大妈咪最大 提交于 2019-12-10 12:30:21
问题 I'm trying to understand when is the right time to use some of the structures that come with boost and had a question regarding the use of boost::optional with a reference. Suppose I have the following class, using boost::optional : class MyClass { public: MyClass() {} initialise(Helper& helper) { this->helper = helper; } boost::optional<Helper&> getHelper() { return helper; } private: boost::optional<Helper&> helper; } Why would I use the above instead of: class MyClass { public: MyClass() :

boost::optional not letting me reassign const value types

大兔子大兔子 提交于 2019-12-10 04:32:21
问题 It seems to me there should be four variants of boost::optional optional<Foo> => holds a mutable Foo and can be reassigned after initialization optional<Foo const> const => holds a const Foo and can't be reassigned after initialization optional<Foo> const => (should?) hold a mutable Foo but can't be reassigned after initialization optional<Foo const> => (should?) hold a const Foo and can be reassigned after initialization The first 2 cases work as expected. But the optional<Foo> const