[type traits] incomplete types

When some type traits, for instance is_base_of, are called with incomplete types, then they may silently "return" incorrect results. It would be better if they failed to compile with incomplete types. Here is a suggestion how that might be implemented: template<int> struct assert_complete0 {}; template<typename T> struct assert_complete : public assert_complete0<sizeof(T)> {}; template<typename B, typename T> is_base_of : public assert_complete<B>, public assert_complete<T> ... I have tested this with with VC 7.1, and it works. Will this work on other platforms? Are there any reasons not to add this check? Johan Råde CTO, Qlucore AB

Johan Råde wrote:
When some type traits, for instance is_base_of, are called with incomplete types, then they may silently "return" incorrect results. It would be better if they failed to compile with incomplete types.
Here is a suggestion how that might be implemented:
template<int> struct assert_complete0 {};
template<typename T> struct assert_complete : public assert_complete0<sizeof(T)> {};
template<typename B, typename T> is_base_of : public assert_complete<B>, public assert_complete<T> ...
I have tested this with with VC 7.1, and it works. Will this work on other platforms? Are there any reasons not to add this check?
Nope, I've fixed this with a couple of static_asserts in the SVN trunk version. John.

on Wed Sep 12 2007, "John Maddock" <john-AT-johnmaddock.co.uk> wrote:
I have tested this with with VC 7.1, and it works. Will this work on other platforms? Are there any reasons not to add this check?
Nope, I've fixed this with a couple of static_asserts in the SVN trunk version.
I once again recommend the use of the MPL assertion suite for improved error messages. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

David Abrahams wrote:
on Wed Sep 12 2007, "John Maddock" <john-AT-johnmaddock.co.uk> wrote:
I have tested this with with VC 7.1, and it works. Will this work on other platforms? Are there any reasons not to add this check?
Nope, I've fixed this with a couple of static_asserts in the SVN trunk version.
I once again recommend the use of the MPL assertion suite for improved error messages.
:-) Point taken, I'd need an "is_incomplete" predicate for that though, any takers? Or would it be a recipe for ODR violations even trying to implement such a beast? My gut feeling at present is that this is more trouble than it's worth... John.

John Maddock ha escrito:
David Abrahams wrote:
[...]
I once again recommend the use of the MPL assertion suite for improved error messages.
:-)
Point taken, I'd need an "is_incomplete" predicate for that though, any takers? Or would it be a recipe for ODR violations even trying to implement such a beast?
My gut feeling at present is that this is more trouble than it's worth...
If you only want the predicate for displaying purposes, maybe you can use something like this: template<typename T,std::size_t Size> struct is_complete:boost::mpl::bool_<Size>{}; BOOST_MPL_ASSERT((is_complete<foo,sizeof(foo)>)); Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

Joaquín Mª López Muñoz wrote:
If you only want the predicate for displaying purposes, maybe you can use something like this:
template<typename T,std::size_t Size> struct is_complete:boost::mpl::bool_<Size>{};
BOOST_MPL_ASSERT((is_complete<foo,sizeof(foo)>));
No that doesn't do it: you never get as far as the MPL error messages 'cos the sizeof(foo) fails to compile (this is what I'm using already to generate an error BTW). John.

John Maddock ha escrito:
Joaquín Mª López Muñoz wrote:
If you only want the predicate for displaying purposes, maybe you can use something like this:
template<typename T,std::size_t Size> struct is_complete:boost::mpl::bool_<Size>{};
BOOST_MPL_ASSERT((is_complete<foo,sizeof(foo)>));
No that doesn't do it: you never get as far as the MPL error messages 'cos the sizeof(foo) fails to compile (this is what I'm using already to generate an error BTW).
Oh, certainly, you're right, applying sizeof() to an incomplete type is illegal, sorry for not thinking about it hard enough. Funny thing is that in GCC 3.2 sizeof() of an incomplete type evaluates to 0, so that struct foo; BOOST_MPL_ASSERT((is_complete<foo,sizeof(foo)>)); actually gets to the MPL error msg, yielding is_complete.cpp:11: `sizeof' applied to incomplete type `foo' is_complete.cpp:11: conversion from `mpl_::failed************is_complete<foo, 0>::************' to non-scalar type `mpl_::assert<false>' requested is_complete.cpp:11: enumerator value for `mpl_assertion_in_line_11' not integer constant Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

on Thu Sep 13 2007, "John Maddock" <john-AT-johnmaddock.co.uk> wrote:
David Abrahams wrote:
on Wed Sep 12 2007, "John Maddock" <john-AT-johnmaddock.co.uk> wrote:
I have tested this with with VC 7.1, and it works. Will this work on other platforms? Are there any reasons not to add this check?
Nope, I've fixed this with a couple of static_asserts in the SVN trunk version.
I once again recommend the use of the MPL assertion suite for improved error messages.
:-)
Point taken, I'd need an "is_incomplete" predicate for that though
Oh, good point :( -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

John Maddock wrote:
Johan Råde wrote:
When some type traits, for instance is_base_of, are called with incomplete types, then they may silently "return" incorrect results. It would be better if they failed to compile with incomplete types.
I've fixed this with a couple of static_asserts in the SVN trunk version.
John.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thank you John. --Johan
participants (4)
-
David Abrahams
-
Joaquín Mª López Muñoz
-
Johan Råde
-
John Maddock