2012-02-05 7 views
0

좋은 하루 되세요!C++ : 템플릿 : 부분 전문화 : 모든 템플릿 인쇄

다음 코드를 갖는

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 

내가 붙어있어. 부분 전문화를 끝내도록 도와주세요.

감사합니다.

+3

내가 [꽤 프린터 라이브러리]에서 당신을 관심 수 있음 (HTTP : //stackoverflow.com/questions/4850473/pretty-print-c-stl-containers)? –

+0

* 더 전문화 된 * 사례로만 전문화 할 수 있습니다. 'T *, Index' 형식은 "특별한"것이 아니라 완전히 다릅니다. –

+0

@KerrekSB 감사합니다. 제안하신 프로젝트는 매우 흥미 롭습니다! – DaddyM

답변

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>

+0

위대한! 그것은 작동합니다! – DaddyM

관련 문제