Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types/is trivially copyable"

From cppreference.com
< cpp‎ | types
m (inherit template)
(simplify the example)
Line 32: Line 32:
 
===Example===
 
===Example===
 
{{example cpp
 
{{example cpp
  | Demonstrates serialization of trivially copyable types: an integer and an array of char. Note that '\0' (the fifth byte in the object representation of "test") is output  before the final endline.
+
  |  
 
  | code=
 
  | code=
 
#include <iostream>
 
#include <iostream>
#include <string>
 
 
#include <type_traits>
 
#include <type_traits>
  
template<typename T>
+
struct A {
typename std::enable_if<std::is_trivially_copyable<T>::value, std::string>::type
+
    int m;
serialize(const T& val)
+
}
{
+
 
     const char* beg = reinterpret_cast<const char*>(&val);
+
struct B {
    const char* end = beg + sizeof val;
+
     B(const B&) {}
     return std::string(beg, end);
+
}
 +
 
 +
struct C {
 +
     virtual void foo();
 
}
 
}
  
 
int main()
 
int main()
 
{
 
{
     std::cout << serialize(0x2a2b2c2d)) << '\n';
+
     std::cout << std::boolalpha;
     std::cout << serialize("test") << '\n';
+
    std::cout << std::is_trivial<A>::value << '\n';
     // compile time error: string is not trivially-copyable
+
     std::cout << std::is_trivial<B>::value << '\n';
    // std::cout << serialize(std::string()) << '\n';
+
     std::cout << std::is_trivial<C>::value << '\n';
 
}
 
}
 
  | output=
 
  | output=
-,+*
+
true
test
+
false
 +
false
 
}}
 
}}

Revision as of 10:39, 26 October 2011

Template:cpp/types/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <type_traits>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
template< class T >
struct is_trivially_copyable;
</td>

<td class="t-dcl-nopad"> </td> <td > Template:mark c++11 feature </td> </tr> Template:ddcl list end

If T is a trivially copyable type, provides the member constant value equal Template:cpp. For any other type, value is Template:cpp.

The only trivially copyable types are scalar types, trivially copyable classes, and arrays of such types/classes (possibly cv-qualified).

A trivially copyable class is a class that

1. Has no non-trivial copy constructors (this also requires no virtual functions or virtual bases)

2. Has no non-trivial move constructors

3. Has no non-trivial copy assignment operators

4. Has no non-trivial move assignment operators

5. Has a trivial destructor

Contents

Inherited from std::integral_constant

Member constants

value
[static]
true if T is a trivially copyable type , false otherwise
(public static member constant)

Member functions

operator bool
converts the object to bool, returns value
(public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>

Notes

Objects of trivially-copyable types are the only C++ objects that may be safely copied with Template:cpp or serialized to/from binary files with write()::std::ofstream/read()::std::ifstream. In general, a trivially copyable type is any type for which the underlying bytes can be copied to an array of char or unsigned char and into a new object of the same type, and the resulting object would have the same value as the original.

Example

Template:example cpp