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

Mark RCP<T>::operator!= as const #1756

Merged
merged 3 commits into from Mar 25, 2021
Merged

Conversation

cqc-alec
Copy link
Contributor

This is necessary in order for code compiled with clang -std=c++20 to include the symengine headers.

To reproduce the issue (sorry I wasn't sure how best to make a test for this):

mkdir build
cd build
cmake ..
make

echo "#include <symengine/visitor.h>" > test.cpp
clang++ -I. -I.. -std=c++20 -Wno-ambiguous-reversed-operator -c test.cpp

This generates the errors:

In file included from test.cpp:1:
../symengine/visitor.h:262:19: error: use of overloaded operator '!=' is ambiguous (with operand types 'SymEngine::RCP<const SymEngine::Basic>' and 'SymEngine::RCP<const SymEngine::Basic>')
        if (farg1 != newarg1 or farg2 != newarg2) {
            ~~~~~ ^  ~~~~~~~
../symengine/type_codes.inc:63:1: note: in instantiation of function template specialization 'SymEngine::TransformVisitor::bvisit<SymEngine::Function>' requested here
SYMENGINE_ENUM(SYMENGINE_ATAN2, ATan2)
^
../symengine/visitor.h:70:37: note: expanded from macro 'SYMENGINE_ENUM'
        down_cast<Derived *>(this)->bvisit(x);                                 \
                                    ^
../symengine/visitor.h:245:5: note: in instantiation of member function 'SymEngine::BaseVisitor<SymEngine::TransformVisitor, SymEngine::Visitor>::visit' requested here
    TransformVisitor()
    ^
../symengine/symengine_rcp.h:178:10: note: candidate function [with T2 = const SymEngine::Basic]
    bool operator!=(const RCP<T2> &p2)
         ^
../symengine/symengine_rcp.h:172:10: note: candidate function [with T2 = const SymEngine::Basic]
    bool operator==(const RCP<T2> &p2)
         ^
../symengine/symengine_rcp.h:172:10: note: candidate function (with reversed parameter order) [with T2 = const SymEngine::Basic]
In file included from test.cpp:1:
../symengine/visitor.h:262:19: error: use of overloaded operator '!=' is ambiguous (with operand types 'SymEngine::RCP<const SymEngine::Basic>' and 'SymEngine::RCP<const SymEngine::Basic>')
        if (farg1 != newarg1 or farg2 != newarg2) {
            ~~~~~ ^  ~~~~~~~
../symengine/type_codes.inc:114:1: note: in instantiation of function template specialization 'SymEngine::TransformVisitor::bvisit<SymEngine::Boolean>' requested here
SYMENGINE_ENUM(SYMENGINE_EQUALITY, Equality)
^
../symengine/visitor.h:70:37: note: expanded from macro 'SYMENGINE_ENUM'
        down_cast<Derived *>(this)->bvisit(x);                                 \
                                    ^
../symengine/visitor.h:245:5: note: in instantiation of member function 'SymEngine::BaseVisitor<SymEngine::TransformVisitor, SymEngine::Visitor>::visit' requested here
    TransformVisitor()
    ^
../symengine/symengine_rcp.h:178:10: note: candidate function [with T2 = const SymEngine::Basic]
    bool operator!=(const RCP<T2> &p2)
         ^
../symengine/symengine_rcp.h:172:10: note: candidate function [with T2 = const SymEngine::Basic]
    bool operator==(const RCP<T2> &p2)
         ^
../symengine/symengine_rcp.h:172:10: note: candidate function (with reversed parameter order) [with T2 = const SymEngine::Basic]
2 errors generated.

With gcc or MSVC the code compiles fine; it is just clang that doesn't like it (and clang is happy to deduce operator!= from operator==). I don't know which compiler is "correct"; the difference may just reflect different levels of C++20 support.

The -Wno-ambiguous-reversed-operator is just to suppress some other warnings, which do however look somewhat related.

@lkeegan
Copy link
Member

lkeegan commented Mar 25, 2021

Thanks for the report & workaround!

I think this problem here is that the operator != defined here is not const, and making it const should resolve this - see e.g. https://stackoverflow.com/questions/60386792/c20-comparison-warning-about-ambiguous-reversed-operator

@cqc-alec
Copy link
Contributor Author

I think this problem here is that the operator != defined here is not const, and making it const should resolve this - see e.g. https://stackoverflow.com/questions/60386792/c20-comparison-warning-about-ambiguous-reversed-operator

Ah, thanks! That's much better, I will amend my fix.

Copy link
Member

@isuruf isuruf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should == be marked const as well?

@cqc-alec
Copy link
Contributor Author

Should == be marked const as well?

Yes I guess it should. Just updated with that addition.

@isuruf isuruf changed the title Omit the template for RCP<T>::operator!= when compiling with clang and C++20. Mark RCP<T>::operator!= as const Mar 25, 2021
@rikardn rikardn merged commit dec0929 into symengine:master Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants