SQLiteCpp icon indicating copy to clipboard operation
SQLiteCpp copied to clipboard

SQLite::INTEGER, SQLite::FLOAT,... not declared `constexpr`

Open Grumbel opened this issue 5 years ago • 3 comments

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.

Grumbel avatar Sep 25 '20 11:09 Grumbel

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.

SRombauts avatar Jul 26 '21 07:07 SRombauts

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.

dougnazar avatar Dec 26 '22 09:12 dougnazar

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

UnixY2K avatar Sep 16 '23 21:09 UnixY2K