1

私は ODB ORM を試していますが、インターフェイスに固執する必要があるため、const オブジェクトを取得して永続化する必要があります。ODB API が const オブジェクトを永続化できるかどうかはわかりません。一部の部分はそのための準備ができているようですが、機能しません。

ここで gcc からこのエラーを受け取ります。

void OdbReportRW::write_object(const MyObject &my_object)
{
    odb::core::transaction t{db->begin()};
    db->persist(my_object);
    t.commit();
}

これはエラーです。これは、my_object を const にすべきではないと言っていると思います。

In file included from /usr/local/include/odb/database.hxx:632:0,
from odb_report.hpp:21,
from src/vcf/odb_report.cpp:18:
/usr/local/include/odb/database.txx: In instantiation of ‘typename odb::object_traits<T>::id_type odb::database::persist_(T&) [with T = const MyObject; odb::database_id DB = (odb::database_id)5u; typename odb::object_traits<T>::id_type = long unsigned int]’:
/usr/local/include/odb/database.ixx:167:45:   required from ‘typename odb::object_traits<T>::id_type odb::database::persist(const T&) [with T = MyObject; typename odb::object_traits<T>::id_type = long unsigned int]’
src/vcf/odb_report.cpp:134:26:   required from here
/usr/local/include/odb/database.txx:38:39: error: no matching function for call to ‘odb::object_traits_impl<MyObject, (odb::database_id)5u>::persist(odb::database&, const MyObject&)’
object_traits::persist (*this, obj);
^
/usr/local/include/odb/database.txx:38:39: note: candidate is:
In file included from src/vcf/odb_report.cpp:27:0:
my-object-error-odb.hxx:247:5: note: static void odb::access::object_traits_impl<MyObject, (odb::database_id)1u>::persist(odb::database&, odb::access::object_traits<MyObject>::object_type&)
persist (database&, object_type&);
^
my-object-odb.hxx:247:5: note:   no known conversion for argument 2 from ‘const MyObject’ to ‘odb::access::object_traits<MyObject>::object_type& {aka MyObject&}’

clang と同じエラーで、もう少し説明的です:

In file included from src/vcf/odb_report.cpp:18:
In file included from inc/vcf/odb_report.hpp:21:
In file included from /usr/local/include/odb/database.hxx:632:
/usr/local/include/odb/database.txx:38:36: error: binding of reference to type 'object_type' (aka 'MyObject') to a value of type 'const MyObject' drops qualifiers
    object_traits::persist (*this, obj);
                                   ^~~
/usr/local/include/odb/database.ixx:167:12: note: in instantiation of function template specialization 'odb::database::persist_<const MyObject, 5>' requested here
    return persist_<const T, id_common> (obj);
           ^
src/vcf/odb_report.cpp:134:13: note: in instantiation of function template specialization 'odb::database::persist<MyObject>' requested here
        db->persist(my_object);
            ^
inc/vcf/error-odb.hxx:247:37: note: passing argument to parameter here
    persist (database&, object_type&);
                                    ^

ただし、データベース インターフェイス (odb から) で、参照と const 参照 (およびその他のポインター) の両方のタイプの永続化が提供されていることがわかります。

// in odb/database.hxx
template <typename T>
typename object_traits<T>::id_type
persist (T& object);

template <typename T>
typename object_traits<T>::id_type
persist (const T& object);

クラスに永続化するデフォルトのコンストラクター(ここでは MyObject )がない場合、同様のエラーが発生することがわかりました( for find、 not persist)が、そこにあるため、問題ではありません。findデフォルトのコンストラクターは、生成されたコードで追加のメソッドのみを生成することを既に確認しました。

私のメソッドでconst指定子を削除するとうまくwrite_objectいきましたが、私が言ったように、インターフェイスに固執する必要があります。

const オブジェクトを永続化するためのアイデアはありますか?

4

1 に答える 1

1

私がこれを見つけたドキュメントをより徹底的に読んでください(http://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.8):

最初の persist() 関数は、永続化されているインスタンスへの定数参照を想定しています。2 番目の関数は、定数オブジェクト ポインターを想定しています。これらの関数は両方とも、アプリケーションによって割り当てられたオブジェクト ID (セクション 14.4.2、「auto」) を持つオブジェクトでのみ使用できます。

実際、autoデータベースで処理される ID の指定子を使用していました。

// class MyObject    
#pragma db id auto
unsigned long id_;

そのため、auto id と const 参照を同時に使用することはできないようです。

于 2016-07-04T12:04:47.297 に答える