C++ is_trivially_copyable check
-
14-11-2019 - |
题
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)?
解决方案
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.
其他提示
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.
The closest thing is boost::is_pod<>.