Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal::same_cont_new_t should preserve allocators #160

Closed
Dobiasd opened this issue Nov 14, 2019 · 0 comments · Fixed by #253
Closed

internal::same_cont_new_t should preserve allocators #160

Dobiasd opened this issue Nov 14, 2019 · 0 comments · Fixed by #253

Comments

@Dobiasd
Copy link
Owner

Dobiasd commented Nov 14, 2019

Minimal example:

#include <fplus/fplus.hpp>
#include <boost/align/aligned_allocator.hpp>

template <typename T>
using aligned_vector = std::vector<T, boost::alignment::aligned_allocator<T, 32>>;

int main()
{
    typedef aligned_vector<int> AlignedIntVector;
    AlignedIntVector aligned_xs = {1,2,2,3,2};
    AlignedIntVector aligned_ys =
        fplus::transform([](auto x) { return x*x; }, aligned_xs);
}

internal::same_cont_new_t can not handle this:

g++:

error: conversion from ‘std::vector<int, std::allocator<int> >’ to non-scalar type ‘AlignedIntVector {aka std::vector<int, boost::alignment::aligned_allocator<int, 32ul> >}’ requested
         fplus::transform([](auto x) { return x*x; }, aligned_xs);

clang++:

error: no viable conversion from 'vector<[...],
      std::allocator<int>>' to 'vector<[...],
      boost::alignment::aligned_allocator<int, 32>>'
    AlignedIntVector aligned_ys =
                     ^

The current workaround is to provide the output container explicitly. fplus::transform_convert can be used for convenience:

#include <fplus/fplus.hpp>
#include <boost/align/aligned_allocator.hpp>

template <typename T>
using aligned_vector = std::vector<T, boost::alignment::aligned_allocator<T, 32>>;

int main()
{
    typedef aligned_vector<int> AlignedIntVector;
    AlignedIntVector aligned_xs = {1,2,2,3,2};
    AlignedIntVector aligned_ys =
        fplus::transform_convert<AlignedIntVector>(
            [](auto x) { return x*x; }, aligned_xs);
}

But since not only fplus::transform is affected, it would be very cool if internal::same_cont_new_t would support that.

Dobiasd added a commit that referenced this issue Nov 28, 2021
* Preserve allocators when deriving container types, fixes #160

* Remove debug test depending on boost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant