This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [c++0x] Considering better error messages for templates.
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Germán Diago <germandiago at gmail dot com>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Thu, 10 Dec 2009 23:33:28 +0000
- Subject: Re: [c++0x] Considering better error messages for templates.
- References: <b798010f0912101501n7bcf642ay36e088910237b917@mail.gmail.com>
2009/12/10 Germán Diago:
>
> template <typename T>
> class set
> {
> public:
> ?static_assert(
> ? ? ?sizeof(less_than_test((T*)0)) == 1,
> ? ? ?"type T must provide operator<");
> };
>
> The g++ 4.4 compiler then gives the following output on the original
> variable declaration:
>
> test.cpp: In instantiation of set<my_type>
> test.cpp:21: ? instantiated from here
> test.cpp:13: error: static assertion failed:
> ? ?"type T must provide operator<"
>
> It works with function templates too.
For the specific case of std::set you'd need to put the static_assert
in less<T>::operator() because that's where operator< is required,
std::set doesn't require LessThanComparable. So you don't get the
error any earlier than the error about a missing operator< (which is
only when the comparison function is instantied)
t.cc: In member function ‘bool less<T>::operator()(const T&, const T&)
const [with T = Foo]’:
t.cc:21:31: instantiated from ‘void set<T, Cmp>::insert(const T&)
[with T = Foo, Cmp = less<Foo>]’
t.cc:29:17: instantiated from here
t.cc:12:9: error: static assertion failed: "T is not LessThanComparable"
t.cc:13:20: error: no match for ‘operator<’ in ‘l < r’
Also, 'T' is not the name of the type, but that's not a big deal, the
lines above show T = Foo.
It is still an improvement, but not as dramatic as one might hope.
There are other places where this technique could be used with more
success (patches welcome :-)
Jonathan