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

Non-copyable types of elements are not supported #22

Open
AStepaniuk opened this issue Dec 9, 2016 · 7 comments
Open

Non-copyable types of elements are not supported #22

AStepaniuk opened this issue Dec 9, 2016 · 7 comments

Comments

@AStepaniuk
Copy link

This will not work:

std::vector<std::unique_ptr<MyType>> myVector;
boolinq::from(xmlLogicFragments)
    .select([](const auto& uniquePtr) { return uniquePtr.get(); })
    .toVector();

I.e. conversion from the vector of the unique ptrs to the vector of raw ptrs does not work.
The vector of unique ptrs is convenient to have RAII for the collection of items, as it is suggested e.g. here http://stackoverflow.com/questions/27460377/why-is-using-vector-of-pointers-considered-bad

Also there are possible the other situations, when non-copyable elements can be used.

Would that be possible if boolinq is working with the references to the elements in such cases instead?

@k06a
Copy link
Owner

k06a commented Dec 9, 2016

Do you have compilation or execution problems with following code sample? I can't check it right now.

@AStepaniuk
Copy link
Author

It produces compilation error.

In the code:

template<typename T, typename TI>
LinqObj<Enumerator<T,TI> > from(TI begin, TI end)
{
    return Enumerator<T,TI>([=](TI & iter){
        return (iter == end) ? throw EnumeratorEndException() : *(iter++);   // <<< here the error occurs
    }, begin);
}

The error message is:

Error C2280 'std::unique_ptr<MyType,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
\lib\boolinq\boolinq\boolinq.h 734

As I see it, Enumerator<T,TI>() constructor takes 2nd parameter by value, i.e. wants the copy of the parameter. That won't compile if parameter is non-copyable. That is the case for unique_ptr.

Another similar issue here is that lambda wants to return *(iter++). That requires copying the return value.

It seems to me that working with the references would work here. I'm not sure though as I had no chance to get the lib working with references.

@k06a
Copy link
Owner

k06a commented Dec 8, 2017

@AStepaniuk some methods work by copying elements in data structures like set/map. How do you think to deal with them?

@k06a
Copy link
Owner

k06a commented Jul 2, 2019

@AStepaniuk just rolled out new version 3.0.0 and started experimenting with unique_ptr: #29

@Gargony
Copy link

Gargony commented May 21, 2021

Will this ever be solved here?

@k06a
Copy link
Owner

k06a commented May 21, 2021

@Gargony not sure, any ideas on the solution?

@Gargony
Copy link

Gargony commented May 24, 2021

@k06a
Linq on c++ is most hardly, but when I was make my own implementation non-copyable type and no virtual methods it was first priority.
TDD was start from this requires.

I solved it through std::ref
Somthing like this:

std::vector<std::unique_ptr<MyType>> myVector;
linq::From(myVector)
    .Select<const MyType&>([](const auto& uniquePtr) { return std::ref(*uniquePtr.get()); })

Here you cannot use vector, because vector not supports refs.
But you can use pointers

std::vector<std::unique_ptr<MyType>> myVector;
std::vector<const MyType*> myVector2 =
linq::From(myVector)
    .Select<const MyType*>([](const auto& uniquePtr) { return uniquePtr.get(); })
    .ToVector();

for changes:

std::vector<std::unique_ptr<MyType>> myVector;
std::vector<MyType*> myVector2 =
linq::From(myVector)
    .Select<MyType*>([](auto& uniquePtr) { return uniquePtr.get(); })
    .ToVector();

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

No branches or pull requests

3 participants