6

全て、

次のコードが 'std::endl' のコンパイルに失敗するのはなぜですか?

#include <sstream> // ostringstream

/// @brief A class that does streamed, formatted output via 'operator<<'.
class My_Stream
{
public:
    /// @brief A member method that manipulates the underlying stream.
    void foo()
    {
        m_oss << "foo_was_here; ";
    }

private:
    /// @brief The underlying stream.
    std::ostringstream m_oss;

    /// @brief 'operator<<' is a friend.
    template< typename T >
    friend My_Stream& operator<<( My_Stream& a_r_my_stream,
                                  const T& a_r_value );
};

/// @brief A manipulator that calls a class method.
My_Stream& manipulator_foo( My_Stream& a_r_my_stream )
{
    a_r_my_stream.foo();
    return a_r_my_stream;
}

/// @brief The generic insertion operator.
template< typename T >
My_Stream& operator<<( My_Stream& a_r_my_stream,
                       const T& a_r_value )
{
    a_r_my_stream.m_oss << a_r_value;
    return a_r_my_stream;
}

/// @brief Define an iostream-like manipulator for my-stream.
typedef My_Stream& ( * my_stream_manipulator ) ( My_Stream& );

/// @brief The specialized 'my_stream_manipulator' insertion operator.
template<>
My_Stream& operator<<( My_Stream& a_r_my_stream,
                       const my_stream_manipulator& a_r_manipulator )
{
    return a_r_manipulator( a_r_my_stream );
}

int main( int argc, char* argv[] )
{
    My_Stream my_stream;

    my_stream << 'c'; // char
    my_stream << "string"; // c-string
    my_stream << 1u; // unsigned int
    my_stream << -1; // signed int
    my_stream << 5.3f; // float
    my_stream << -23.345; // double
    my_stream << std::boolalpha; // std::ios_base manipulator
    my_stream << std::endl; // std::ostream manipulator
    my_stream << manipulator_foo; // my_stream manipulator

    return 0;
}

次の G++ 4.5 エラーが発生します。

willo:~/test_cpp$ g++ -Wall test_overloaded_insertion_manipulators.cpp test_overloaded_insertion_manipulators.cpp: 関数 'int main(int, char**)': test_overloaded_insertion_manipulators.cpp:60: エラー: 'my_stream < の 'operator<<' に一致しません< std::endl'</p>

プリミティブ、std::ios_base、およびカスタム マニピュレータの場合と同様に、コードが std::endl の「operator<<」をインスタンス化することを期待しています。

コンテキストとして、現在の IOStream マニピュレータと 1 つまたは 2 つのカスタム マニピュレータで動作するライト API IOStream のようなクラスを作成しようとしています。

4

2 に答える 2

8

endlは関数テンプレートであるため:

template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);

したがって、識別子自体は値ではありません。インスタンス化されたときにのみ値 (関数ポインター) になります。ただし、operator<<それ自体がテンプレートであり、インスタンス化する型を決定するためにコンパイラが使用できる型情報はありませんendl

対照的に、たとえばboolalpha次のとおりです。

ios_base& boolalpha(ios_base& str);

したがって、なぜそれが機能するのか。

endlで機能します。これは、オーバーロードを関数ポインターを取るメンバー関数としてbasic_ostream定義しているためです。operator<<特に:

basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));

そのため、 のような呼び出しではstream << endl、の型(つまり、演算子の左側) からcharTとを認識し、右側で期待される関数ポインターの正確な型を取得します。これを使用して、 の対応するバージョンをインスタンス化します。クラスでも同じことができます。traitsthisendl

于 2011-05-18T23:13:58.680 に答える
2

ストリーム マニピュレータを別のフレンドとして定義する必要があります。

この友達を追加:

// Note: Untested (don't have a compiler here).
template <class charT, class Traits>
friend My_Stream& operator<<( My_Stream&, std::basic_ostream<charT, Traits>& (*)(std::basic_ostream<charT, Traits>&));

次に、関数を定義する必要があります。

template <class charT, class Traits>
My_Stream& operator<<( My_Stream& stream, std::basic_ostream<charT, Traits>& (*manip)(std::basic_ostream<charT, Traits>&))
{
    (*manip)(stream.m_oss);
    return stream;
}
于 2011-05-18T23:15:27.107 に答える