0

我正在尝试使用模板做一些看起来很简单的事情,但我就是无法让它工作。该课程正在为 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它就符合我的目标!

4

1 回答 1

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() { /* ... */ }
};
于 2013-02-20T23:44:57.700 回答