Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining function parameter pack and default arguments

I have a function with a parameter pack:

template<typename... Targs>
void tprintf(const char* format, Targs... args) {}

(the implementation shouldn't matter, just the signature). I want to add source position as default argument, using the GCC/Clang builtins. Something like

template<typename... Targs>
void tprintf(const char* format, Targs... args,
             const char* file = __builtin_FILE(),
             unsigned line = __builtin_LINE()) {}

This compiles, but calls to it are not passing parameters to args as I hoped; e.g.

tprintf("%d%s", 0, "a");

gives (on Clang 10)

<source>:7:5: error: no matching function for call to 'tprintf'
    tprintf("%d%s", 0, "a");    
    ^~~~~~~
<source>:2:6: note: candidate function template not viable: no known conversion from 'const char [2]' to 'unsigned int' for 3rd argument
void tprintf(const char* format, Targs... args,    
     ^

which seems to indicate args is empty, 0 is file, and "a" is line.

Actually, while writing the question I've found that explicitly passing Targs works:

tprintf<int, char*>("%d%s", 0, "a");

Is it possible to avoid this?

like image 866
Alexey Romanov Avatar asked Oct 21 '25 10:10

Alexey Romanov


2 Answers

The solution would be to us C++20's std::source_location:

#include <iostream>
#include <source_location>

template<typename... Targs, auto location = []{ return source_location::current(); }()>
auto tprintf(char const* format, Targs const&... args) -> void {
    std::cout
        << std::format("{}:{}: ", location.file_name(), location.line())
        << std::format(format, args...);
}

auto main() -> int {
    tprintf("hello {}", "world"); // prints "example.cpp:12: hello world"
}

If C++20 is not an option (especially at this current time, compilers don't support source location) then there is a way to do it without macros. You can simply do a little indirection over the compiler built-ins.

You cannot put defaulted arguments after the variadic arguments, but you can put them in the constructor of the first parameter to have the same effect:

#include <cstdio>

struct location_and_format {
    constexpr location_and_format(
        char const* _format,
        char const* _file_name = __builtin_FILE(),
        unsigned _line = __builtin_LINE()
    ) noexcept : format{_format}, file_name{_file_name}, line{_line} {}

    char const* format;
    char const* file_name;
    unsigned line;
};

template<typename... Targs>
void tprintf(location_and_format format, Targs... args) {
    printf("%s:%u: ", format.file_name, format.line);
    printf(format.format, args...);
}

int main() {
    tprintf("hello %s", "world"); // prints example.cpp:22: hello world
}

Live example

like image 122
Guillaume Racicot Avatar answered Oct 23 '25 01:10

Guillaume Racicot


How about using variadic macros?

template<typename... Targs>
void tprintfhelper(const char* f, int l, const char* format, Targs... ) 
{
    std::cout << format << " " << l << " " << f << std::endl;
}

#define tprintf(...) tprintfhelper(__builtin_FILE(), __builtin_LINE() __VA_OPT__(,) ##__VA_ARGS__)

int main()
{
    tprintf("%d", 1, 2, 3, 4, 5, "dog");
}
like image 36
A.Hristov Avatar answered Oct 23 '25 01:10

A.Hristov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!