[odb-users] PRAGMA_DB in preprocessor define

Zhao Xiang xiang.zhao at gamegou.com
Thu Nov 5 04:42:04 EST 2015

Hi Boris,

Thanks for your help, your marco works like a charm.

I tried do find a "#pragma db column" that does not depend on position, but failed due to unfamiliar with the manual.

Zhao Xiang
------------------ Original ------------------
From:  "Boris Kolpackov"<boris at codesynthesis.com>;
Date:  Thu, Nov 5, 2015 05:21 PM
To:  "Zhao Xiang"<xiang.zhao at gamegou.com>; 
Cc:  "odb-users"<odb-users at codesynthesis.com>; 
Subject:  Re: [odb-users] PRAGMA_DB in preprocessor define

Hi Zhao,

Zhao Xiang <xiang.zhao at gamegou.com> writes:

> I'm trying to put a "#pragma db column" in a preprocessor macro [...]
> [...]
> However the odb compiler gives me two errors:

Yes this is a limitation/bug in the ODB compiler. The "position 
pragmas" (i.e., those that apply to the declaration that follow
them) are implemented using source locations. For macros we happen
to use the macro expansion point as the location of the pragmas
inside the macro. And fixing this properly won't be simple. On
the other hand, there is a pretty simple workaround. It makes the
macro slightly more verbose but is probably not a big deal in this

#include <odb/core.hxx>

#define ODB_COUNT_EXISTS(obj, key)                     \
        PRAGMA_DB(view object(obj))                    \
        struct obj##_count                             \
        {                                              \
          PRAGMA_DB(column("count(" + obj::key + ")")) \
          int count;                                   \

#define ODB_COUNT_EXISTS(obj, key)                     \
        struct obj##_count                             \
        {                                              \
          int count;                                   \
        };                                             \
        PRAGMA_DB(view(obj##_count) object(obj))       \
        PRAGMA_DB(member(obj##_count::count)           \
                  column("count(" + obj::key + ")"))   \

#pragma db object
struct test
  #pragma db id
  int id;



More information about the odb-users mailing list