This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
P1285R0 and type_traits hardening discussion
- From: Antony Polukhin <antoshkka at gmail dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Wed, 10 Apr 2019 23:25:23 +0300
- Subject: P1285R0 and type_traits hardening discussion
Hi,
https://wg21.link/P1285R0 allows UB for some type traits that depend on an
incompletely-defined object type T and yield different result for complete
and incomplete types.
I've tried to implement a compile time check for such traits. With the
check instead of UB and hard detectable errors we get a static assertion
for the most common cases of misuse. Draft version of the check with some
tests https://gcc.godbolt.org/z/FVJEkz
Hopefully all the corner cases with references and forward declared enums
are bypassed.
Applying the checks to the type traits are quite trivial:
/// is_move_constructible
template<typename _Tp>
struct is_move_constructible
: public __is_move_constructible_impl<_Tp>
{
+ static_assert(std::__is_size_known(__type_identity<_Tp>{}),
+ "_Tp must not be a forward declared class");
};
// tests
enum class incomplete_enum: int;
enum incomplete_enum2: int;
constexpr bool res1 = std::is_move_constructible<incomplete_enum>::value;
// OK
constexpr bool res2 = std::is_move_constructible<incomplete_enum2>::value;
// OK
struct X;
constexpr bool res = std::is_move_constructible<X>::value; // ill-formed
Questions:
1) Is there an interest in such addition to the libstdc++?
2) The check does not do memoization of the result, so it may produce
different results with the same template arguments. This works on GCC and
Clang, but is this feature desired?
3) Any other suggestions or comments?