我有一些问题超载模板成员运营商和使用make_pair:模板运营商<<重载和make_pair
class MyArchive
{
public:
template <class C> MyArchive & operator<< (C & c)
{
return (*this);
}
};
class A
{
};
int main()
{
MyArchive oa;
A a;
oa << a; //it works
oa << std::make_pair(std::string("lalala"),a); //it doesn't work
return 0;
}
我得到这个有趣错误:
/home/carles/tmp/provaserialization/main.cpp: In function ‘int main()’:
/home/carles/tmp/provaserialization/main.cpp:30: error: no match for ‘operator<<’ in ‘oa << std::make_pair(_T1, _T2) [with _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = A]((a, A()))’
/home/carles/tmp/provaserialization/main.cpp:11: note: candidates are: MyArchive& MyArchive::operator<<(C&) [with C = std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, A>]
为什么它的任何想法在第二种情况下没有找到operator<<
?
不知道理论基础是否非常有说服力,但是,因为无论如何临时都会坚持到表达式结尾(至少直到函数返回),并且不需要任何扩展。 – visitor 2011-02-16 12:34:36
@visitor:编辑并添加更多演示! – Nawaz 2011-02-16 12:41:47