2016-12-01 113 views
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; 
} 

回答

2

你錯過了static關鍵字:

template<typename S> 
static T* create() { 
    static_assert(std::is_base_of<T, S>::value, "S must be a derived of T."); 
    return new S(); 
} 

Demo

因爲沒有static,這是一個非靜態成員函數,它的類型是T* (Factory::*)(); 如果你真的需要它作爲一個非 - 靜態成員函數:

using self = Factory; 
using create_t = T* (self::*)(); 

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 = &self::create<S>; 
    T * pt = (this->*pf)(); 
    pt->print();                                                
    delete pt; 
} 

Demo

+0

非常感謝,它的工作原理。但爲什麼非靜態函數不起作用?非模板普通版本可以。 –