C++ is_trivially_copyable check

2.5k views Asked by At

How to check whether or not C++ type is trivially copyable? I have a class, which uses memcpy and memcmp functions with specified template type T and I would like to fire assert for types, that are not safe to copy with memcpy. Is there any way to do that (with existing standard)?

3

There are 3 answers

1
aschepler On BEST ANSWER

No, not possible in C++98/C++03. Things like this are why <type_traits> was added to C++0x. Some of the features from <type_traits> can be implemented in C++03, often using the SFINAE principle, but several, including std::is_trivially_copyable<T>, will simply require built-in compiler support.

0
Maxim Egorushkin On

The closest thing is boost::is_pod<>.

2
Puppy On

There are type traits available for this in boost.

However, you're wasting your time- memcpying a type is not going to be faster than what your optimizer will produce with a copy constructor if the type is trivially copyable. Just use the copy constructor.