Topic: Can std::iter_swap call swap for efficiency?


Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/08/18
Raw View
> Darin Adler wrote:
>>         template <> void swap(X&, X&) { cout << "Bad news!\n"; }

Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
> What would that mean ? (Cite std ref please)

A quick note: My original question has been answered, although not on this
newsgroup. Someone forwarded the suggestion that std::iter_swap() be based
on std::swap() to the C++ committee. My library vendor liked the suggestion
so much that he has already made the change. I'm happy about all that.

I assume that Valentin is challenging the line above because he thinks it's
not standard C++. The part of the standard that made me think I could write
it is section 17.4.3.1 [lib.reserved.names]. It says, "A program may add
template specializations for any standard library template to namespace std.
Such a specialization (complete or partial) of a standard library template
results in undefined behavior unless the declaration depends on a
user-defined name of external linkage and unless the specialization meets
the standard library requirements for the original template."

I believe what I wrote above is a specialization of the standard swap
function template, defined in <algorithm>. Since it takes parameters of type
X&, it depends on a user-defined name of external linkage, X.

As Lisa Lippincott pointed out, it's arguable whether sending text to cout
meets the standard library requirements for the original template, swap.
Perhaps that's what you were driving at, Valentin?

    -- Darin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Lisa Lippincott <lisa_lippincott@advisories.com>
Date: 1999/08/17
Raw View
Darin Adler <darin@bentspoon.com> asks:
> Does the standard allow the implementation of the std::iter_swap template to
> call instances of the std::swap template?

By my reading, it does.

> I can't figure out if it would be conforming for an implementation to call
> swap from iter_swap. For example, if I specialize swap for one of my
> classes, and then use iter_swap, am I guaranteed to not get a call to my
> swap specialization. For example:
>
>     #include <algorithm>
>     #include <iostream>
>
>     class X { };
>
>     namespace std {
>         template <> void swap(X&, X&) { cout << "Bad news!\n"; }
>     }
>
>     int main()
>     {
>         X a[2];
>         std::iter_swap(&a[0], &a[1]);
>     }
>
> It seems that an implementation must not call swap from iter_swap to avoid
> writing "Bad news!" on some implementations but not on others.

By 17.4.3.1 [lib.reserved.names] paragraph 1, your specialization must
meet the requirements for std::swap.  Since the exact meaning of
"exchanges the values" isn't entirely clear in this case, I can't
tell whether it does.

But if it does meet the requirements, an implementation of iter_swap
that uses it appropriately will also meet its requirements.  Otherwise,
your program has undefined behavior, and an implementation that calls
your specialization is still correct.

To sum up, I'd say it's a quality of implementation issue, and you should
encourage our compiler vendor to call std::swap.

                                                   --Lisa Lippincott
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/08/17
Raw View
Darin Adler wrote:

>         template <> void swap(X&, X&) { cout << "Bad news!\n"; }

What would that mean ? (Cite std ref please)

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/08/13
Raw View
Does the standard allow the implementation of the std::iter_swap template to
call instances of the std::swap template? Here's why I ask:

I know that the swap template can be (and usually is) specialized for
classes from the library that can do a more efficient swap without requiring
a temporary object. But it seems that iter_swap doesn't have the
corresponding optimization.

I can't figure out if it would be conforming for an implementation to call
swap from iter_swap. For example, if I specialize swap for one of my
classes, and then use iter_swap, am I guaranteed to not get a call to my
swap specialization. For example:

    #include <algorithm>
    #include <iostream>

    class X { };

    namespace std {
        template <> void swap(X&, X&) { cout << "Bad news!\n"; }
    }

    int main()
    {
        X a[2];
        std::iter_swap(&a[0], &a[1]);
    }

It seems that an implementation must not call swap from iter_swap to avoid
writing "Bad news!" on some implementations but not on others.

I'd write a defect report if I understood this better. It seems bad that
calling iter_swap on two vector* is required use a temporary vector instead
of using vector::swap.

    -- Darin


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]