与Template的参数类型/值不匹配

时间:2014-10-13 07:46:34

标签: c++ templates

我正在编写一些c ++模板代码来替换当前源中的if-else条件。在这里,我基于两个条件派生Helper数据类型,1。是Advice 2. SimpleOrComplex

见下面的模板代码:

template<bool isAdvice, class SH, class CH>
class IfThenElse;

template<class SH, class CH>
class IfThenElse<true, SH, CH>
{
    public:
    typedef SH Helper;
};

template<class SH, class CH>
class IfThenElse<false, SH, CH>
{
    public:
    typedef CH Helper;
};

template <bool isAdvice, bool SimpleOrComplex>
class DeriveHelper
{
    public:
        typedef typename IfThenElse<isAdvice,
                 IfThenElse<SimpleOrComplex, SimpleHelper, ComplexHelper>::Helper,
                 IfThenElse<SimpleOrComplex, SimpleNoAdvHelper, ComplexNoAdvHelper>::Helper>::Helper DerivedHelper;


};

但是,在编译时出现此错误:

template.cpp:135: error: type/value mismatch at argument 2 in template parameter list for 'template<bool isTradeAdvice, class SH, class GH> struct IfThenElse'
template.cpp:135: error:   expected a type, got 'IfThenElse::Helper'
template.cpp:135: error: type/value mismatch at argument 3 in template parameter list for 'template<bool isTradeAdvice, class SH, class GH> struct IfThenElse'
template.cpp:135: error:   expected a type, got 'IfThenElse::Helper'

有人可以说明原因吗?

1 个答案:

答案 0 :(得分:5)

您应该为两个模板类型参数typename添加IfThenElse关键字,就像对第一个模板类型参数一样

template <bool isAdvice, bool SimpleOrComplex>
class DeriveHelper
{
public:
    typedef typename IfThenElse<isAdvice,
                 typename IfThenElse<SimpleOrComplex, SimpleHelper, ComplexHelper>::Helper,
                 ^^^^^^^^
                 typename IfThenElse<SimpleOrComplex, SimpleNoAdvHelper, ComplexNoAdvHelper>::Helper>::Helper DerivedHelper;
                 ^^^^^^^^
};