chrono_types Namespace Reference

Description

Namespace for custom make_shared implementation.

Classes

class  class_has_custom_new_operator
 Check if a class has a custom new operator. More...
 

Functions

template<typename T , typename... Args, typename std::enable_if< class_has_custom_new_operator< T >::value, int >::type = 0>
std::shared_ptr< T > make_shared (Args &&... args)
 Replacement for make_shared guaranteed to use operator new rather than placement new in order to avoid memory alignment issues (classes with members that are fixed-sized Eigen matrices have overriden operator new). More...
 

Function Documentation

◆ make_shared()

template<typename T , typename... Args, typename std::enable_if< class_has_custom_new_operator< T >::value, int >::type = 0>
std::shared_ptr< T > chrono_types::make_shared ( Args &&...  args)
inline

Replacement for make_shared guaranteed to use operator new rather than placement new in order to avoid memory alignment issues (classes with members that are fixed-sized Eigen matrices have overriden operator new).

For classes without members that are fixed-sized Eigen matrices (and hence do not have an overriden operator new), it is safe to default to using std::make_shared (no alignment issues).

Dynamic objects of classes with fixed-size vectorizable Eigen object members

  • Many of the Chrono classes now have members that are fixed-size vectorizable Eigen types. These classes overload their operator new to generate 16-byte-aligned pointers (using an Eigen - provided macro).
  • This requirement for aligned memory allocation has implications on creation of shared pointers. Indeed, std::make_shared uses placement new instead of operator new. To address this issue and preserve encapsulation (as much as possible), Chrono provides this for make_shared. This function will automatically infer if it can safely fallback on std::make_shared or else create a shared pointer with a mechanism that ensures use of aligned memory. As such, user code should always use chrono_types::make_shared as in
      auto my_body = chrono_types::make_shared<ChBody>();