Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Created February 9, 2014 21:24
Show Gist options
  • Save Fiona-J-W/8906137 to your computer and use it in GitHub Desktop.
Save Fiona-J-W/8906137 to your computer and use it in GitHub Desktop.
converting-variadics-wrapper
#include <cstdio> // printf
struct mytype {
int value;
operator int() const {return value;}
};
template<typename T> struct argument_type_helper{using type = T;};
template<> struct argument_type_helper<mytype>{using type = int;};
template<typename T> using argument_type = typename argument_type_helper<T>::type;
template<typename T> argument_type<T> argument_cast(const T& arg) {return arg;}
template<typename...Args>
void converting_printf(const char* format, const Args&...args) {
std::printf(format, argument_cast(args)...);
}
int main() {
mytype myvar{42};
converting_printf("mytype: %d, some int: %d\n", myvar, 23);
}
@Fiona-J-W
Copy link
Author

Output:
mytype: 42, some int: 23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment