mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-04 19:14:07 +08:00
support bazel complie this project and format code.
This commit is contained in:
34
cpp2.0/cpp11/variadic/variadic2.cpp
Normal file
34
cpp2.0/cpp11/variadic/variadic2.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by light on 19-11-4.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void print(const char *s) {
|
||||
while (*s) {
|
||||
if (*s == '%' && *(++s) != '%')
|
||||
throw std::runtime_error("invalid format string: missing arguments");
|
||||
std::cout << *s++;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void print(const char *s, T value, Args... args) {
|
||||
while (*s) {
|
||||
if (*s == '%' && *(++s) != '%') {
|
||||
std::cout << value;
|
||||
print(++s, args...); // 即便当 *s == 0 也会产生调用,以检测更多的类型参数。
|
||||
return;
|
||||
}
|
||||
std::cout << *s++;
|
||||
}
|
||||
throw std::logic_error("extra arguments provided to printf");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int *pi = new int;
|
||||
print("%d %s %p %f\n", 15, "Acc", pi, 3.14);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user