0

Good day everybody!

Having the following code:

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_;
};

Compiler: MSVCPP10

Compiler Output:

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

I'm stuck. Help me to finish partial specialization.

Thanks!

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
nickolay
  • 2,860
  • 2
  • 25
  • 35
  • 3
    Might I interest you in the [pretty printer library](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers)? – Kerrek SB Feb 05 '12 at 19:42
  • You can only specialise something with a *more specialized* case. Your `T*, Index` form is not "more special", it is completely different. – Kerrek SB Feb 05 '12 at 19:44
  • @KerrekSB Thanks, the project you've suggested is very interesting! – nickolay Feb 05 '12 at 19:48
  • @KerrekSB Thanks, I've tried to add template parameters after `struct print – nickolay Feb 05 '12 at 19:49
  • 1
    Well, it doesn't make sense to have a "specialization" that is *more* general than the primary template. Perhaps you could have a single class templated on `T`, and then *multiple* member function template overloads. Make them `static`, too, like I suggest in the other question, and you could probably build a very general solution. – Kerrek SB Feb 05 '12 at 19:54

1 Answers1

1

I assume this is what you mean: (This may be incorrect code, I'm only trying to translate what you wrote)

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_;
};

The compiler explains what went wrong:

default arguments not allowed on a partial specialization

Meaning things like typename Index = unsigned int can only appear in the non-specialized template.

template parameter not used or deducible in partial specialization

Meaning you must use all the parameters in this part: struct print<HERE>

Pubby
  • 48,511
  • 12
  • 121
  • 172