[c++0x] Considering better error messages for templates.
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Dec 10 23:33:00 GMT 2009
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
More information about the Libstdc++
mailing list