@@ -336,6 +336,41 @@ You can enable boost support by defining _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT b
336336 }
337337```
338338
339+ Variant type support (C++17)
340+ ----
341+ If your columns may have flexible types, you can use C++17's `std::variant` to extract the value.
342+
343+ ```c++
344+ db << "CREATE TABLE tbl (id integer, data);";
345+ db << "INSERT INTO tbl VALUES (?, ?, ?);" << 1 << vector<int> { 1, 2, 3};
346+ unique_ptr<string> ptr_null; // you can even bind empty unique_ptr<T>
347+ db << "INSERT INTO tbl VALUES (?, ?, ?);" << 2 << 2.5;
348+
349+ db << "select data from tbl where id = 1"
350+ >> [](std::variant<vector<int>, double> data) {
351+ if(data.index() != 1) {
352+ cerr << "ERROR: we expected a blob" << std::endl;
353+ }
354+
355+ for(auto i : get<vector<int>>(data)) cout << i << ","; cout << endl;
356+ };
357+
358+ db << "select age,name,img from tbl where id = 2"
359+ >> [](std::variant<vector<int>, double> data) {
360+ if(data.index() != 2) {
361+ cerr << "ERROR: we expected a real number" << std::endl;
362+ }
363+
364+ cout << get<double>(data) << endl;
365+ };
366+ ```
367+
368+ If you read a specific type and this type does not match the actual type in the SQlite database, yor data will be converted.
369+ This does not happen if you use a ` variant ` .
370+ If the ` variant ` does an alternative of the same value type, an ` mismatch ` exception will be thrown.
371+ The value types are NULL, integer, real number, text and BLOB.
372+ To support all possible values, you can use ` variant<nullptr_t, sqlite_int64, double, string, vector<char> ` .
373+
339374Errors
340375----
341376
0 commit comments