1

I want to create a class Entity that can be templated for owning and non-owning string types (e.g. std::string or std::string_view). In both cases though there is internal datastructure to be allocated (vector) which needs an allocator, however in the string case I want to use the same allocator for the internal data structure as the string and have the Entity constructor forward this allocator to the string object. In the string_view case, the constructor of string_view doesn't take an allocator, so I would have to specialize the call to the constructor to NOT forward the allocator but initialize empty. I can get close with uses_allocator_construction_args but I can't unpack the tuple into the parameter list, what can I do instead?

Here's what I have and want to do:

Demo

#include <iostream>
#include <vector>
#include <string>
#include <string_view>

template <typename Allocator>
struct view_adapter {
    using string_type = std::string_view;
    using allocator_type = Allocator;
};

template <typename StringType>
struct string_type_of_or {
    using type = StringType;
};

template <typename Allocator>
struct string_type_of_or<view_adapter<Allocator>> {
    using type = view_adapter<Allocator>::string_type;
};


template <typename StringType>
struct Entity {
    using allocator_type = StringType::allocator_type;
    using string_type = string_type_of_or<StringType>::type;

    Entity(allocator_type allocator = {})
        : vec{ allocator }
        , str{ uses_allocator_construction_args<StringType>(allocator)... } // <---- How to write this?
    {}

    StringType str;
    std::vector<StringType,
        typename std::allocator_traits<allocator_type>::template rebind_alloc<StringType>> vec;
};


int main() {
    Entity<std::string> my_val;
    Entity<view_adapter<std::allocator<std::string_view::value_type>>> my_view;
}

1 Answer 1

2

Use std::make_obj_using_allocator<T>

https://godbolt.org/z/sTffGn1ez:

    Entity(allocator_type allocator = {})
        : vec{ allocator }
        , str( std::make_obj_using_allocator<string_type>(allocator) )
    {}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm wondering how I didn't consider using the copy constructor, since it is optimized out anyway.., thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.