2013-02-20 162 views
0

我想做一些看起来很简单使用模板,但我不能让它工作。该类正在为AVR处理器实现各种串行IO实现,但问题是一个通用的C++问题。目标是在编译时根据模板参数制作选项,以便用户友好并增加代码重用,并在某些地方由于重用而提高性能。C++模板部分类专业化与功能专业

问题很简单,但我找不到解决方案(如果有的话)。当编译下面使用Visual Studio 2008的代码我得到:

error C2039: 't1' : is not a member of 'Interface<T1,0,_C> 
error C2039: 't1' : is not a member of 'Interface<T1,1,_C> 
error C2039: 't2' : is not a member of 'Interface<T2,0,_C> 
error C2039: 't2' : is not a member of 'Interface<T2,1,_C> 

**我已经拆了我的测试代码到解释块,把它们放在一起进行整体测试情况**

这是的“通用”基本模板:

enum eType { T1, T2 }; 
enum eT1 { T1_I1, T1_I2 }; 
enum eT2 { T2_I1, T2_I2 }; 

//This defines the 'global/default' interface that is required 
template< eType _T, int _I, typename _C> 
struct Interface 
{ 
    bool initialise(); 
}; 

为此,我部分专门基于所述_T参数模板中添加由INITIALISE使用成员变量()等等:

//t1 has a new member function which initialise() uses 
template< int _I, typename _C> 
struct Interface< T1, _I, _C > 
{ 
    bool initialise(); 
    void t1(); 
}; 

//t2 has a new member function which initialise() might uses 
template< int _I, typename _C> 
struct Interface< T2, _I, _C > 
{ 
    bool initialise(); 
    void t2(); 
}; 

//We can implement a function for T1 type 
template< int _I, typename _C> 
bool Interface< T1, _I, _C >::initialise() 
{ printf("T1 initialise\n"); return true; } 

//We can implement a function for T2 type 
template< int _I, typename _C> 
bool Interface< T2, _I, _C >::initialise() 
{ printf("T2 initialise\n"); return true; } 

//We can implement a function for T1 special function 
template< int _I, typename _C> 
void Interface< T1, _I, _C >::t1() 
{ printf("T1\n"); } 

//We can implement a function for T2 special function 
template< int _I, typename _C> 
void Interface< T2, _I, _C >::t2() 
{ printf("T2\n"); } 

现在到了我无法弄清楚如何根据第二个模板参数_I来专门实现t1()和t2()函数的地方。

//################ ISUE BELOW ################### 

//ERROR: We can't implement the special function for T1 based on _I specialization 
template< typename _C> 
void Interface< T1, (int)T1_I1, _C >::t1() 
{ printf("T1_I1 Special function\n"); } 

//ERROR: We can't implement the special function for T1 based on _I specialization 
template< typename _C> 
void Interface< T1, (int)T1_I2, _C >::t1() 
{ printf("T1_I2 Special function\n"); } 

//ERROR: We can't implement the special function for T2 based on _I specialization 
template< typename _C> 
void Interface< T2, (int)T2_I1, _C >::t2() 
{ printf("T2_I1 Special function\n"); } 

//ERROR: We can't implement the special function for T2 based on _I specialization 
template< typename _C> 
void Interface< T2, (int)T2_I2, _C >::t2() 
{ printf("T2_I2 Special function\n"); } 

//################ ISUE END ################### 

我们在main()函数测试中,它的所有编译:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    struct Config {}; 
    Interface<T1, T1_I1, Config> t1i1; 
    Interface<T1, T1_I2, Config> t1i2; 
    Interface<T2, T2_I1, Config> t2i1; 
    Interface<T2, T2_I2, Config> t2i2; 

    t1i1.initialise(); 
    t1i2.initialise(); 
    t1i1.t1(); 
    t1i2.t1(); 

    t2i1.initialise(); 
    t2i2.initialise(); 
    t2i1.t2(); 
    t2i2.t2(); 
    return 0; 
} 

的问题会被编译器造成没有看到原始类专业化的存在和它使用的是非专用接口,它没有t1()或t2()。我在哪里得到的语法错误,或者是否有简单的黑客/解决方法来完成我正在尝试做的事情。只要解决方案能够以Serial<UART,Hardware,Config> io的形式产生一个类型,它就符合我的目标!

+0

你不能做到这一点。函数模板不能被部分地专门化。 – 2013-02-20 23:41:30

回答

0

你必须一一列出所有部分专业化。例如:

template <eType E, int I, typename T> struct Interface; 

template <int I, typename T> struct Interface<T1, I, T> 
{ 
    void t1() { /* ... */ } 
    bool initialize() { /* ... */ } 
}; 

template <typename T> struct Interface<T1, static_cast<int>(T1), T> 
{ 
    void t1() { /* ... */ } 
    bool initialize() { /* ... */ } 
}; 

您可以随时考虑您的代码,以避免在适当时重复。例如:

namespace detail 
{ 
    template <typename T, int I> struct T1Helper 
    { 
     static bool initialize() { /* ... */ } 
    }; 
} 

// ... primary template as above ... 

template <int I, typename T> struct Interface<T1, I, T> 
{ 
    void t1() { /* ... */ } 
    bool initialize() { return detail::T1Helper<T, I>::initialize(); } 
}; 

// etc. 

或者你也可以因素的共同代码放到一个混合模板:

template <int I, typename T> 
struct Interface<T1, I, T> : Mixin<T, I> 
{ 
    void t1() { /* ... */ } 
}; 
+0

感谢您的回复。我发现无法获得部分功能专业化,并且您的三个示例在展示如何解决此问题方面非常有用。我将尝试一个MixIn的版本,它是一个Helper,它将会拥有我需要的所有功能。再次感谢你的帮助。 – Crog 2013-02-21 09:58:28