具有函数特化的C ++ Template部分类特化

时间:2013-02-20 23:33:46

标签: c++ templates

我正在尝试使用模板做一些看似简单的事情,但我无法让它工作。该类正在为AVR处理器实现各种串行IO实现,但问题是通用的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形式的类型,它就符合我的目标!

1 个答案:

答案 0 :(得分: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.

或者您可以将公共代码分解为mixin模板:

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