【发布时间】:2018-06-19 10:03:24
【问题描述】:
我有一个使用外部函数bind 的类InsertStatement:
namespace sqlite {
template <typename T>
inline void bind(SQLiteStatement &statement, size_t idx, const T &value) {
statement.bind(idx, value);
}
template<typename ...FIELDS>
class InsertStatement {
...
template <typename T>
bool bindValue(int idx, const T& t) {
sqlite::bind(*statement, idx+1, t);
return true;
}
...
};
使用外部函数的原因是能够覆盖它并支持在InsertStatement类中使用其他类型。例如,如果我想将它与StrongType<T> 一起使用,我可以这样做:
template <typename T, typename TAG>
class StrongType {
private:
T value;
public:
StrongType (T&& v)
: value(std::forward<T>(v)) {
}
T toValue() const
{
return value;
}
};
namespace sqlite {
template <typename T, typename TAG>
inline void bind (SQLiteStatement &statement, size_t s, const StrongType<T,TAG> &strongType) {
statement.bind(s, strongType.toValue());
}
}
问题是我需要包含StrongType.h before InsertStatement.h,否则编译器无法正确解析函数调用。
虽然我可以直观地解释它,但问题是,我该如何避免这个问题?我不想从#include "StrongType.h" 到InsertStatement.h,因为StrongType 是一个与这个库没有直接关系的外部类,因为这确实会发生在任何新类型上,我想让这个类足够灵活。
我正在使用不同的编译器(gcc、clang 和 MSVC)、c++14(c++17 或更高版本暂时不是一个选项)。
- 如何避免这种“标题排序”问题?
- 让 Templated 类扩展为其他类型的最佳方法是什么?
【问题讨论】:
标签: c++ templates c++14 template-meta-programming