Skip to content

Instantly share code, notes, and snippets.

@jacquelinekay
Created October 15, 2015 01:04
Show Gist options
  • Save jacquelinekay/723d8c2e03efbae39bbb to your computer and use it in GitHub Desktop.
Save jacquelinekay/723d8c2e03efbae39bbb to your computer and use it in GitHub Desktop.
allocator template example in rclcpp
template<typename MessageT, template<typename> class Alloc = std::allocator>
class Publisher : public PublisherBase
{
// ...
Alloc<MessageT> message_allocator_;
// Publisher doesn't have any STL structures, but suppose it did (or think about the case for services):
std::vector<AnotherType, Alloc<AnotherType>> my_vector_;
// ...
void my_function() {
// instead of MessageT * msg = new MessageT;
MessageT * msg = std::allocator_traits<Alloc<MessageT>>::allocate(message_allocator_, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment