C++ 重载的部分模板专门化<&书信电报;操作人员
我很难专门化一个重载的函数,我可能是错的,但我会毫不犹豫地说函数模板不能部分专门化 即使可以,他们也更喜欢直接重载 另见(Herb Sutter)C++ 重载的部分模板专门化<&书信电报;操作人员,c++,template-specialization,C++,Template Specialization,我很难专门化一个重载的函数,我可能是错的,但我会毫不犹豫地说函数模板不能部分专门化 即使可以,他们也更喜欢直接重载 另见(Herb Sutter) 查看它 #include <iostream> #include <string> template<typename DocIdType,typename DocType> struct Document {}; template<typename DocIdType> std::ostream
查看它
#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";
}
我可能错了,但我会毫不犹豫地说函数模板不能部分专用化 即使可以,他们也更喜欢直接重载 另见(Herb Sutter)
查看它
#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";
}
#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