Topic: Anonymous types and the standard library


Author: ianmcc@physik.rwth-aachen.de (Ian McCulloch)
Date: Thu, 18 Aug 2005 22:53:09 GMT
Raw View
Hi,

Is the following program well-formed C++ ?

#include <vector>
#include <algorithm>  // or substitute some other part of the standard lib

template <typename T, typename U>
struct Plus {};

template <typename T, typename U>
typename Plus<T, U>::result_type
operator+(T const& x, U const& y);

struct foo
{
   int n;
   foo() : n(0) {}
};
bool operator<(foo const& x, foo const& y) { return x.n < y.n; }

int main()
{
   std::vector<foo> x(10);
   std::sort(x.begin(), x.end());
}



In case it isn't clear, the issue here is
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#488 .  Without
a resolution of this DR, then at least the gcc devs argue
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20589) that something like

int main()
{
   enum { X = 5 }
   int y = X+1;
}

is ill-formed.  But my question is, given the above templated operator+, is
an arbitrary call to a standard library function ill-formed too?  Gcc 3.3.6
fails with the original version with

/usr/lib/gcc-lib/x86_64-pc-linux-gnu/3.3.6/include/g++-v3/bits/stl_algo.h:
In
   function `void std::__final_insertion_sort(_RandomAccessIter,
   _RandomAccessIter) [with _RandomAccessIter =
   __gnu_cxx::__normal_iterator<foo*, std::vector<foo, std::allocator<foo> >
>]
   ':
/usr/lib/gcc-lib/x86_64-pc-linux-gnu/3.3.6/include/g++-v3/bits/stl_algo.h:2182:
instantiated from `void std::sort(_RandomAcces          sIter,
_RandomAccessIter) [with _RandomAccessIter =
__gnu_cxx::__normal_iterator<foo*, std::vector<foo, std::allocator<foo> >
>]          '
testenum.cpp:21:   instantiated from here
/usr/lib/gcc-lib/x86_64-pc-linux-gnu/3.3.6/include/g++-v3/bits/stl_algo.h:2060:
error: template-argument
   `std::<anonymous enum>' uses anonymous type


Regards,
Ian McCulloch

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]