zpp_bits icon indicating copy to clipboard operation
zpp_bits copied to clipboard

Serializing std::vector<int> vs std::vector<int> const different sizes

Open bjadamson opened this issue 1 year ago • 13 comments

OS: windows Compiler: Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.10.2

Hi, is this behavior expected?

I've been reading through the section on serializing standard library types but I can't find anything anything about whether the vector being const affecting the sizes. What I understand is the size should always be serialized using 4 bytes (by default).

#include <external/zpp_full.hpp>
#include <cassert>

using U8Buffer = std::vector<std::uint8_t>;
using U8Writer = zpp::bits::out<U8Buffer>;

int main(int, char**)
{
  U8Buffer b0;
  U8Buffer b1;
  {
    U8Writer writer{b0};
    std::vector<int> const data;
    assert(std::errc{} == writer(data));
  }
  {
    U8Writer writer{b1};
    std::vector<int> data;
    assert(std::errc{} == writer(data));
  }
  assert(b0 == b1); // FAIL, std::size(b0)==0, and std::size(b1)==4
  return 0;
}

bjadamson avatar Jun 14 '24 21:06 bjadamson

Here's a link using gcc: https://godbolt.org/z/h9es5KbYf

clang: https://godbolt.org/z/nrre8j3bW

bjadamson avatar Jun 14 '24 21:06 bjadamson

To anyone else who runs into this for now I'm just casting away the const from my reference that I'm serializing.

bjadamson avatar Jun 18 '24 02:06 bjadamson

Hi @bjadamson , I understand that the issue occurs only with MSVC, right? Let me try and explore this.

eyalz800 avatar Jul 04 '24 13:07 eyalz800

It's most likely an MSVC bug, if I change this code (line 2151, 2152): image

To this:

image

It resolves - this means that msvc finds the wrong "container" variable despite the nearest variable is the one declared in the requires expression.

Please check here- https://godbolt.org/z/8hdTaYE8M

eyalz800 avatar Jul 04 '24 15:07 eyalz800

Hi @eyalz800 ,

Thanks for looking into this. Unfortunately the issue seems to happen in gcc/clang as well:

https://github.com/eyalz800/zpp_bits/issues/164#issuecomment-2168793777

I hope this helps. If it helps I noticed the issue also seems to occur with std::variant I'm using.

bjadamson avatar Jul 04 '24 15:07 bjadamson

@bjadamson but in GCC and clang both are size 4 no?

eyalz800 avatar Jul 04 '24 15:07 eyalz800

The size of b0 in both are 0. The size of b1 in both are 4.

b0 in my opinion should be 4 as well, even though it's empty.

bjadamson avatar Jul 04 '24 15:07 bjadamson

But in GCC and clang is says 4 for both in your links: image image

eyalz800 avatar Jul 04 '24 15:07 eyalz800

Wow your right I'm so sorry. My brain was swapping the b0 with the value for some reason.

bjadamson avatar Jul 04 '24 15:07 bjadamson

@bjadamson at least you seem to have found some bug in MSVC, I didn't submit a patch to my own code because my entire code is written with name hiding from requires expressions so fixing just this piece is not gonna cut it - want to submit an MSVC bug and link it here?

eyalz800 avatar Jul 04 '24 15:07 eyalz800

Sure -- if you have suggestions on going through the process I'd love to hear them. This will be my first bug report with those folks.

bjadamson avatar Jul 04 '24 15:07 bjadamson

Always a good day to learn something new

eyalz800 avatar Jul 04 '24 15:07 eyalz800

Hi, I couldn't find an issue opened with Microsoft regarding this and went ahead with submitting a ticket. https://developercommunity.visualstudio.com/t/C-requires-expression-name-shadowing-i/10726925

SenseOnline avatar Aug 21 '24 05:08 SenseOnline

Updating that this issue has been resolved in MSVC release version 17.12.

SenseOnline avatar Nov 13 '24 09:11 SenseOnline

Thank you for the update

eyalz800 avatar Nov 13 '24 10:11 eyalz800