[odb-users] Issue with std::weak_ptr in Bidirectional Relationship with ODB Classes

xuewen wang xuewen.ok at gmail.com
Thu Aug 8 09:24:13 EDT 2024


Dear ODB Users,

I am encountering an issue when using  `std::weak_ptr`  to declare a
bidirectional relationship between two ODB classes. Here are the details:

1. According to the libodb manual,  `std::weak_ptr`  can be used to declare
a bidirectional relationship between two ODB classes, where one class uses
 `std::shared_ptr`  and the other uses  `std::weak_ptr` .
2. In all official examples,  `lazy_weak_ptr`  is predominantly used.
3. I have successfully compiled code using  `lazy_weak_ptr`  with forward
declarations of the class members.
4. However, when I try to use  `std::weak_ptr` , the compilation fails. It
seems that  `std::weak_ptr`  requires the template parameter to have a
complete definition, otherwise it results in a compilation error.

The compilation error I am encountering is as follows:
====================[ Build | result_not_cache_bug | Debug
]====================
/opt/JetBrains/Toolbox/apps/CLion/ch-0/233.13135.93/bin/cmake/linux/x64/bin/cmake
--build /path/to/project --target result_not_cache_bug -j 10
[1/6] Generating /path/to/project/IrradianceTemperatureDb-odb.h,
/path/to/project/IrradianceTemperatureDb-odb.cpp,
/path/to/project/IrradianceTemperatureDb-odb.ixx.h
FAILED: /path/to/project/IrradianceTemperatureDb-odb.h
/path/to/project/IrradianceTemperatureDb-odb.cpp
/path/to/project/IrradianceTemperatureDb-odb.ixx.h
cd /path/to/project/cmake-build-debug && /usr/bin/odb --database sqlite
--generate-query --generate-schema --generate-session --std c++11
--hxx-suffix .h --cxx-suffix .cpp --ixx-suffix .ixx.h --output-dir
/path/to/project -I /path/to/project --default-pointer std::shared_ptr
/path/to/project/IrradianceTemperatureDb.h
In file included from /usr/include/c++/10/bits/move.h:57,
                 from /usr/include/c++/10/bits/stl_pair.h:59,
                 from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/bits/char_traits.h:39,
                 from /usr/include/c++/10/string:40,
                 from <standard-odb-prologue>:7:
/usr/include/c++/10/type_traits: In instantiation of ‘struct
std::is_constructible<std::__weak_ptr<database::IrradianceProfileDb,
__gnu_cxx::_S_atomic>, const
std::weak_ptr<database::IrradianceProfileDb>&>’:
/usr/include/c++/10/bits/shared_ptr.h:688:8:   required by substitution of
‘template<class _Tp> template<class _Arg> using _Constructible = typename
std::enable_if<std::is_constructible<std::__weak_ptr<_Tp>,
_Arg>::value>::type [with _Arg = const
std::weak_ptr<database::IrradianceProfileDb>&; _Tp =
database::IrradianceProfileDb]’
/usr/include/c++/10/bits/shared_ptr.h:707:30:   required by substitution of
‘template<class _Yp, class>
std::weak_ptr<database::IrradianceProfileDb>::weak_ptr(const
std::weak_ptr<_Tp>&) [with _Yp = database::IrradianceProfileDb;
<template-parameter-1-2> = <missing>]’
/usr/include/odb/tr1/pointer-traits.hxx:116:1:   required from here
/usr/include/c++/10/type_traits:909:52: error: non-constant condition for
static assertion
  909 |
static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
      |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/type_traits:909: confused by earlier errors, bailing out
My development environment is as follows:
- OS: Ubuntu 22.04.4 LTS
- CMake version: 3.22.1
- Ninja version: 1.10.1
- GCC version: 11.4.0
- ODB version: 2.4.0

Could you please advise if  `std::weak_ptr`  can be used to declare ODB
classes with bidirectional relationships and how to avoid the above
compilation error?

Thank you so much for your help.
Best regards...
---- Sean Wang (Xuewen Wang), Beijing, China.


More information about the odb-users mailing list