https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120305

            Bug ID: 120305
           Summary: Cannot create a std::vector from a
                    std::ranges::transform_view.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aneris at disroot dot org
  Target Milestone: ---

The following code

--
#include <iostream>
#include <ranges>
#include <vector>

template<long unsigned N>
constexpr auto triangularNumbersTill()
{
    auto triangularNums = std::ranges::views::iota(1lu, N + 1) |
std::views::transform([](long unsigned a){return (a * (a + 1))/2;}); 
    return std::vector<long unsigned>{triangularNums.begin(),
triangularNums.end()};
}

int main() {

    for (auto x : triangularNumbersTill<10>()) {
        std::cout << x;
    }
}
--
does not compile on GCC 14, GCC 15 and GCC 16 (HEAD).

Options used were,
-O2 -Wall -Wextra -pedantic -std=c++20

The code compiles correctly if you replace all the long unsigned with just
unsigned though, which I find very peculiar, the code also compiles with
gnu++20 as the standard. I also tried with clang and it failed to compile it as
well (though, some versions like those provided by wandbox.org could compile it
properly... somehow.)

My GCC version:
sing built-in specs.
COLLECT_GCC=g++-15
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/15/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
15-20250404-0ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-15/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2,rust,cobol,algol68
--prefix=/usr --with-gcc-major-version-only --program-suffix=-15
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-15-n9L2J0/gcc-15-15-20250404/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-15-n9L2J0/gcc-15-15-20250404/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.0.1 20250404 (experimental) [master r15-9193-g08e803aa9be]
(Ubuntu 15-20250404-0ubuntu1)

-------------------------------------------------------------------------------
Here is the complete error.
-------------------------------------------------------------------------------
x.cpp: In instantiation of ‘constexpr auto triangularNumbersTill() [with long
unsigned int N = 10]’:
x.cpp:14:44:   required from here
   14 |     for (auto x : triangularNumbersTill<10>()) {
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~^~
x.cpp:9:83: error: no matching function for call to ‘std::vector<long unsigned
int>::vector(<brace-enclosed initializer list>)’
    9 | d::vector<long unsigned>{triangularNums.begin(), triangularNums.end()};
      |                                                                      ^
x.cpp:9:83: note: there are 12 candidates
In file included from /usr/include/c++/15/vector:68,
                 from x.cpp:3:
/usr/include/c++/15/bits/stl_vector.h:736:9: note: candidate 1: ‘template<class
_InputIterator, class> constexpr std::vector<_Tp,
_Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with
<template-parameter-2-2> = _InputIterator; _Tp = long unsigned int; _Alloc =
std::allocator<long unsigned int>]’
  736 |         vector(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:736:9: note: template argument
deduction/substitution failed:
In file included from /usr/include/c++/15/bits/move.h:37,
                 from /usr/include/c++/15/bits/exception_ptr.h:41,
                 from /usr/include/c++/15/exception:168,
                 from /usr/include/c++/15/ios:43,
                 from /usr/include/c++/15/bits/ostream.h:43,
                 from /usr/include/c++/15/ostream:42,
                 from /usr/include/c++/15/iostream:43,
                 from x.cpp:1:
/usr/include/c++/15/type_traits: In substitution of ‘template<bool _Cond, class
_Tp> using std::__enable_if_t = typename std::enable_if::type [with bool _Cond
= false; _Tp = void]’:
/usr/include/c++/15/bits/stl_iterator_base_types.h:252:11:   required by
substitution of ‘template<class _InIter> using std::_RequireInputIter =
std::__enable_if_t<((bool)std::is_convertible<typename
std::iterator_traits<_Iterator>::iterator_category,
std::input_iterator_tag>::value)> [with _InIter =
std::ranges::transform_view<std::ranges::iota_view<long unsigned int, long
unsigned int>, triangularNumbersTill<10>()::<lambda(long unsigned int)>
>::_Iterator<false>]’
  252 |     using _RequireInputIter =
      |           ^~~~~~~~~~~~~~~~~
/usr/include/c++/15/bits/stl_vector.h:734:9:   required from ‘constexpr auto
triangularNumbersTill() [with long unsigned int N = 10]’
  734 |                typename = std::_RequireInputIter<_InputIterator>>
      |                ^~~~~~~~
x.cpp:14:44:   required from here
   14 |     for (auto x : triangularNumbersTill<10>()) {
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/15/type_traits:143:11: error: no type named ‘type’ in ‘struct
std::enable_if<false, void>’
  143 |     using __enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~~~
x.cpp: In instantiation of ‘constexpr auto triangularNumbersTill() [with long
unsigned int N = 10]’:
x.cpp:14:44:   required from here
   14 |     for (auto x : triangularNumbersTill<10>()) {
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/15/bits/stl_vector.h:708:7: note: candidate 2: ‘constexpr
std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const
allocator_type&) [with _Tp = long unsigned int; _Alloc = std::allocator<long
unsigned int>; allocator_type = std::allocator<long unsigned int>]’
  708 |       vector(initializer_list<value_type> __l,
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:708:43: note: no known conversion for
argument 1 from ‘std::ranges::transform_view<std::ranges::iota_view<long
unsigned int, long unsigned int>, triangularNumbersTill<10>()::<lambda(long
unsigned int)> >::_Iterator<false>’ to ‘std::initializer_list<long unsigned
int>’
  708 |       vector(initializer_list<value_type> __l,
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/15/bits/stl_vector.h:689:7: note: candidate 3: ‘constexpr
std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&,
std::__type_identity_t<_Alloc>&) [with _Tp = long unsigned int; _Alloc =
std::allocator<long unsigned int>; std::__type_identity_t<_Alloc> =
std::allocator<long unsigned int>]’
  689 |       vector(vector&& __rv, const __type_identity_t<allocator_type>&
__m)
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:689:23: note: no known conversion for
argument 1 from ‘std::ranges::transform_view<std::ranges::iota_view<long
unsigned int, long unsigned int>, triangularNumbersTill<10>()::<lambda(long
unsigned int)> >::_Iterator<false>’ to ‘std::vector<long unsigned int>&&’
  689 |       vector(vector&& __rv, const __type_identity_t<allocator_type>&
__m)
      |              ~~~~~~~~~^~~~
/usr/include/c++/15/bits/stl_vector.h:670:7: note: candidate 4: ‘constexpr
std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const
allocator_type&, std::false_type) [with _Tp = long unsigned int; _Alloc =
std::allocator<long unsigned int>; allocator_type = std::allocator<long
unsigned int>; std::false_type = std::false_type]’
  670 |       vector(vector&& __rv, const allocator_type& __m, false_type)
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:670:7: note: candidate expects 3
arguments, 2 provided
/usr/include/c++/15/bits/stl_vector.h:665:7: note: candidate 5: ‘constexpr
std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const
allocator_type&, std::true_type) [with _Tp = long unsigned int; _Alloc =
std::allocator<long unsigned int>; allocator_type = std::allocator<long
unsigned int>; std::true_type = std::true_type]’
  665 |       vector(vector&& __rv, const allocator_type& __m, true_type)
noexcept
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:665:7: note: candidate expects 3
arguments, 2 provided
/usr/include/c++/15/bits/stl_vector.h:654:7: note: candidate 6: ‘constexpr
std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&,
std::__type_identity_t<_Alloc>&) [with _Tp = long unsigned int; _Alloc =
std::allocator<long unsigned int>; std::__type_identity_t<_Alloc> =
std::allocator<long unsigned int>]’
  654 |       vector(const vector& __x, const
__type_identity_t<allocator_type>& __a)
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:654:28: note: no known conversion for
argument 1 from ‘std::ranges::transform_view<std::ranges::iota_view<long
unsigned int, long unsigned int>, triangularNumbersTill<10>()::<lambda(long
unsigned int)> >::_Iterator<false>’ to ‘const std::vector<long unsigned int>&’
  654 |       vector(const vector& __x, const
__type_identity_t<allocator_type>& __a)
      |              ~~~~~~~~~~~~~~^~~
/usr/include/c++/15/bits/stl_vector.h:650:7: note: candidate 7: ‘constexpr
std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = long
unsigned int; _Alloc = std::allocator<long unsigned int>]’
  650 |       vector(vector&&) noexcept = default;
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:650:7: note: candidate expects 1
argument, 2 provided
/usr/include/c++/15/bits/stl_vector.h:631:7: note: candidate 8: ‘constexpr
std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp =
long unsigned int; _Alloc = std::allocator<long unsigned int>]’
  631 |       vector(const vector& __x)
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:631:7: note: candidate expects 1
argument, 2 provided
/usr/include/c++/15/bits/stl_vector.h:599:7: note: candidate 9: ‘constexpr
std::vector<_Tp, _Alloc>::vector(size_type, const value_type&, const
allocator_type&) [with _Tp = long unsigned int; _Alloc = std::allocator<long
unsigned int>; size_type = long unsigned int; value_type = long unsigned int;
allocator_type = std::allocator<long unsigned int>]’
  599 |       vector(size_type __n, const value_type& __value,
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:599:24: note: no known conversion for
argument 1 from ‘std::ranges::transform_view<std::ranges::iota_view<long
unsigned int, long unsigned int>, triangularNumbersTill<10>()::<lambda(long
unsigned int)> >::_Iterator<false>’ to ‘std::vector<long unsigned
int>::size_type’ {aka ‘long unsigned int’}
  599 |       vector(size_type __n, const value_type& __value,
      |              ~~~~~~~~~~^~~
/usr/include/c++/15/bits/stl_vector.h:586:7: note: candidate 10: ‘constexpr
std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp =
long unsigned int; _Alloc = std::allocator<long unsigned int>; size_type = long
unsigned int; allocator_type = std::allocator<long unsigned int>]’
  586 |       vector(size_type __n, const allocator_type& __a =
allocator_type())
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:586:24: note: no known conversion for
argument 1 from ‘std::ranges::transform_view<std::ranges::iota_view<long
unsigned int, long unsigned int>, triangularNumbersTill<10>()::<lambda(long
unsigned int)> >::_Iterator<false>’ to ‘std::vector<long unsigned
int>::size_type’ {aka ‘long unsigned int’}
  586 |       vector(size_type __n, const allocator_type& __a =
allocator_type())
      |              ~~~~~~~~~~^~~
/usr/include/c++/15/bits/stl_vector.h:572:7: note: candidate 11: ‘constexpr
std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = long
unsigned int; _Alloc = std::allocator<long unsigned int>; allocator_type =
std::allocator<long unsigned int>]’
  572 |       vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:572:7: note: candidate expects 1
argument, 2 provided
/usr/include/c++/15/bits/stl_vector.h:561:7: note: candidate 12: ‘constexpr
std::vector<_Tp, _Alloc>::vector() [with _Tp = long unsigned int; _Alloc =
std::allocator<long unsigned int>]’
  561 |       vector() = default;
      |       ^~~~~~
/usr/include/c++/15/bits/stl_vector.h:561:7: note: candidate expects 0
arguments, 2 provided

I have attached the .ii file.

Thank you for your work on GCC!

Reply via email to