[c++模板] 用可变参数模板来实现 printf

/ 5,672评论 / 21064阅读 / 3点赞

模板

// 普通函数, 只能接受 int 类型参数,如果传入其他类型需要类型转换
int sum(int a, int b) {
    return (a + b);
}

double sum(double a, double b) {
    return (a + b);
}
// 模板
template<typename T>
T sum(T a, T b) {
    return (a + b);
}

// 编译会生成:
int sum(int a, int b) {
    return (a + b);
}
double sum(double a, double b) {
    return (a + b);
}

可变参数模板

int sum(int a, int b, int c) {
    return (a + b + c);
}

int sum(int a, int b, int c, int d) {
    return (a + b + c + d);
}
// ......
template<typename T>
T sum(T... args) {
    // 展开求和
}
template<typename T>
T mysum(T item) {
    return item;
}
template<typename T>
T mysum(T item, T... args) {
    return mysum(item) + mysum(args);
}
/// 入口
template<typename T>
T sum(T... args) {
    return mysum(args);
}
int sum(int arg0, int arg1, int arg2, int arg3, int arg4);
int mysum(int item, int arg1, int arg2, int arg3, int arg4);
int mysum(int item);
int mysum(int item, int arg2, int arg3, int arg4);
int mysum(int item, int arg3, int arg4);
int mysum(int item, int arg4);

不定类型数量

template<typename T1, typename T2, typename ...Args>
T1 mysum(T2 item, Args... args);

实现printf

template<typename T>
int printNum(const std::string& str, int index, T&& item) {
	auto doShift = true;
	while (index < str.size()) {
		if (index == (str.size() - 1) || str[index] != '%') {
			cout << str[index];
			++index;
		}
		else if(doShift) {
			doShift = false;
			++index;
			switch (str[index]) {
			case 's':
			case 'd':
				cout << item;
				break;
			}
			++index;
		}
		else {
			return index - 1;
		}
	}
	return index - 1;
}

template<typename T, typename... Args>
int printNum(const std::string& str, int index, T&& item, Args&&... args) {
	index = printNum(str, index, item);
	return printNum(str, index + 1, std::forward<Args>(args)...);
}

template<typename ...Args>
void myPrintNum(const std::string& str, Args&&... args) {
	int index = 0;
	while (index < str.size()) {
		if (str[index] != '%') {
			cout << str[index];
			++index;
		}
		else {
			break;
		}
	}
	index = printNum(str, index, std::forward<Args>(args)...);
	++index;
	while (index < str.size()) {
		cout << str[index];
		++index;
	}
}

int main() {

        // 调用,参数数量允许和字符串内数量不同,不会出错
	myPrintNum("output: %d, %d, %d, %d  ---", 123, 100.123, true);
	return 0;
}
  1. As you purchase new True Followers, you may also add many more Lesser Fans.

  2. Companies for Rosa Nell Caughron (Montanez), 60, can be at eleven a.m.

  3. Then make the higher move and see how issues go.

  4. Take a look at my emails with Spanish tales, ideas, and be informed about next occasions…

  5. JaimeNer说道:

    cipro buy cipro online usa cipro pharmacy

  6. You may even personal a bulk sms website and promote to people all over Nigeria and beyond.

  7. 犬 亡くなる说道:

    So, visiting Madrid, I get the feeling of a trendy era metropolis.

  8. This was principally on account of the higher situations offered to the gamers by the American organizers.

  9. Keep in thoughts others will see your comments with adverse reviewers so it is an important opportunity to showcase your brand as skilled and courteous.

  10. Place after 20.Rb1. Carlsen (White) previously supplied a second pawn sacrifice with the move 19.Nd6, Hou Yifan felt that if calculated appropriately, was acceptable on this place.

  11. These couple Rakhi can be found in varied interesting shapes and designs decorated beautifully with stones and gems.

  12. Compatible with the Protovision 4 participant interface / the Classical Video games Adapter, Digital Excess & Hitmen 4 player joystick adapter and the Kingsoft Adapter.

  13. First Security Group, Inc, the biggest Chattanooga-primarily based financial institution, formed in 2000, can be acquired by the Atlanta-primarily based Atlantic Capital Bancshares, Inc., for $160 million.

  14. JimmieInawl说道:

    http://lisinoprilus.com/# buy lisinopril 20 mg without prescription

  15. RobertUnsuh说道:

    buy ciprofloxacin: cipro 500mg best prices – cipro pharmacy

  16. JaimeNer说道:

    cytotec abortion pill buy cytotec pills online cheap buy cytotec online

  17. ArmandoLiaig说道:

    lisinopril cost 40 mg: lisinopril 2.5 mg medicine – buy cheap lisinopril

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注