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

REQUIRE does not compile when operator== in different namespace #443

Closed
darrenjs opened this issue Nov 27, 2020 · 3 comments
Closed

REQUIRE does not compile when operator== in different namespace #443

darrenjs opened this issue Nov 27, 2020 · 3 comments
Labels

Comments

@darrenjs
Copy link

Hi,

I have a simple equality that compiles normally but fails when placed within a REQUIRE. This is a surprise to me, since I was hoping to replace many of my manual tests (which are based on f ( a == b) ) by moving them into a REQUIRE. (Aside, this same conditional compiles when inside the equivalent CppUnit macro).

To reproduce, please compile the file below. I used gcc 7.5.0 on Ubuntu 18, doctest 2.4.1

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

namespace user {

struct label {
  label() : i(0) {}
  int i;
};

}

/* deliberately outside the user:: namespace */
bool operator==(const user::label& lhs, const user::label& rhs) {
  return lhs.i == rhs.i;
}

void foo() {
  user::label a;
  user::label b;

  if (a == b) return; /* this compiles */

  REQUIRE(a == b); /* does not compile */
}

Error:

g++   -Wfatal-errors  -pedantic -Wall --std=c++1z main.cc  
In file included from main.cc:2:0:
doctest.h: In instantiation of ‘doctest::detail::Result doctest::detail::Expression_lhs<L>::operator==(const R&) [with R = user::label; L = const user::label&]’:
main.cc:26:3:   required from here
doctest.h:1149:32: error: no match for ‘operator==’ (operand types are ‘const user::label’ and ‘const user::label’)
 #define DOCTEST_CMP_EQ(l, r) l == r
                                ^
doctest.h:1044:20: note: in expansion of macro ‘DOCTEST_CMP_EQ’
         bool res = op_macro(lhs, rhs);                                                             \
                    ^~~~~~~~
doctest.h:1186:9: note: in expansion of macro ‘DOCTEST_DO_BINARY_EXPRESSION_COMPARISON’
         DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(==, " == ", DOCTEST_CMP_EQ) //!OCLINT bitwise operator in conditional

The declaration of the operator== outside the user namespace is significant; if I move it into user, everything does compiles.

@onqtam
Copy link
Member

onqtam commented Dec 4, 2020

Well, this isn't good... Catch2 handles this OK as well - only doctest doesn't.

I'm curious what's the use case of having the operator== outside of the namespace?

@onqtam onqtam added the bug label Dec 4, 2020
@darrenjs
Copy link
Author

darrenjs commented Dec 7, 2020

I'm curious what's the use case of having the operator== outside of the namespace?

Am not sure. I came across this code when porting a large project from cppunit to doctest, I don't think it was put in that other namespace deliberately, more likely an accident. But it never caused any problem, well, except as we port to doctest.

Fyi, the reason we prefer doctest over catch, is doctest support for threading, ie, calling REQUIRE from any thread.

onqtam pushed a commit that referenced this issue Mar 21, 2021
#468)

* REQUIRE does not compile when operator== in different namespace #443 .
Expression_lhs.op member method is not instantiated when it is missing
a member operator and the user defined conversion is able to apply the
global operator.

* Removing utility and using an overloaded version of declval which is faster in doctest_fwd.h .

* Using templated operator== inside TEST_CASE changes deduced types of forwarding references #399 . This is fixed by using rvalues as function argument and using forward for the right type of reference. Now both gcc and doctest either fails or either compiles but not like one compiles and the other fails
@onqtam
Copy link
Member

onqtam commented Mar 21, 2021

closed thanks to #468 (merged in dev)

@onqtam onqtam closed this as completed Mar 21, 2021
onqtam pushed a commit that referenced this issue Mar 22, 2021
#468)

* REQUIRE does not compile when operator== in different namespace #443 .
Expression_lhs.op member method is not instantiated when it is missing
a member operator and the user defined conversion is able to apply the
global operator.

* Removing utility and using an overloaded version of declval which is faster in doctest_fwd.h .

* Using templated operator== inside TEST_CASE changes deduced types of forwarding references #399 . This is fixed by using rvalues as function argument and using forward for the right type of reference. Now both gcc and doctest either fails or either compiles but not like one compiles and the other fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants