mardi 31 août 2021

C++11 type-trait to determine whether sizeof() makes sense

Is there a C++11 (only) type trait to determine whether it is sensible to call sizeof? My code is

void f_(const void* data, size_t sz);

template <typename T>
void f(const T& t)
{
   static_assert(std::is_pod<T>::value, "Can't call sizeof() on T."); // TODO ???
   f_(&t, sizeof(T));
}

std::is_pod looks pretty close; but not only is that depreciated in C++20, it's not clear to me that std::is_trivial and std::is_standard_layout are sufficient.

I want a call like f("abc"); to fail (the call should be f_("abc", strlen("abc")) not sizeof("abc")), while f(123) is OK. Also, I think

struct Foo final {int i; double d; };
// ...
Foo foo;
f(foo);

should also be OK, but probably not something "complicated."

Aucun commentaire:

Enregistrer un commentaire