k06a / boolinq

Simplest C++ header-only LINQ template library
MIT License
628 stars 79 forks source link

Example appears to crash on MSVC 2017 #34

Closed sharpe5 closed 5 years ago

sharpe5 commented 5 years ago

I've been using C# and LINQ for years, so this library looks just fantastic, as I am using C++ now and really miss how expressive C# is.

I run this example code:

int src[] = {1,2,3,4,5,6,7,8};
std::vector<int> dst = boolinq::from(src)
    .where([](int a) { return a % 2 == 1; }) // 1,3,5,7
    .select([](int a) { return a * 2; }) // 2,6,10,14
    .where([](int a) { return a > 2 && a < 12; }) // 6,10
    .toStdVector();

I get an exception in this function:

////////////////////////////////////////////////////////////////
// Linq Creators
////////////////////////////////////////////////////////////////

template<typename T>
Linq<std::pair<T, T>, typename std::iterator_traits<T>::value_type> from(const T & begin, const T & end)
{
    return Linq<std::pair<T, T>, typename std::iterator_traits<T>::value_type>(
        std::make_pair(begin, end),
        [](std::pair<T, T> &pair) {
            if (pair.first == pair.second) {
                throw LinqEndException();
            }
            return *(pair.first++);
        }
    );
}

The exception is on this line:

return *(pair.first++);

The exception is:

Exception thrown at 0x00007FFB4A13A388 in DemoRange.exe: Microsoft C++ exception: boolinq::LinqEndException at memory location 0x000000807813CCF4.

Environments tried in:

MSVC 2017 15.9.16 + MSVC with "ISO C++17 Standard (/std:c++17)"
MSVC 2017 15.9.16 + Intel Compiler v19.0 
k06a commented 5 years ago

The library works on exceptions, actually on a single exception. You should skip it, it should be handled in a proper way.

k06a commented 5 years ago

I mean Visual Studio for some reason decided to show you not a really unhandled exception but handled one.

sharpe5 commented 5 years ago

Ah, that makes sense. I'll try skipping the exception.

I'm from a C# background, so that exception looked like a memory access violation to me. The exception appears to occur on a pointer access. It's probably an issue with the MSVC debugger.

On Thu, 3 Oct 2019, 14:15 Anton Bukov, notifications@github.com wrote:

I mean Visual Studio for some reason decided to show you not a really unhandled exception but handled one.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/k06a/boolinq/issues/34?email_source=notifications&email_token=AAJ3FJBPYT2YJ3RSUGMSHETQMXV47A5CNFSM4I43YGS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAIFC5Q#issuecomment-537940342, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJ3FJC2RUUG6MBNJXYXKNTQMXV47ANCNFSM4I43YGSQ .

sharpe5 commented 5 years ago

Closing as not an issue. Thanks for the quick response, and thank you for the fantastic work on the library.

k06a commented 5 years ago

@sharpe5 you're welcome! Tell me which functions are missing and you wish to see, I'll try to deliver it.