#include <iostream>
template< typename U >
struct base {
template< typename T >
base const & operator<<( T x ) const {
std::cout << sizeof( x ) << std::flush;
return *this;
}
};
template< typename U >
struct derived : public base< U > {
using base<U>::operator<<;
derived const & operator<<( float const & x ) const {
std::cout << "derived" << std::flush;
return *this;
}
};
int main() {
unsigned char c( 3 );
derived< double > d;
d << c;
d.operator<<( c );
return 0;
}
上記のコードの正しい答えを得るために必要なルール (テンプレートに関連するオーバーロードとオーバーライド、インテグラル プロモーションなど) について説明していただけますか? 有効ですか?ルールが長すぎる場合は、参考文献を提供してください。最新のコンパイラは、正しい結果について意見が分かれています。gcc-4.6 と icpc-12.1.0 は「11」が正解だと主張していますが、VS2010 はd << c;
あいまいさのためにコンパイルを拒否しますが、 d.operator<<( c );
. 後者は1
iircを出力します。では、誰が正しくて誰が間違っているのでしょうか?