Good evening!
There is one interesting question: does using a constructor instead of calling a function in C++ is overhead?
For example, the program uses one class with partial specialization of templates:
class BindingApplierBase {
public:
template<typename ...Args>
void bindOrThrow(std::_Bind<int (*((Args...)))(Args...)> sqliteBind) {
if (sqliteBind() != SQLITE_OK)
throw std::runtime_error("Bind error");
}
};
template<typename T>
class BindingApplier : public BindingApplierBase {
public:
BindingApplier(sqlite3_stmt *statement, int id, T data) {
throw std::runtime_error("Template by id used!");
}
};
template<>
class BindingApplier<int> : public BindingApplierBase {
public:
BindingApplier(sqlite3_stmt *statement, int id, int data) {
this->bindOrThrow(std::bind(sqlite3_bind_int, statement, id, data));
}
};
template<>
class BindingApplier<double> : public BindingApplierBase {
public:
BindingApplier(sqlite3_stmt *statement, int id, double data) {
this->bindOrThrow(std::bind(sqlite3_bind_double, statement, id, data));
}
};
BindingApplier(float) -> BindingApplier<double>;
Usage example:
BindingApplier(this->m_statement, this->m_currentBindingId, value);
Instead of a function, a separate class is used for two reasons
- In order not to inflate the end interface
- In c++ there is an interesting thing for “redirecting” a template type.
BindingApplier(float) -> BindingApplier<double>;
And so I think – is there an overhead from this, maybe it’s worth just processing float and std::string separately?
Or maybe you have another cool idea?