0

大家好!

具有以下代码:

template<typename T, typename OutStream = std::ostream>
struct print
{
    OutStream &operator()(T const &toPrint, OutStream &outStream = std::cout) const
    {
        outStream << toPrint;
        return outStream;
    }
};

template<typename T, typename Index = unsigned int, typename CharType = char, typename OutStream = std::ostream>
struct print<T *, Index>
{
    print(CharType delimiter = ' '): delimiter_(delimiter) {}
    OutStream &operator()(T const *toPrint, Index startIndex, Index finishIndex, OutStream &outStream = std::cout) const
    {
        for (Index i(startIndex) ; i < finishIndex ; ++i)
            outStream << toPrint[i] << delimiter_;
        return outStream;
    }
protected:
    CharType delimiter_;
};

编译器:MSVCPP10

编译器输出:

1>main.cpp(31): error C2764: 'CharType' : template parameter not used or deducible in partial specialization 'print<T*,Index>'
1>main.cpp(31): error C2764: 'OutStream' : template parameter not used or deducible in partial specialization 'print<T*,Index>'
1>main.cpp(31): error C2756: 'print<T*,Index>' : default arguments not allowed on a partial specialization

我被困住了。帮助我完成部分专业化。

谢谢!

4

1 回答 1

1

我想这就是你的意思:(这可能是不正确的代码,我只是想翻译你写的东西)

template<typename T> struct print<T*, unsigned int> {
  typedef unsigned int Index;
  typedef std::ostream OutStream;
  typedef char CharType;
  print(CharType delimiter = ' '): delimiter_(delimiter) {}
  OutStream &operator()(T const *toPrint, Index startIndex, Index finishIndex, OutStream &outStream = std::cout) const {
    for (Index i(startIndex) ; i < finishIndex ; ++i)
      outStream << toPrint[i] << delimiter_;
    return outStream;
  }
protected:
  CharType delimiter_;
};

编译器解释出了什么问题:

部分特化不允许使用默认参数

含义之类的东西typename Index = unsigned int只能出现在非专业模板中。

在部分特化中未使用或可推导出的模板参数

这意味着您必须使用此部分中的所有参数:struct print<HERE>

于 2012-02-05T19:46:02.433 回答