Skip to content

Commit

Permalink
Add implicit cast operators to char & short and their unsigned variants
Browse files Browse the repository at this point in the history
Fix #179 error: conversion from 'SQLite::Column' to 'unsigned char' is ambiguous
  • Loading branch information
SRombauts committed Mar 3, 2019
1 parent ca45c67 commit f31b16e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ Version 2.2.0 - Sept 19 2017
- Fix warnings #134
- Deprecated Statement::IsOk() to Statement::HasRow()

Version 2.3.0
Version 2.3.0 - March 3 2019
- Update SQLite3 from 3.20.1 to latest 3.27.2 (2019-02-25) #183 #187
- Add Statement binding for long int values #147
- Allows long int for bind when used with name #148
- More cmake instructions for linux #151
- Add comparison with sqlite_orm #141
- Fix Statement::bind truncates long integer to 32 bits on x86_64 Linux #155
- Add a move constructor to Database #157
- Added tests for all MSVC compilers available on AppVeyor #169
- Added tests for all MSVC compilers available on AppVeyor (2013, 2015, 2017) #169
- Update VariadicBind.h #172
- Better CMake compatibility #170
- Add implicit cast operator to char and short types
21 changes: 21 additions & 0 deletions include/SQLiteCpp/Column.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ class Column
return getBytes ();
}

/// Inline cast operator to char
inline operator char() const
{
return static_cast<char>(getInt());
}
/// Inline cast operator to unsigned char
inline operator unsigned char() const
{
return static_cast<unsigned char>(getInt());
}
/// Inline cast operator to short
inline operator short() const
{
return static_cast<short>(getInt());
}
/// Inline cast operator to unsigned short
inline operator unsigned short() const
{
return static_cast<unsigned short>(getInt());
}

/// Inline cast operator to int
inline operator int() const
{
Expand Down
20 changes: 14 additions & 6 deletions tests/Column_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ TEST(Column, basis) {

// validates every variant of cast operators, and conversions of types
{
const sqlite3_int64 id1 = query.getColumn(0); // operator int64_t()
const int64_t id2 = query.getColumn(0); // operator int64_t()
const long long id3 = query.getColumn(0); // operator int64_t()
const long id4 = query.getColumn(0); // operator int64_t() or long() depending on compiler/architecture
const unsigned int uint1 = query.getColumn(0); // operator uint32_t()
const uint32_t uint2 = query.getColumn(0); // operator uint32_t()
const sqlite3_int64 id1 = query.getColumn(0); // operator long long()
const int64_t id2 = query.getColumn(0); // operator long long()
const long long id3 = query.getColumn(0); // operator long long()
const long id4 = query.getColumn(0); // operator long long() or long() depending on compiler/architecture
const char id5 = query.getColumn(0); // operator char()
const short id6 = query.getColumn(0); // operator short()
const unsigned int uint1 = query.getColumn(0); // operator unsigned int()
const uint32_t uint2 = query.getColumn(0); // operator unsigned int()
const unsigned char uint3 = query.getColumn(0); // operator unsigned char()
const unsigned short uint4 = query.getColumn(0); // operator unsigned short()
const char* ptxt = query.getColumn(1); // operator const char*()
const std::string msg = query.getColumn(1); // operator std::string() (or const char* with MSVC)
const int integer = query.getColumn(2); // operator int()
Expand All @@ -74,8 +78,12 @@ TEST(Column, basis) {
EXPECT_EQ(1, id2);
EXPECT_EQ(1, id3);
EXPECT_EQ(1, id4);
EXPECT_EQ(1, id5);
EXPECT_EQ(1, id6);
EXPECT_EQ(1U, uint1);
EXPECT_EQ(1U, uint2);
EXPECT_EQ(1U, uint3);
EXPECT_EQ(1U, uint4);
EXPECT_STREQ("first", ptxt);
EXPECT_EQ("first", msg);
EXPECT_EQ(-123, integer);
Expand Down

0 comments on commit f31b16e

Please sign in to comment.