Skip to content

Remove unnecessary friend keywords #2974

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

Closed
wants to merge 1 commit into from

Conversation

ZXShady
Copy link
Contributor

@ZXShady ZXShady commented Apr 16, 2025

Note that I didn't remove it for internal\catch_optional.hpp for friend bool operator==(Optional<T>,Optional<T>) and operator!= since the friend function will allow for implicit conversions which allows comparing against a T like Catch::Optional<int>() == int(1) nor did I make inline functions not inline like operator<< for ITransientExpression.

This change removes several friend declarations that are no longer necessary. Overuse of friend can lead to tighter coupling between classes, reduced encapsulation and thinking it relies on implementation details.
in general avoid granting excessive access unless explicitly required.

Note that I didn't remove it for `internal\catch_optional.hpp` for ` friend bool operator==(Optional<T>,Optional<T>)` and `operator!=` since the friend function will allow for implicit conversions which allows comparing against a `T`
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.98%. Comparing base (5b3b228) to head (6e6820b).
Report is 1 commits behind head on devel.

Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #2974      +/-   ##
==========================================
- Coverage   91.01%   90.98%   -0.03%     
==========================================
  Files         198      198              
  Lines        8599     8599              
==========================================
- Hits         7826     7823       -3     
- Misses        773      776       +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@neheb
Copy link
Contributor

neheb commented Apr 17, 2025

../src/catch2/internal/catch_reusable_string_stream.hpp:46:20: error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup
   46 |             *m_oss << value;
      |                    ^
../src/catch2/catch_tostring.hpp:124:21: note: in instantiation of function template specialization 'Catch::ReusableStringStream::operator<<<foo::helper_1403>' requested here
  124 |                 rss.operator<<(value);
      |                     ^
../src/catch2/catch_tostring.hpp:146:88: note: in instantiation of function template specialization 'Catch::StringMaker<foo::helper_1403>::convert<foo::helper_1403>' requested here
  146 |             return ::Catch::StringMaker<std::remove_cv_t<std::remove_reference_t<T>>>::convert(e);
      |                                                                                        ^
../src/catch2/internal/catch_decomposer.hpp:195:42: note: in instantiation of function template specialization 'Catch::Detail::stringify<foo::helper_1403>' requested here
  195 |                     ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) );
      |                                          ^
../src/catch2/internal/catch_decomposer.hpp:199:19: note: in instantiation of member function 'Catch::BinaryExpr<foo::helper_1403 &, foo::helper_1403 &>::streamReconstructedExpression' requested here
  199 |         constexpr BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs )
      |                   ^
../tests/SelfTest/UsageTests/Compilation.tests.cpp:32:22: note: 'operator<<' should be declared prior to the call site or in namespace 'foo'
   32 | static std::ostream& operator<<(std::ostream& out, foo::helper_1403 const&) {
      |                      ^
1 error generated.

Copy link
Member

@horenmar horenmar left a comment

Choose a reason for hiding this comment

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

friend declaration in C++ has two reasons

  1. To access the internals of a class
  2. To declare a hidden friend

While it is annoying that to get 2) you also have to have 1), 2) is desirable enough to keep the functions as hidden friends even if they do not need access to the class internals.

@ZXShady
Copy link
Contributor Author

ZXShady commented Apr 18, 2025

friend declaration in C++ has two reasons

  1. To access the internals of a class
  2. To declare a hidden friend

While it is annoying that to get 2) you also have to have 1), 2) is desirable enough to keep the functions as hidden friends even if they do not need access to the class internals.

Yea,

I guess it can be removed for friended functions declared out of line since hidden friends only work if it is defined inline.

I will update my pr.

@horenmar
Copy link
Member

@ZXShady
Copy link
Contributor Author

ZXShady commented Apr 18, 2025

Hidden friends can be defined out of line as well

Oh! I didn't know that, you can close my pr.

@ZXShady ZXShady closed this Apr 18, 2025
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.

3 participants