-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Provide a forward declaration headers #1481
Comments
you can include only logger.h. It is is not fwd, but close enough.. |
|
Fair enough. However I am not sure what meaningful fwd head is possible since a major part of the logger are templates. |
The important part is that with a forward declaration, you can make (smart) pointers/references to a To use this pointer/reference, you then do need to include the logger header, but that can be done in the |
This is a utility header for measuring time taken by a blck of code we have in our codebase: #include <memory>
#include <string>
namespace spdlog {
class logger;
}
struct Timer {
// Keeps track of how long it was running
};
struct TimerMeasureRAII {
TimerMeasureRAII(std::string id, std::shared_ptr<spdlog::logger> const &logger)
: m_id(std::move(id))
, m_logger(logger)
{}
//! Initializes m_logger to `spdlog::get("main-logger")`.
explicit TimerMeasureRAII(std::string id);
//! Log the time elapsed since calling the constructor
~TimerMeasureRAII();
private:
Timer m_timer;
std::shared_ptr<spdlog::logger> m_logger;
const std::string m_id;
static thread_local std::size_t s_measure_nesting;
};
template <typename F>
auto Timer::measure(std::string id, F &&f) -> decltype(f()) {
TimerMeasureRAII timer(std::move(id));
return f();
}
template <typename F>
auto Timer::measure(std::string id, std::shared_ptr<spdlog::logger> const &logger, F &&f) -> decltype(f()) {
TimerMeasureRAII timer(std::move(id), logger);
return f();
} Notice that it doesn't need to include |
Not sure that those 3 lines really worth a new header file (on the other hand its mere existence might remind developers to use it which is good). |
You should probably also provide fwd decls for other classes/types your users might want to customize, e.g. sinks. But even if you provide just the logger, you should provide it because it is generally a bad practice to create forward declarations for 3rd namespaces. |
Fixed by adding |
I have multiple places where I want to only deal with pointer to
spdlog::logger
, without having to include spdlog headers.I can (and currently do) forward declare the class myself, but forward declaring names from thirdparty namespaces is generally considered poor form and I would like to avoid it.
The text was updated successfully, but these errors were encountered: