cpp-lazy icon indicating copy to clipboard operation
cpp-lazy copied to clipboard

Capturing lambdas and compile errors with MSVC

Open adihodos opened this issue 1 year ago • 3 comments

I have the following code that compiles with GCC/Clang but produces compilation error on MSVC (Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34809 for x64):

    containers::vector<vec2i32> slabs_visible_current_frame =
        lz::chain(slabs_current_frame)
            .filter([half_size = static_cast<float>(_terrain_params.size), ray_dir, ray_origin = cam_pos_xz_plane](
                        vec2i32 slab_center) {
                const vec2f32 slab_box_min = vec2f32{ slab_center } - vec2f32{ half_size };
                const vec2f32 slab_box_max = vec2f32{ slab_center } + vec2f32{ half_size };
                return ray_aabb_intersect(ray_origin, ray_dir, slab_box_min, slab_box_max);
            })
            .to<containers::vector<vec2i32>>(std::execution::seq, MemoryArenaAllocator<vec2i32>{ *scratchpad.arena });

The errors:

18>    see declaration of 'B5::Terrain::loop_event::<lambda_8>'
18>    C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\FunctionContainer.hpp(17,86):
18>    the template instantiation context (the oldest one first) is
18>        C:\projects\c_c_plus_plus\xray-ng\src\samples\vulkan\code\terrain.cc(873,14):
18>        see reference to class template instantiation 'lz::IterView<lz::detail::FilterIterator<Iterator,B5::Terrain::loop_event::<lambda_8>,std::execution::sequenced_policy>>' being compiled
18>        with
18>        [
18>            Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types<xray::math::scalar2<int32_t>>>>
18>        ]
18>        C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\Lz.hpp(90,39):
18>        see reference to class template instantiation 'lz::detail::BasicIteratorView<Iterator>' being compiled
18>        with
18>        [
18>            Iterator=lz::detail::FilterIterator<std::_List_const_iterator<std::_List_val<std::_List_simple_types<xray::math::scalar2<int32_t>>>>,B5::Terrain::loop_event::<lambda_8>,std::execution::sequenced_policy>
18>        ]
18>        C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\BasicIteratorView.hpp(226,8):
18>        see reference to class template instantiation 'lz::detail::FilterIterator<Iterator,B5::Terrain::loop_event::<lambda_8>,std::execution::sequenced_policy>' being compiled
18>        with
18>        [
18>            Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types<xray::math::scalar2<int32_t>>>>
18>        ]
18>        C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\iterators\FilterIterator.hpp(55,47):
18>        see reference to class template instantiation 'lz::detail::FunctionContainer<UnaryPredicate>' being compiled
18>        with
18>        [
18>            UnaryPredicate=B5::Terrain::loop_event::<lambda_8>
18>        ]
18>        C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\FunctionContainer.hpp(17,5):
18>        while compiling class template member function 'lz::detail::FunctionContainer<UnaryPredicate>::FunctionContainer(std::false_type)'
18>        with
18>        [
18>            UnaryPredicate=B5::Terrain::loop_event::<lambda_8>
18>        ]
18>            C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\FunctionContainer.hpp(91,54):
18>            see the first reference to 'lz::detail::FunctionContainer<UnaryPredicate>::FunctionContainer' in 'lz::detail::FunctionContainer<UnaryPredicate>::FunctionContainer'
18>        with
18>        [
18>            UnaryPredicate=B5::Terrain::loop_event::<lambda_8>
18>        ]
18>            C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\iterators\FilterIterator.hpp(55,57):
18>            see the first reference to 'lz::detail::FunctionContainer<UnaryPredicate>::FunctionContainer' in 'lz::detail::FilterIterator<Iterator,B5::Terrain::loop_event::<lambda_8>,std::execution::sequenced_policy>::FilterIterator'
18>        with
18>        [
18>            UnaryPredicate=B5::Terrain::loop_event::<lambda_8>
18>        ]
18>        and
18>        [
18>            Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types<xray::math::scalar2<int32_t>>>>
18>        ]
18>C:\Users\Adi\AppData\Local\CPM_SOURCE_CACHE\cpp-lazy\cc4f387d43c3d23ab158939607e6ab5afcbbfa1e\include\Lz\detail\FunctionContainer.hpp(18,42): error C2338: static_assert failed: 'Please use std::function instead of a lambda in this case, because lambda's are not default constructible pre C++20'

adihodos avatar Mar 17 '25 19:03 adihodos

Hi, thanks for reaching out and thanks for the issue. Currently, the dev branch is under heavy maintenance and performance improvements & revisioning the library. So I guess you could do the following things while I work on a fix for this on the master branch (which will be replaced by the dev branch within now and 1 or 2 months or so):

  • use std::function for now as a workaround
  • use the dev branch which is still under heavy development but 80/90% is done, include Lz/filter.hpp and use the following code (this is an approximation)
#include <Lz/filter.hpp>

auto container = your_container | lz::filter([](){}) | lz::to<container>(args…);

I hope the code above will not give the same error, otherwise, I’d have to check for the repro.

  • or wait a little while till I work out a fix for this, but cannot give you an estimate (yet)

ICreateYank avatar Mar 17 '25 23:03 ICreateYank

Fixed in https://github.com/Kaaserne/cpp-lazy/pull/169/commits/ed5040b478472ceef480839f509a6ffa4f283f58.

ICreateYank avatar May 22 '25 10:05 ICreateYank

I'd like to add that this was properly fixed in 5dc2e2ef12614ea58546946199db2abd4bb114b4

ICreateYank avatar Jun 05 '25 16:06 ICreateYank