C++ "type uses the anonymous namespace"

49 views Asked by At

I have a bunch of files involved in the problem, so I'll try to summarize as much as possible.

1 - 'traits/logger.h' defines a concept like (shortly):

namespace tenacitas::lib::traits {

template <typename t>
concept logger = requires(t p_t) {
  {
    p_t.deb(std::declval<std::string_view>(),
            std::declval<std::source_location>() =
                std::source_location::current())
    } -> std::same_as<void>;
    ...

};
} 

2 - 'log/internal/logger.h' defines the base class for logging, which conforms to the concept above, inside tenacitas::lib::log::internal.

3 - In 'log/cerr.h` we have:

namespace tenacitas::lib::log {

using cerr = tenacitas::lib::log::internal::logger<decltype(
    [](std::string_view s) { std::cerr << s; })>;

} 

4 - In 'container/circular_queue.h' there is:

namespace tenacitas::lib::container {

template <typename t>
concept circular_queue_supplier = requires(t p_t) {
  typename t::logger;
  tenacitas::lib::traits::logger<typename t::logger>;
  { p_t.get_logger() } -> std::same_as<typename t::logger &>;
};


template <typename t_data, circular_queue_supplier t_circular_queue_supplier>
requires std::move_constructible<t_data> && std::copy_constructible<t_data>
struct circular_queue {
  using data = t_data;
  using logger = typename t_circular_queue_supplier::logger;
...
private:
  logger &m_logger;
...
};

}

5 - In 'test/container/circular_queue.h` I wrote:

struct supplier {
  using logger = tenacitas::lib::log::cerr;
  logger &get_logger() { return m_log; };

private:
  logger m_log;
};

bool test(){
    supplier _supplier;
    container::circular_queue<std::string, supplier> queue(_supplier);
    ...
}

6 - In main.cpp I call the test function above.

When I try to build using gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, I get:

‘supplier’ has a field ‘supplier::m_log’ whose type uses the anonymous namespace [-Werror=subobject-linkage]

and also

‘tenacitas::lib::container::circular_queue<std::__cxx11::basic_string<char>, supplier>’ has a field ‘tenacitas::lib::container::circular_queue<std::__cxx11::basic_string<char>, supplier>::m_log’ whose type uses the anonymous namespace [-Werror=subobject-linkage]

I have tried many things, but with no results.

Any ideas?

1

There are 1 answers

0
canellas On

As @IgorTandetnik posted, the correction is to use something like:

namespace tenacitas::lib::log {

struct cerr_writer {
  void operator()(std::string_view p_str) { std::cerr << p_str; }
};

using cerr = tenacitas::lib::log::internal::logger<cerr_writer>;

} // namespace tenacitas::lib::log