Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite::INTEGER, SQLite::FLOAT,... not declared constexpr #302

Open
Grumbel opened this issue Sep 25, 2020 · 3 comments
Open

SQLite::INTEGER, SQLite::FLOAT,... not declared constexpr #302

Grumbel opened this issue Sep 25, 2020 · 3 comments
Assignees

Comments

@Grumbel
Copy link

Grumbel commented Sep 25, 2020

The declaration of those constants currently looks like this:

namespace SQLite
{

extern const int INTEGER;   ///< SQLITE_INTEGER
extern const int FLOAT;     ///< SQLITE_FLOAT
extern const int TEXT;      ///< SQLITE_TEXT
extern const int BLOB;      ///< SQLITE_BLOB
extern const int Null;      ///< SQLITE_NULL

This renders them unusable in contexts that require a constant expression, such as a switch statements. Declaring them constexpr and assigning a value inside the header should fix this.

@SRombauts SRombauts self-assigned this Jul 25, 2021
@SRombauts
Copy link
Owner

Hello, thanks for the suggestion.
I think I'll go for it, though I wanted to avoid including <sqlite3.h> in any header for encapsulation, avoiding huge compilation time etc.

@dougnazar
Copy link
Contributor

You could do something like this:

diff --git a/include/SQLiteCpp/Column.h b/include/SQLiteCpp/Column.h
index 8fee096..a524e95 100644
--- a/include/SQLiteCpp/Column.h
+++ b/include/SQLiteCpp/Column.h
@@ -22,11 +22,11 @@ struct sqlite3_stmt;
 namespace SQLite
 {

-extern const int INTEGER;   ///< SQLITE_INTEGER
-extern const int FLOAT;     ///< SQLITE_FLOAT
-extern const int TEXT;      ///< SQLITE_TEXT
-extern const int BLOB;      ///< SQLITE_BLOB
-extern const int Null;      ///< SQLITE_NULL
+constexpr int INTEGER = 1;   ///< SQLITE_INTEGER
+constexpr int FLOAT   = 2;   ///< SQLITE_FLOAT
+constexpr int TEXT    = 3;   ///< SQLITE_TEXT
+constexpr int BLOB    = 4;   ///< SQLITE_BLOB
+constexpr int Null    = 5;   ///< SQLITE_NULL

 /**
  * @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement.
diff --git a/src/Column.cpp b/src/Column.cpp
index 60b3c3b..da80bf5 100644
--- a/src/Column.cpp
+++ b/src/Column.cpp
@@ -18,11 +18,11 @@
 namespace SQLite
 {

-const int INTEGER   = SQLITE_INTEGER;
-const int FLOAT     = SQLITE_FLOAT;
-const int TEXT      = SQLITE_TEXT;
-const int BLOB      = SQLITE_BLOB;
-const int Null      = SQLITE_NULL;
+static_assert(INTEGER == SQLITE_INTEGER, "SQLITE_INTEGER is no longer 1");
+static_assert(FLOAT == SQLITE_FLOAT, "SQLITE_FLOAT is no longer 2");
+static_assert(TEXT == SQLITE_TEXT, "SQLITE_TEXT is no longer 3");
+static_assert(BLOB == SQLITE_BLOB, "SQLITE_BLOB is no longer 4");
+static_assert(Null == SQLITE_NULL, "SQLITE_NULL is no longer 5");


 // Encapsulation of a Column in a row of the result pointed by the prepared Statement.

The only downside is I think it will affect binary compatibility.

@UnixY2K
Copy link
Contributor

UnixY2K commented Sep 16, 2023

we could add this as an optional feature, something similar to the std::filesystem in #378 or the DLL support in #406
so the main idea is to add a #define that the user or the build system can set to enable it
probably SQLITECPPP_USE_CONSTEXPR_TYPES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants