[odb-users] Can I use C++ 17 std::optional members with ODB?
Boris Kolpackov
boris at codesynthesis.com
Thu Jun 16 10:27:43 EDT 2022
Mann, David W (B62) CTR USN NAVSURFWARCEN DAH VA (USA) <david.w.mann5.ctr at us.navy.mil> writes:
> std::optional<double> m_testDouble;
>
> The error was error: unable to map C++ type '::std::optional< double >' used
> in data member 'm_testDouble' to a SQLite database type.
>
> Is there a way I can work around this?
Yes, we should actually add built-in support for this (I will try to
add it before the next 2.5.0 pre-release).
In the meantime, you can place the following specialization into a
header, say, odb-optional-traits.hxx:
------------------------------------------------------------------
#pragma once
#include <optional>
#include <type_traits>
#include <odb/wrapper-traits.hxx>
namespace odb
{
template <typename T>
class wrapper_traits<std::optional<T>>
{
public:
using wrapped_type = T;
using wrapper_type = std::optional<T>;
// T can be const.
//
using unrestricted_wrapped_type = typename std::remove_const<T>::type;
static const bool null_handler = true;
static const bool null_default = true;
static bool
get_null (const wrapper_type& o)
{
return !o;
}
static void
set_null (wrapper_type& o)
{
o = wrapper_type ();
}
static const wrapped_type&
get_ref (const wrapper_type& o)
{
return *o;
}
static unrestricted_wrapped_type&
set_ref (wrapper_type& o)
{
if (!o)
o = unrestricted_wrapped_type ();
return const_cast<unrestricted_wrapped_type&> (*o);
}
};
}
------------------------------------------------------------------
And then add the following two ODB compiler options when compiling
your headers:
--odb-epilogue '#include "odb-optional-traits.hxx"'
--hxx-prologue '#include "odb-optional-traits.hxx"'
More information about the odb-users
mailing list