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

Define operator<< in the proper class namespaces to fix ADL. #990

Closed
pjungkamp opened this issue Sep 22, 2023 · 2 comments · Fixed by #1096
Closed

Define operator<< in the proper class namespaces to fix ADL. #990

pjungkamp opened this issue Sep 22, 2023 · 2 comments · Fixed by #1096
Labels
bug Something isn't working

Comments

@pjungkamp
Copy link

Description

The operator<< implementations have been defined in the global namespace instead of the class namespace.

See for example:

} // namespace rtc
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::IceState state);
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
rtc::PeerConnection::GatheringState state);
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out,
rtc::PeerConnection::SignalingState state);

This breaks ADL based discovery of operators. I run into this issue when trying to format libdatachannel types using fmt 10.0.0.

An example of broken ADL operator discovery

Here is a very basic reproducer to demonstrate broken ADL based discovery: https://godbolt.org/z/Gx8sb3eb8

#include <fmt/ostream.h>
#include <fmt/format.h>
#include <iostream>

// create a class in a namespace

namespace test {
class Test {};
}

// define the ostream operator outside the namespace

std::ostream &operator<<(std::ostream &s, test::Test) {
    return s << "test";
}

// the fmt::ostream_formatter can't find the operator<< using ADL

template<> class fmt::formatter<test::Test> : public fmt::ostream_formatter {};

// A simple main function to trigger the compilation error

int main() {
  test::Test t;
  std::cout << fmt::format("{}", t);
  return 0;
}

This is the crude fix I'm using to enable printing on my side: https://godbolt.org/z/TK3bf786E

#include <fmt/ostream.h>
#include <fmt/format.h>
#include <iostream>

// create a class in a namespace

namespace test {
class Test {};
}

// define the ostream operator outside the namespace

std::ostream &operator<<(std::ostream &s, test::Test) {
    return s << "test";
}

// pull all global defined operator<< definitions into the test namespace

namespace test {
    using ::operator<<;
}

// the fmt::ostream_formatter can now find ::operator<< using ADL

template<> class fmt::formatter<test::Test> : public fmt::ostream_formatter {};

// A simple main function

int main() {
  test::Test t;
  std::cout << fmt::format("{}", t);
  return 0;
}

Here is a proper fix for this example: https://godbolt.org/z/bPvj6vqPG

#include <fmt/ostream.h>
#include <fmt/format.h>
#include <iostream>

namespace test {

// create a class in a namespace

class Test {};

// define the ostream operator inside the namespace

std::ostream &operator<<(std::ostream &s, test::Test) {
    return s << "test";
}

} // namespace test

// the fmt::ostream_formatter can now find test::operator<< using ADL

template<> class fmt::formatter<test::Test> : public fmt::ostream_formatter {};

// A simple main function

int main() {
  test::Test t;
  std::cout << fmt::format("{}", t);
  return 0;
}

Expected

This is the expected code to make libdatachannel types formattable using fmt.

// declare a formatter based on the ostream operator provided by the library
template<> fmt::formatter<rtc::PeerConnection> : public fmt::ostream_formatter {}

Workaround

This is actually needed to make the libdatachannel type formattable by fmt.

// pull the operator<< definition into the rtc namespace
namespace rtc { using ::operator<<; }
// declare a formatter based on the ostream operator provided by the library
template<> fmt::formatter<rtc::PeerConnection> : public fmt::ostream_formatter {}

Suggested Fix

Please move all the std::ostream &operator<<(std::ostream &os, T t) definitions into the namespace where the corresponding type is defined.

@paullouisageneau paullouisageneau added the bug Something isn't working label Sep 25, 2023
@melpon
Copy link
Contributor

melpon commented Oct 20, 2023

I encountered the same issue.

void foo() {
  rtc::PeerConnection::GatheringState state =
      rtc::PeerConnection::GatheringState::Complete;
  // error: no match for ‘operator<<’
  PLOG_DEBUG << "onGatheringStateChange: " << state;
}

Thanks to the workaround namespace rtc { using ::operator<<; }, it's now working. Thank you.

@paullouisageneau
Copy link
Owner

Thank you for reporting, fixed by #1096

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants