1

オーバーロードされた << 演算子テンプレートを特殊化するのに問題があります:

一般的なテンプレートは次のように定義されています。

template<typename DocIdType,typename DocType>
std::ostream & operator << (std::ostream & os,
                            const Document<DocIdType,DocType> & doc)
{
   [...]
}

一般的なテンプレートは正常に機能します。次に、2 番目のテンプレート パラメーターを特殊化します。私はもう試した:

template<typename DocIdType>
std::ostream & operator << <DocIdType,std::string> (std::ostream & os,
                           const Document<DocIdType,std::string> & doc)
{
   [...]
}

このコードをコンパイルしようとすると、「C2768: 明示的なテンプレート引数の不正な使用」というコンパイラ エラーが発生します。

誰かが私が間違っていることを教えてもらえますか?

4

1 に答える 1

1

私は間違っているかもしれませんが、関数テンプレートを部分的に特化することはできないと思います。

できたとしても、単純な過負荷を好みます。

関数テンプレートを特化しない理由も参照してください。(ハーブ・サッターによる)


Coliruでライブを見る

#include <iostream>
#include <string>

template<typename DocIdType,typename DocType>
struct Document {};

template<typename DocIdType>
std::ostream & operator << (std::ostream & os, const Document<DocIdType,std::string> & doc) {
   return os << "for string";
}

template<typename DocIdType,typename DocType>
std::ostream & operator << (std::ostream & os, const Document<DocIdType,DocType> & doc) {
   return os << "for generic";
}

using namespace std;

int main(int argc, char *argv[])
{
    std::cout << Document<struct anything, std::string>() << "\n";
    std::cout << Document<struct anything, struct anything_else>() << "\n";
}

版画

for string
for generic
于 2013-09-15T18:47:51.177 に答える