Skip to content

<algorithm>: fill does not accept volatile byte pointers #1556

@Ryan-rsm-McKenzie

Description

@Ryan-rsm-McKenzie

Describe the bug
fill cannot be called using a volatile byte*, as it triggers an optimized call to memset, which does not accept volatile pointers. This bug does not manifest with char types. See related bug #1183

Command-line test case

E:\test>type main.cpp
#include <algorithm>
#include <cstddef>

template <class T>
void volatile_zero(void* ptr, std::size_t size)
{
    auto begin = static_cast<volatile T*>(ptr);
    constexpr T val{ 0 };
    std::fill_n(begin, size, val);
}

int main()
{
    int data;
    volatile_zero<char>(&data, sizeof(data));
    volatile_zero<signed char>(&data, sizeof(data));
    volatile_zero<unsigned char>(&data, sizeof(data));
    volatile_zero<std::byte>(&data, sizeof(data));
}

E:\test>cl /EHsc /std:c++17 .\main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29617 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

main.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\include\xutility(4833): error C2664: 'void *memset(void *,int,size_t)': cannot convert argument 1 from '_DestTy *const ' to 'void *'
        with
        [
            _DestTy=std::byte
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\include\xutility(4833): note: Conversion loses qualifiers
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\include\vcruntime_string.h(63): note: see declaration of 'memset'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\include\xutility(4930): note: see reference to function template instantiation 'void std::_Fill_memset<volatile T,_Ty>(_DestTy *const ,const _Ty,const size_t)' being compiled
        with
        [
            T=std::byte,
            _Ty=std::byte,
            _DestTy=std::byte
        ]
.\main.cpp(9): note: see reference to function template instantiation '_OutIt std::fill_n<volatile T*,size_t,T>(_OutIt,const _Diff,const _Ty &)' being compiled
        with
        [
            _OutIt=volatile std::byte *,
            T=std::byte,
            _Diff=size_t,
            _Ty=std::byte
        ]
.\main.cpp(18): note: see reference to function template instantiation 'void volatile_zero<std::byte>(void *,size_t)' being compiled

Expected behavior
I would expect this to compile.

STL version
Microsoft Visual Studio Community 2019 Preview
Version 16.9.0 Preview 2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions