我有一些代碼。禁止運營商<<致電
#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