SRombauts / SQLiteCpp

SQLiteC++ (SQLiteCpp) is a smart and easy to use C++ SQLite3 wrapper.
http://srombauts.github.io/SQLiteCpp
MIT License
2.22k stars 510 forks source link

cannot bind to long long (64 bit signed integer)? #462

Open MacroUniverse opened 7 months ago

MacroUniverse commented 7 months ago

Hi, I was trying to bind an SQLite::Statement with ? to the type long long, with member function bind(1, i). But there's a compiler error, I had to cast the integer to 32bit int to make it work. Is this a limitation of sqlite3 itself or just SQLiteCpp?

I'm using version 3.2.1

tamaskenez commented 7 months ago

This works for me (with apple clang 15)

            int64_t i64 = 0;
            statement.bind(1, i64);

Can you provide more information, code, platform, compiler, actual error?

MacroUniverse commented 7 months ago

I see, I think the problem is using long long instead of int64_t. The compiler says it's ambiguous with candidates:

void SQLite::Statement::bind(int, int32_t)
void SQLite::Statement::bind(int, uint32_t)
void SQLite::Statement::bind(int, int64_t)
void SQLite::Statement::bind(int, double)

It would be nice to have a version explicitly for long long and use some template programming to redirect to one of the above.

I'm using g++ 11.4 on Ubuntu 22.04.

tamaskenez commented 7 months ago

I see and I agree. I'd simply start with this:

void SQLite::Statement::bind(int index, long long value) {
    static_assert(sizeof(long long) == sizeof(int64_t));
    bind(index, int64_t(value));
}

And improve it only if there's a demand supporting platforms where sizeof(long long) is different.

UnixY2K commented 5 months ago

I think the user should be aware of how sqlite expects the types, probably using some templates/concepts could help, however this should be optional as it will break for some users