9

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)?

Viktor Dahl
  • 1,942
  • 3
  • 25
  • 36
axe
  • 217
  • 3
  • 9
  • 2
    You may be able to use the `is_pod` type trait. What is your definition of "safe to copy with `memcpy`"? If an object has a pointer data member that points to an owned dynamically allocated object, is that "safe to copy with `memcpy`"? – James McNellis Feb 14 '11 at 16:15
  • @James: The definition is, trivial copy constructor- that is, the type does not have a user-defined copy constructor, nor any base classes or data members with such. – Puppy Feb 14 '11 at 16:17
  • 1
    The definition from standard "A trivially copyable class is a class that: - has no non-trivial copy constructors (12.8), - has no non-trivial copy assignment operators (13.5.3, 12.8), - has a trivial destructor (12.4)." – axe Feb 14 '11 at 16:20
  • @DeadMG, @axe: I suspect @James knows both those definitions, but he still raises a very good question - is that *really* the correct thing to check for what you're trying to do? – aschepler Feb 14 '11 at 16:23
  • I'm trying to make it impossible for someone to use my class with types, that have non-trivial copy-constructor. – axe Feb 14 '11 at 16:24
  • This is a terrible thing to do, as far as I'm concerned- but that doesn't mean that the question isn't valid. – Puppy Feb 14 '11 at 16:30
  • Yes, I know what a trivially copyable class is; my question was more along the lines of: what are you trying to accomplish and are you sure that checking whether a type is trivially copyable is really what you want to do? – James McNellis Feb 14 '11 at 16:42
  • @DeadMG: You wrote: _This is a terrible thing to do, as far as I'm concerned_. Why is that terrible thing? Terrible would be to allow user to crash the program by using class with inappropriate types. – axe Feb 14 '11 at 17:40
  • @James McNellis: There is template class, which has to work ONLY with trivially copyable types by it's definition. That's what specification says. Of course there are reasons for that. – axe Feb 14 '11 at 17:43
  • @axe: Because, I mean, what could you possibly be doing that could require such a thing? – Puppy Feb 14 '11 at 21:07

3 Answers3

10

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
5

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Which type traits? is_pod is not exactly what I'm looking for, as Maxim pointed it is nearly the same. – axe Feb 14 '11 at 16:21
  • 1
    _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._ I'ts not for the performance, I just don't want my class to work for types with non-trivial copy constructor. I could specialize it for all integral types, however then it will not work for POD's. – axe Feb 14 '11 at 16:22
4

The closest thing is boost::is_pod<>.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271