Skip to content

<atomic>: atomic_ref<int8_t>::operator--(int) fails to compile #1302

Description

There is an incorrect cast on line 1368 of <atomic>. The cast to _Ty should be _TVal.

_TVal operator--(int) noexcept {
    return static_cast<_Ty>(_InterlockedExchangeAdd8(_Atomic_address_as<char>(this->_Storage), -1));
}

Command-line test case

C:\Temp>type repro.cpp
#include <atomic>

int main() {
	int8_t x = 0;
	std::atomic_ref(x)--;
}

C:\Temp>cl /c /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29304.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29304\include\atomic(1368): error C2440: 'static_cast': cannot convert from 'char' to '_Ty'
        with
        [
            _Ty=int8_t &
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29304\include\atomic(1368): note: static_cast and safe_cast to reference can only be used for valid initializations or for lvalue casts between related classes
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29304\include\atomic(1367): note: while compiling class template member function 'signed char std::_Atomic_integral<_Ty &,1>::operator --(int) noexcept'
        with
        [
            _Ty=int8_t
        ]
repro.cpp(5): note: see reference to function template instantiation 'signed char std::_Atomic_integral<_Ty &,1>::operator --(int) noexcept' being compiled
        with
        [
            _Ty=int8_t
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29304\include\atomic(1781): note: see reference to class template instantiation 'std::_Atomic_integral<_Ty &,1>' being compiled
        with
        [
            _Ty=int8_t
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29304\include\atomic(2328): note: see reference to class template instantiation 'std::_Atomic_integral_facade<_Ty &>' being compiled
        with
        [
            _Ty=int8_t
        ]
repro.cpp(5): note: see reference to class template instantiation 'std::atomic_ref<int8_t>' being compiled

Expected behavior
Should compile successfully.

STL version
Latest

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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