Type of ios manipulators

Discussion in 'C++' started by Lionel B, Apr 20, 2007.

  1. Lionel B

    Lionel B Guest

    The following code compiles ok and runs as expected:

    #include <iostream>
    #include <iomanip>

    int main()
    {
    typedef std::eek:stream& (*manip_t)(std::eek:stream&);

    manip_t x = std::endl;

    if (x==static_cast<manip_t>(std::endl))
    std::cout << "endl detected" << std::endl;

    return 0;
    }

    However, without the cast I get a compiler error:

    main.cpp:10: error: assuming cast to type ‘std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)’ from overloaded function

    which I interpret as:

    main.cpp:10: error: assuming cast to type ‘std::eek:stream& (*)(std::eek:stream&)’ from overloaded function

    ie.

    main.cpp:10: error: assuming cast to type ‘manip_t’ from overloaded function

    But my standard library ref defines:

    template<typename CharT, typename Traits> basic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)

    which also boils down to my manip_t... so why does my compiler
    seem to think an explicit cast is necessary?

    --
    Lionel B
    Lionel B, Apr 20, 2007
    #1
    1. Advertising

  2. * Lionel B:
    > The following code compiles ok and runs as expected:
    >
    > #include <iostream>
    > #include <iomanip>
    >
    > int main()
    > {
    > typedef std::eek:stream& (*manip_t)(std::eek:stream&);
    >
    > manip_t x = std::endl;
    >
    > if (x==static_cast<manip_t>(std::endl))
    > std::cout << "endl detected" << std::endl;
    >
    > return 0;
    > }
    >
    > However, without the cast I get a compiler error:
    >
    > main.cpp:10: error: assuming cast to type ‘std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)’ from overloaded function
    >
    > which I interpret as:
    >
    > main.cpp:10: error: assuming cast to type ‘std::eek:stream& (*)(std::eek:stream&)’ from overloaded function
    >
    > ie.
    >
    > main.cpp:10: error: assuming cast to type ‘manip_t’ from overloaded function
    >
    > But my standard library ref defines:
    >
    > template<typename CharT, typename Traits> basic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)
    >
    > which also boils down to my manip_t... so why does my compiler
    > seem to think an explicit cast is necessary?


    A cast resolves the ambiguity of which overload you mean.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Apr 20, 2007
    #2
    1. Advertising

  3. Lionel B

    Lionel B Guest

    On Fri, 20 Apr 2007 13:03:09 +0200, Alf P. Steinbach wrote:

    > * Lionel B:
    >> The following code compiles ok and runs as expected:
    >>
    >> #include <iostream>
    >> #include <iomanip>
    >>
    >> int main()
    >> {
    >> typedef std::eek:stream& (*manip_t)(std::eek:stream&);
    >>
    >> manip_t x = std::endl;
    >>
    >> if (x==static_cast<manip_t>(std::endl))
    >> std::cout << "endl detected" << std::endl;
    >>
    >> return 0;
    >> }
    >>
    >> However, without the cast I get a compiler error:
    >>
    >> main.cpp:10: error: assuming cast to type ‘std::basic_ostream<char,
    >> std::char_traits<char> >& (*)(std::basic_ostream<char,
    >> std::char_traits<char> >&)’ from overloaded function
    >>
    >> which I interpret as:
    >>
    >> main.cpp:10: error: assuming cast to type ‘std::eek:stream&
    >> (*)(std::eek:stream&)’ from overloaded function
    >>
    >> ie.
    >>
    >> main.cpp:10: error: assuming cast to type ‘manip_t’ from overloaded
    >> function
    >>
    >> But my standard library ref defines:
    >>
    >> template<typename CharT, typename Traits> basic_ostream<CharT,
    >> Traits>& endl(basic_ostream< CharT, Traits >&)
    >>
    >> which also boils down to my manip_t... so why does my compiler seem to
    >> think an explicit cast is necessary?

    >
    > A cast resolves the ambiguity of which overload you mean.


    Ah, ok. Comeau online gives a clearer (to me) error message:

    "ComeauTest.c", line 10: error: cannot determine which instance of
    function
    template "std::endl" is intended
    if (x == std::endl)
    ^
    Thanks,

    --
    Lionel B
    Lionel B, Apr 20, 2007
    #3
  4. Lionel B

    James Kanze Guest

    On Apr 20, 12:56 pm, Lionel B <> wrote:
    > The following code compiles ok and runs as expected:


    > #include <iostream>
    > #include <iomanip>


    > int main()
    > {
    > typedef std::eek:stream& (*manip_t)(std::eek:stream&);


    > manip_t x = std::endl;


    > if (x==static_cast<manip_t>(std::endl))
    > std::cout << "endl detected" << std::endl;
    > return 0;
    > }


    > However, without the cast I get a compiler error:
    >
    > main.cpp:10: error: assuming cast to type ?std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)? from overloaded function


    > which I interpret as:
    >
    > main.cpp:10: error: assuming cast to type ?std::eek:stream& (*)(std::eek:stream&)? from overloaded function


    > ie.


    > main.cpp:10: error: assuming cast to type ?manip_t? from overloaded function


    > But my standard library ref defines:


    > template<typename CharT, typename Traits> basic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)


    > which also boils down to my manip_t...


    If that were true, then your code wouldn't be legal. I'll bet
    your manip_t is a type, and not a template.

    > so why does my compiler seem to think an explicit cast is
    > necessary?


    Because std::endl is a template, and it has to know which one to
    instantiation.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 20, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Davis

    Problem using I/O manipulators

    Paul Davis, Oct 10, 2003, in forum: C++
    Replies:
    6
    Views:
    462
    Mike Wahler
    Oct 11, 2003
  2. Andy
    Replies:
    1
    Views:
    347
    tom_usenet
    Nov 17, 2003
  3. Darius.Moos AT esigma-systems DOT de

    manipulators act strange under g++/gcc-2.96

    Darius.Moos AT esigma-systems DOT de, Apr 27, 2004, in forum: C++
    Replies:
    16
    Views:
    883
    Old Wolf
    Apr 28, 2004
  4. Replies:
    3
    Views:
    433
    Dietmar Kuehl
    Jan 18, 2005
  5. Dave
    Replies:
    2
    Views:
    2,987
Loading...

Share This Page