1
我想將混凝土工廠變爲模板工廠類。以下是我寫的;我正在使用g ++(GCC)4.8.5。如何將模板類中的模板函數轉換爲函數指針
g ++抱怨說error: no matches converting function ‘create’ to type ‘using create_t = class Base* (*)() {aka class Base* (*)()}’
。它接近句子create_t pf = create<S>;
失敗,但我不知道它是什麼。
#include<iostream>
#include<type_traits>
class Base {
public:
virtual ~Base() {};
virtual void print() {
std::cout << "In Base." << std::endl;
}
};
class Derived: public Base {
public:
void print() {
std::cout << "In Derived." << std::endl;
}
};
template<typename T>
class Factory {
public:
using create_t = T* (*)();
template<typename S>
T* create() {
static_assert(std::is_base_of<T, S>::value, "S must be a derived of T.");
return new S();
}
template<typename S>
void test() {
create_t pf = create<S>;
T * pt = pf();
pt->print();
delete pt;
}
};
int main() {
Factory<Base> base_factory;
base_factory.test<Derived>();
return 0;
}
非常感謝,它的工作原理。但爲什麼非靜態函數不起作用?非模板普通版本可以。 –