[Non-DoD Source] Re: [odb-users] Can I use C++ 17 std::optional members with ODB?

Mann, David W (B62) CTR USN NAVSURFWARCEN DAH VA (USA) david.w.mann5.ctr at us.navy.mil
Thu Jun 16 10:59:25 EDT 2022


Thank you!  I will give that a try and let you know if I need any more info.

V/R

David

-----Original Message-----
From: Boris Kolpackov <boris at codesynthesis.com> 
Sent: Thursday, June 16, 2022 10:28 AM
To: Mann, David W (B62) CTR USN NAVSURFWARCEN DAH VA (USA)
<david.w.mann5.ctr at us.navy.mil>
Cc: odb-users at codesynthesis.com
Subject: [Non-DoD Source] Re: [odb-users] Can I use C++ 17 std::optional
members with ODB?

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"'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 6698 bytes
Desc: not available
Url : https://codesynthesis.com/pipermail/odb-users/attachments/20220616/c4cf162f/smime.bin


More information about the odb-users mailing list