【发布时间】:2016-02-02 21:12:39
【问题描述】:
虽然这里有几个类似主题的问题,但我没有让我的代码工作。我有以下课程:
template<class T, class DataGetter>
class LogableTemplate
{
public:
//logs data got by dataGetter
static DataGetter dataGetter;
};
template<class T, class DataGetter>
DataGetter LogableTemplate<T, DataGetter>::dataGetter;
template<class T, class DataGetter>
class Logable : public LogableTemplate<T, DataGetter>
{
};
我是这样使用这个类的:
class ADataGetter;
class A : public Logable<A, ADataGetter> {}
class ADataGetter { //generic external functor returning some data
public:
int operator()(const A&a) { return 3; }
};
但有一种特殊情况,当我有某个类将其他类转换为所需类型时,例如:
template <class T>
class IntClassMapper {
public:
int mapToInt(const T &t);
};
这很好,但它不需要 operator()。我做了助手类:
template<class T, class Mapper>
class IntMapperDataGetter {
public:
int operator()(const T &t) {
return mapper.mapToInt(t);
}
static Mapper mapper;
};
template<class T, class Mapper>
Mapper IntMapperDataGetter<T, Mapper>::mapper;
对于 Logable 的第二个模板参数是从 IntClassMapper 继承的情况,我不需要部分特化。我尝试了以下代码(以及数百个类似代码),但我总是遇到一些编译错误 - 通常:
error: template parameters not deducible in partial specialization
代码如下:
template<class T, class Mapper>
class Logable<T, typename std::enable_if<std::is_base_of<IntClassMapper<T>, Mapper>::value, IntMapperDataGetter<T, Mapper>>::type> : public LogableTemplate<T, IntMapperDataGetter<T, Mapper>>
{
};
您可以在https://ideone.com/qz9jIt上检查和测试代码
这样可以吗? 谢谢你,马丁
【问题讨论】: