missing optimization opportunity for const std::vector compared to std::array

for gcc-discussion see: 58483 – missing optimization opportunity for const std::vector compared to std::array

is there a (good) reason not to remove the new/delete combination when the result is already known?

#include <vector>
#include <numeric>
#include <array>

static int calc(const std::array<int,3> p_ints, const int& p_init)
//static int calc(const std::vector<int> p_ints, const int& p_init)
 return std::accumulate(p_ints.begin(), p_ints.end(), p_init);

int main()
 const int result = calc({10,20,30},100);
 return result;


gcc.godbolt x86-64 clang trunk, with -O2

with std::array

main: # @main
mov eax, 160

with std::vector

main: # @main
push   rax
mov    edi, 12
call   operator new(unsigned long)
movabs rcx, 85899345930
mov    qword ptr [rax], rcx
mov    dword ptr [rax + 8], 30
mov    rdi, rax
call   operator delete(void*)
mov    eax, 160
pop    rcx

so i've rembered correct - Richard Smith already fixed that with an patch to LibC++ years ago


see post ~Jun 04, 2014; 12:16am

this post informed about the patch

what happend to this optimization - was it removed or broken over time?
or is it just not possible to test the behavior on gcc.godbolt.org with the laste clang, missing flags or something?

its is fixed: forgot to use libc+++ at all

godbolt: -O2 -stdlib=libc++

is the answer