关于c ++:与Template的参数类型/值不匹配

type/value mismatch at argument with Template

本问题已经有最佳答案,请猛点这里访问。

我正在编写一些C++模板代码来替换当前源中的IF条件。这里,我根据两个条件1派生助手数据类型。建议2。单纯形复合体

见以下模板代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;


};

但是,在编译时获取此错误:

1
2
3
4
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'

有人能提出原因吗?


您应该为两个模板类型参数IfThenElse预先准备一个typename关键字,就像对第一个模板类型参数所做的那样。

1
2
3
4
5
6
7
8
9
10
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;
                 ^^^^^^^^
};