Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equality == comparison between not_null and normal pointer works with Microsoft 2019 compiler, but not with g++ 11 compiler #1195

Closed
daltairwalter opened this issue Feb 11, 2025 · 2 comments
Labels
Status: Open Needs attention Type: Bug Indicates a bug or error

Comments

@daltairwalter
Copy link

The below code compiles on windows with MSVC 2019, it appears to be using the implicit conversion operator and doing the equality comparison as a standard pointer comparison. On linux using g++11, the following code fails to compile and generates an error:

   13 |     if (notNull == test)
      |         ~~~~~~~ ^~ ~~~~
      |         |          |
      |         |          Test*
      |         gsl::not_null<Test*>

I have tried various c++ versions with -std:c++14,17,20 and /permissive- and none of these options seem to make the MS compiler fail or the g++ compiler pass.

The provided equality operator only seems to be effective when both inputs are gsl::not_null - given this state, I am wondering if it would make sense to make the equality operator a class member so that it isn't considered for all of the equality comparisons that don't involve not_null.

#include "gsl/gsl"
#include <iostream>
#include <memory>

class Test
{
};

int main()
{
    auto test = new Test();
    auto notNull = gsl::not_null<Test*>(test);
    if (notNull == test)
    {
        std::cout << "not_null == Test*" << std::endl;
    }
    else
    {
        std::cout << "not_null != Test*" << std::endl;
    }
    return 0;
}
@carsonRadtke
Copy link
Collaborator

carsonRadtke commented Feb 11, 2025

Could you update the issue to include the complete error message?

I am unable to reproduce the compilation failure on gcc-11 (https://godbolt.org/z/PM6f9fx49). Could you please provide more information:

  • Confirm you are using GSL v4.1.0
  • Output of $ gcc --version?
  • A compiler explorer link

@daltairwalter
Copy link
Author

daltairwalter commented Feb 11, 2025

I had changes to templatize the implicit conversion operator see:
#1187

The templatized implicit conversion operator seems to work, but for this equality to work with g++, the non-templatized implicit conversion operator is also needed.

Sorry about the confusion - this is working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Open Needs attention Type: Bug Indicates a bug or error
Projects
Status: New
Development

No branches or pull requests

2 participants