OutputIterator value_type

Discussion in 'C++' started by FabioAng, May 10, 2007.

  1. FabioAng

    FabioAng Guest

    Assuming I have this function:

    template<typename OutputIterator>
    void copy(const char* input, OutputIterator result)
    {
    while ( *input != NULL )
    {
    *result = *input;
    ++result;
    ++input;
    }
    }

    I would like to cast input value to output value in this way

    template<typename OutputIterator>
    void copy(const char* input, OutputIterator result)
    {
    while ( *input != NULL )
    {
    *result = (cast to output value type) *input;
    ++result;
    ++input;
    }
    }

    Any suggestions ?

    Regards,
    Fabio
    FabioAng, May 10, 2007
    #1
    1. Advertising

  2. On 10 Maj, 09:40, "FabioAng" <> wrote:
    > Assuming I have this function:
    >
    > template<typename OutputIterator>
    > void copy(const char* input, OutputIterator result)
    > {
    > while ( *input != NULL )
    > {
    > *result = *input;
    > ++result;
    > ++input;
    > }
    > }
    >
    > I would like to cast input value to output value in this way
    >
    > template<typename OutputIterator>
    > void copy(const char* input, OutputIterator result)
    > {
    > while ( *input != NULL )
    > {
    > *result = (cast to output value type) *input;
    > ++result;
    > ++input;
    > }
    > }


    Normally the output iterator has a value_type associated with it,
    which is the type you want. So what you want might be something like
    this (untested):

    *result = static_cast<typename OutputIterator::value_type>(*input);

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 10, 2007
    #2
    1. Advertising

  3. FabioAng

    David Harmon Guest

    On Thu, 10 May 2007 09:40:00 +0200 in comp.lang.c++, "FabioAng"
    <> wrote,
    >I would like to cast input value to output value in this way
    >
    > template<typename OutputIterator>
    > void copy(const char* input, OutputIterator result)
    > {
    > while ( *input != NULL )
    > {
    > *result = (cast to output value type) *input;


    *result =
    static_cast<std::iterator_traits<OutputIterator>::value_type>(*input);
    David Harmon, May 10, 2007
    #3
  4. FabioAng

    Pete Becker Guest

    FabioAng wrote:
    > Assuming I have this function:
    >
    > template<typename OutputIterator>
    > void copy(const char* input, OutputIterator result)
    > {
    > while ( *input != NULL )
    > {
    > *result = *input;
    > ++result;
    > ++input;
    > }
    > }
    >
    > I would like to cast input value to output value in this way
    >
    > template<typename OutputIterator>
    > void copy(const char* input, OutputIterator result)
    > {
    > while ( *input != NULL )
    > {
    > *result = (cast to output value type) *input;
    > ++result;
    > ++input;
    > }
    > }
    >


    Output iterators don't generally have a well-defined value type that you
    can get at. Technically, the requirement for an output iterator that
    applies here is that the expression *iter = val; must be valid and must
    copy val to the location in the output sequence that iter points to. If
    you can't assign the value, then the iterator just isn't the right type.

    Take a step back, and describe the problem that you're trying to solve,
    rather than an attempted solution that didn't work.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, May 10, 2007
    #4
  5. FabioAng

    Pete Becker Guest

    David Harmon wrote:
    > On Thu, 10 May 2007 09:40:00 +0200 in comp.lang.c++, "FabioAng"
    > <> wrote,
    >> I would like to cast input value to output value in this way
    >>
    >> template<typename OutputIterator>
    >> void copy(const char* input, OutputIterator result)
    >> {
    >> while ( *input != NULL )
    >> {
    >> *result = (cast to output value type) *input;

    >
    > *result =
    > static_cast<std::iterator_traits<OutputIterator>::value_type>(*input);
    >
    >


    This doesn't work: an output iterator's value_type isn't necessarily
    meaningful. For example, ostream_iterator's value_type is void.

    The reason for this is that there are typically many different types
    that can be assigned through an output iterator, and picking one of them
    wouldn't be particularly helpful. The relevant requirement for an output
    iterator is that *result = val must be valid.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, May 10, 2007
    #5
    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. Chyi Pin Lim

    value_type of a back_insert_iterator

    Chyi Pin Lim, Dec 5, 2003, in forum: C++
    Replies:
    9
    Views:
    512
    ppLiu_china
    Dec 11, 2003
  2. chris
    Replies:
    8
    Views:
    486
    Tom Widmer
    Dec 10, 2004
  3. cesco
    Replies:
    5
    Views:
    367
    Maxim Yegorushkin
    Feb 10, 2006
  4. Andrew
    Replies:
    4
    Views:
    365
    Victor Bazarov
    Mar 7, 2006
  5. Ricky65
    Replies:
    1
    Views:
    278
    Ricky65
    Sep 8, 2011
Loading...

Share This Page