[odb-users] Templated view class

dieter.govaerts at bricsys.com dieter.govaerts at bricsys.com
Thu Jul 2 08:15:54 EDT 2015


Hello,

If I define my view class as follows:

#pragma db view object(Composition)
struct CompositionNameView
{
    String name;
};

it works as expected in the function:

template<>
String getName<Composition>(odb::database *db, unsigned int id)
{
    typedef odb::query< CompositionNameView > Query;
    odb::result< CompositionNameView > r(db->query< CompositionNameView >(Query::id == id));
    CompositionNameView nameView;
    if (!r.empty())
        r.begin().load(nameView);
    return nameView.name;
}

If I rewrite the view class in a templated form, that is equivalent in my opinion, as:

template<class T>
struct ObjectNameView
{
    String name;
};

typedef ObjectNameView<Composition> CompositionNameView;
#pragma db view(CompositionNameView) object(Composition)

then the odb compiler succeeds but the (visual) c++ compiler fails:

1> libodb\include\odb/query.hxx(105): error C2504: 'odb::query_selector_impl<T,DB,kind>' : base class undefined
1>          with
1>          [
1>              T=bim_lib::CompositionNameView,
1>              DB=id_common,
1>              kind=class_other
1>          ]
1> ...
1> libodb\include\odb/result.hxx(76): error C2504: 'odb::result_base<T,kind>' : base class undefined
1>          with
1>          [
1>              T=bim_lib::CompositionNameView,
1>              kind=class_other
1>          ]

The point is of course to generalize the getName function so I don't have to write it for each object type:

template<class T>
String getName<Composition>(odb::database *db, unsigned int id)
{
    typedef odb::query< ObjectNameView<T> > Query;
    odb::result< ObjectNameView<T> > r(db->query< ObjectNameView<T> >(Query::id == id));
    ObjectNameView<T> nameView;
    if (!r.empty())
        r.begin().load(nameView);
    return nameView.name;
}

Can I use templated view classes and how?
Thanks in advance.

Dieter Govaerts





More information about the odb-users mailing list