Discussion
Loading...

Post

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Kat March谩n 馃悎
@zkat@toot.cat  路  activity timestamp 2 months ago

Dear C++ #CPP people: how do you actually fucking do this? It seems bad enough that I need to have two overloads just to allow for the "no args" version.

But that template overload fails at link time because of an undefined symbol???

UPDATE: It鈥檚 because I needed to define it in the header file itself, not just declare it

void info(std::string_view out) {
  ::sys::log::f_log_log(::sys::log::LogLevel::Info,
                              static_cast<std::string>(out));
}
template <class... Args>
void info(std::format_string<Args...> fmt, Args &&...args) {
  ::sys::log::f_log_log(::sys::log::LogLevel::Info,
                              std::format(fmt, std::forward<Args>(args)...));
}
void info(std::string_view out) { ::sys::log::f_log_log(::sys::log::LogLevel::Info, static_cast<std::string>(out)); } template <class... Args> void info(std::format_string<Args...> fmt, Args &&...args) { ::sys::log::f_log_log(::sys::log::LogLevel::Info, std::format(fmt, std::forward<Args>(args)...)); }
void info(std::string_view out) { ::sys::log::f_log_log(::sys::log::LogLevel::Info, static_cast<std::string>(out)); } template <class... Args> void info(std::format_string<Args...> fmt, Args &&...args) { ::sys::log::f_log_log(::sys::log::LogLevel::Info, std::format(fmt, std::forward<Args>(args)...)); }
  • Copy link
  • Flag this post
  • Block
ananas
@ananas@scicomm.xyz replied  路  activity timestamp 2 months ago
@zkat
```cpp
#include <format>
#include <print>

template <typename... Args>
void info(std::format_string<Args...> fmt, Args&&... args) {
auto res = std::format(fmt, std::forward<Args&&>(args)...);
std::println("{}", res);
}

int main() {
info("{}", 42);
info("moi");
}
```

Seems to work in compiler explorer. Typos might be present, writing this on phone. Did you need something else?

Failing on link time might happen if you have template in a source code file not visible to an another translation unit (which is why we write template code into headers, or modules if we're in the future).

  • Copy link
  • Flag this comment
  • Block
Kat March谩n 馃悎
@zkat@toot.cat replied  路  activity timestamp 2 months ago

C++ not be fucking obtuse about a simple fucking task challenge. Difficulty level: impossible.

  • Copy link
  • Flag this comment
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About 路 Code of conduct 路 Privacy 路 Users 路 Instances
Bonfire social 路 1.0.0-rc.2.21 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login