我有一些代码。禁止运营商<<致电
#include <iostream>
template<typename T>
struct Test
{
Test(bool v):flg(v) { }
void func() { }
typedef void (Test::*unspecified)();
operator unspecified() const
{
return flg ? &Test::func : 0;
}
bool flg;
};
template<typename T>
std::ostream& operator << (std::ostream&, typename Test<T>::unspecified);
int main()
{
Test<int> t(true);
std::cout << t << std::endl;
}
输出是
1
它工作正常,但我想未定义的引用。如果Test
是not template class
我得到未定义的参考。那么,为什么编译器不能使用operator <<
作为函数类型,并且做了从pointer to class-member
到bool
的非标准转换?
请参阅[为什么模板参数推演不适用于此?](http://stackoverflow.com/questions/1268504/why-is-the-template-argument-deduction-not-working-here?lq=1 ) – 2012-08-10 11:39:17
是否有任何理由不能使用'template std :: ostream&operator <<(std :: ostream&,const Test &);'? –
juanchopanza
2012-08-10 11:42:32