Query: Use of std::vector<const something> in Chromium?

1,368 views
Skip to first unread message

Harald Alvestrand

unread,
Feb 5, 2024, 12:34:03 PM2/5/24
to c...@chromium.org
Hi,
I recently got pinged on https://crbug.com/webrtc/15829 - an external user of WebRTC had his compile break on this line:

std::vector<const int> foo = {1, 2, 3};

It seems that const inside a std::vector has been disallowed in the C++ spec for a while. But the chromium compile didn't catch it, and some grepping found quite a bit of usage inside Chrome.

Is this something that someone's working on fixing?

Harald

Peter Kasting

unread,
Feb 5, 2024, 2:16:40 PM2/5/24
to Harald Alvestrand, cxx
I'm not aware of any effort to find or fix this.

I would file a bug, link to the relevant c++ spec bits, and if you have time send some patches to improve things.

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAOqqYVFA8Z7HBcSwR%2B%3Dq6kPEBiO4AQAtf2y5hXTCC4L6oDK_7g%40mail.gmail.com.

Hans Wennborg

unread,
Feb 5, 2024, 2:43:34 PM2/5/24
to Peter Kasting, Harald Alvestrand, cxx, Nico Weber

Adam Rice

unread,
Feb 6, 2024, 1:10:14 AM2/6/24
to Hans Wennborg, Peter Kasting, Harald Alvestrand, cxx, Nico Weber
Very odd that you can just wrap your const value in a struct and the problem goes away:


The restriction seems very artificial.

Tomasz Sniatowski

unread,
Feb 6, 2024, 11:27:02 AM2/6/24
to Harald Alvestrand, Peter Kasting, cxx
I recall this popping up from time to time, breaking on libstdc++, and getting fixed as part of the libstdc++ community effort in https://issues.chromium.org/issues/41455655 (formerly known as crbug.com/957519). So there shouldn't be very many "old" instances, at least in //content and below.

--
Tomasz Śniatowski

From: 'Peter Kasting' via cxx <c...@chromium.org>
Sent: Monday, February 5, 2024 20:16
To: Harald Alvestrand <h...@google.com>
Cc: cxx <c...@chromium.org>
Subject: Re: Query: Use of std::vector<const something> in Chromium?
 
 

This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email. For any questions don't hesitate to reach out to secu...@xperi.com


Dana Jansens

unread,
Feb 6, 2024, 1:19:09 PM2/6/24
to Adam Rice, Hans Wennborg, Peter Kasting, Harald Alvestrand, cxx, Nico Weber
On Tue, Feb 6, 2024 at 1:10 AM Adam Rice <ri...@chromium.org> wrote:
Very odd that you can just wrap your const value in a struct and the problem goes away:


The restriction seems very artificial.

Yeah I think what vector really wants is move-constructible and move-assignable, which would fail with a const member in a struct. But it may go out of its way and do non-optimal things if a type is not move-assignable.
 

Harald Alvestrand

unread,
Feb 6, 2024, 2:42:21 PM2/6/24
to Dana Jansens, Adam Rice, Hans Wennborg, Peter Kasting, cxx, Nico Weber
the patch to llvm to allow us to not fix this was kind of illuminating.


It just does const_cast to strip away the constness, and then uses memcpy on the result.
In other words - the const keyword is being ignored by the std::vector implementation in order to make std::vector work.

Not requiring this seems like a Good Thing.

David Benjamin

unread,
Feb 6, 2024, 3:05:21 PM2/6/24
to Harald Alvestrand, Dana Jansens, Adam Rice, Hans Wennborg, Peter Kasting, cxx, Nico Weber
Huh. How does that interact with Adam's example in https://godbolt.org/z/z4f6qdr6h? Has that been ignoring the const all this time too?

Harald Alvestrand

unread,
Feb 7, 2024, 1:10:05 AM2/7/24
to David Benjamin, Dana Jansens, Adam Rice, Hans Wennborg, Peter Kasting, cxx, Nico Weber
My impression is that memcpy() ignores most things about the inner workings of the object it's copying... so the outer const is the only part that needs to be ignored in order to make memcpy() eat the object.


David Benjamin

unread,
Feb 7, 2024, 1:17:02 AM2/7/24
to Harald Alvestrand, Dana Jansens, Adam Rice, Hans Wennborg, Peter Kasting, cxx, Nico Weber
Right, but std::vector isn't defined to call memcpy. That's an optimization libc++ does when a type is trivially movable. If we have a vector of a type that isn't movable at all, to say nothing of trivially so, it's surprising that libc++ is calling memcpy. I.e. my question isn't why the memcpy compiles. It's whether there's a deeper problem in memcpy getting chosen at all.
Reply all
Reply to author
Forward
0 new messages