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!
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>