[odb-users] reuse inheritance with templated base class

Boris Kolpackov boris at codesynthesis.com
Wed Aug 6 05:23:02 EDT 2014


Hi Marcel,

Marcel Nehring <mne at qosmotec.com> writes:

> #pragma db object abstract optimistic
> struct base_data
> {
> #pragma db id auto
>     unsigned long id;
> 
> #pragma db version
>     unsigned long version;
> };
> 
> template <typename T>
> struct base : public base_data
> {
>     T x;
> };
> 
> typedef base<std::string> base_derived;
> #pragma db object(base_derived)
> //#pragma db member(base_derived::id) id auto //uncomment to resolve error
> //#pragma db member(base_derived::version) version //this is not needed

What happens here is quite interesting: Since base_derived is not actually
used (e.g., derived from), the C++ compiler does not instantiate the
template. In other words, from the C++ compiler's point of view, the
above typedef is semantically equivalent to a forward declaration:

class base_derived;

As a result, when ODB gets a chance to look at the AST, it doesn't "see"
that base_derived actually inherits from base_data which in turn defines
the object id.

A workaround for this problem would be to explicitly instantiate the
otherwise unused templates for the ODB compiler:

typedef base<std::string> base_derived;
#pragma db object(base_derived)

#ifdef ODB_COMPILER
  template struct base<std::string>;
#endif

I am also trying to see if there an automatic way to do this during
the ODB compilation. Will let you know what I find.

Boris



More information about the odb-users mailing list