Skip to content

Commit 35aaf73

Browse files
committed
Add implicit cast operators to char & short and their unsigned variants
Fix #179 error: conversion from 'SQLite::Column' to 'unsigned char' is ambiguous
1 parent 2e69a81 commit 35aaf73

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,15 @@ Version 2.2.0 - Sept 19 2017
116116
- Fix warnings #134
117117
- Deprecated Statement::IsOk() to Statement::HasRow()
118118

119-
Version 2.3.0
119+
Version 2.3.0 - March 3 2019
120120
- Update SQLite3 from 3.20.1 to latest 3.27.2 (2019-02-25) #183 #187
121121
- Add Statement binding for long int values #147
122122
- Allows long int for bind when used with name #148
123123
- More cmake instructions for linux #151
124124
- Add comparison with sqlite_orm #141
125125
- Fix Statement::bind truncates long integer to 32 bits on x86_64 Linux #155
126126
- Add a move constructor to Database #157
127-
- Added tests for all MSVC compilers available on AppVeyor #169
127+
- Added tests for all MSVC compilers available on AppVeyor (2013, 2015, 2017) #169
128128
- Update VariadicBind.h #172
129129
- Better CMake compatibility #170
130+
- Add implicit cast operator to char and short types

include/SQLiteCpp/Column.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ class Column
166166
return getBytes ();
167167
}
168168

169+
/// Inline cast operator to char
170+
inline operator char() const
171+
{
172+
return static_cast<char>(getInt());
173+
}
174+
/// Inline cast operator to unsigned char
175+
inline operator unsigned char() const
176+
{
177+
return static_cast<unsigned char>(getInt());
178+
}
179+
/// Inline cast operator to short
180+
inline operator short() const
181+
{
182+
return static_cast<short>(getInt());
183+
}
184+
/// Inline cast operator to unsigned short
185+
inline operator unsigned short() const
186+
{
187+
return static_cast<unsigned short>(getInt());
188+
}
189+
169190
/// Inline cast operator to int
170191
inline operator int() const
171192
{

tests/Column_test.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ TEST(Column, basis) {
5757

5858
// validates every variant of cast operators, and conversions of types
5959
{
60-
const sqlite3_int64 id1 = query.getColumn(0); // operator int64_t()
61-
const int64_t id2 = query.getColumn(0); // operator int64_t()
62-
const long long id3 = query.getColumn(0); // operator int64_t()
63-
const long id4 = query.getColumn(0); // operator int64_t() or long() depending on compiler/architecture
64-
const unsigned int uint1 = query.getColumn(0); // operator uint32_t()
65-
const uint32_t uint2 = query.getColumn(0); // operator uint32_t()
60+
const sqlite3_int64 id1 = query.getColumn(0); // operator long long()
61+
const int64_t id2 = query.getColumn(0); // operator long long()
62+
const long long id3 = query.getColumn(0); // operator long long()
63+
const long id4 = query.getColumn(0); // operator long long() or long() depending on compiler/architecture
64+
const char id5 = query.getColumn(0); // operator char()
65+
const short id6 = query.getColumn(0); // operator short()
66+
const unsigned int uint1 = query.getColumn(0); // operator unsigned int()
67+
const uint32_t uint2 = query.getColumn(0); // operator unsigned int()
68+
const unsigned char uint3 = query.getColumn(0); // operator unsigned char()
69+
const unsigned short uint4 = query.getColumn(0); // operator unsigned short()
6670
const char* ptxt = query.getColumn(1); // operator const char*()
6771
const std::string msg = query.getColumn(1); // operator std::string() (or const char* with MSVC)
6872
const int integer = query.getColumn(2); // operator int()
@@ -78,8 +82,12 @@ TEST(Column, basis) {
7882
EXPECT_EQ(1, id2);
7983
EXPECT_EQ(1, id3);
8084
EXPECT_EQ(1, id4);
85+
EXPECT_EQ(1, id5);
86+
EXPECT_EQ(1, id6);
8187
EXPECT_EQ(1U, uint1);
8288
EXPECT_EQ(1U, uint2);
89+
EXPECT_EQ(1U, uint3);
90+
EXPECT_EQ(1U, uint4);
8391
EXPECT_STREQ("first", ptxt);
8492
EXPECT_EQ("first", msg);
8593
EXPECT_EQ(-123, integer);

0 commit comments

Comments
 (0)