Skip to content

<compare>: std::_Literal_zero has an impact on real code. #4359

@fsb4000

Description

@fsb4000

Describe the bug

Our type std::_Literal_zero has an impact on real code.

From the comment: #4332 (comment)

Command-line test case

#include <compare>

template<typename T, typename U>
constexpr bool not_equal(T lhs, U rhs) 
requires(requires(T lhs, U rhs) { lhs != rhs; })
{
        return lhs != rhs;
}

template<typename T, typename U>
constexpr bool not_equal(T lhs, U rhs) 
requires(!requires(T lhs, U rhs) { lhs != rhs; })
{
        return false;
}

int main() {
    constexpr int int_zero = 0;
    constexpr unsigned int unsigned_zero = 0;
    constexpr auto ordering = 1 <=> 1;
    //constexpr bool b = not_equal(ordering, unsigned_zero);
    constexpr bool b = not_equal(ordering, int_zero);
    return b == false;
}


C:\Temp>cl /EHsc /W4 /WX /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33321 for x64
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
.\repro.cpp(7): error C7595: 'std::_Literal_zero::_Literal_zero': call to immediate function is not a constant expression
.\repro.cpp(7): note: failure was caused by a read of a variable outside its lifetime
.\repro.cpp(7): note: see usage of 'rhs'
.\repro.cpp(7): note: the template instantiation context (the oldest one first) is
.\repro.cpp(22): note: see reference to function template instantiation 'bool not_equal<std::strong_ordering,int>(T,U)' being compiled
        with
        [
            T=std::strong_ordering,
            U=int
        ]

Expected behavior

Should compile

STL version

202e382

Additional context

Other compiler libraries compile the code: https://godbolt.org/z/1vbYbq5b7

We added std::_Literal_zero because of this issue: #3581

Previously using _Literal_zero = decltype(nullptr); warned this code:

#include <compare>

auto b = 1 <=> 2 < 0;

int main() {}
clang-cl /std:c++20 -Wzero-as-null-pointer-constant main.cpp
main.cpp(3,20): warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
auto b = 1 <=> 2 < 0;
                   ^
                   nullptr
1 warning generated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    externalThis issue is unrelated to the STL

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions