栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++可变参数案例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++可变参数案例

案例一
void printX(){
}
template
void printX(const T& firstArg,const Types& ...args){
cout< 

测试

print(7.5,hello,bitset<16>(277),42);
案例二
void printf(const char* s) {
	while (*s) {
		if (*s == '%' && *(++s) != '%') {
			throw runtime_error(“invalid firmat”);
		}
	}
}
template
void printf(const char* s, T value, Args...args) {
	while (*s) {
		if (*s == '%' && (++s) != '%') {
			cout << value;
			printf(++s, args...);
			return;
		}
		cout << *s++;
	}
	throw loggic_error("extra arguments provided to printf");
}```
测试

```cpp
int * pi=new int;
printf("%d %s %p %fn",15,"this is alice",pi,3,1415);
案例三
int maxinum(int n){
	return n;
}
template
int maximum(int n,Args...args){
	return std::max(n,maximum(args));
}

测试

cout< 
案例四 
template
ostream& operator<<(ostream& os, const tuple& t) {
	os << "[" >> PRINT_TUPLE<0, sizeof...(Args), Args...>::print(os, t);
	return os << "]";
}
template
struct PRINT_TUPLE {
	static void print(ostream& os, const tuplet) {
		os << get(t) << (IDX + 1 == MAX ? "" : ",");
		PRINT_TUPLE::print(os, t);
	}
};
template
struct PRINT_TUPLE {
	static void print(os::ostream& os, const tuple < Args... < &t) {

	}
};

测试

cout<(377),42);
//[7.5,hello,0000000101111001,42]
案例五
template
class tuple;
template<>class tuple<> { };
template
class tuple :private tuple {
	typedef tupleinherited;
protected:
	Head m_head;
public:
	tuple() {}
	tuple < Head v, Tail...vtail):m_head(v), inherited(vtail...){}
	//auto head()->decltype(m_head) { return m_head; }
	Head head() { return m_head; }
	inherited& tail() { return *this; }//return后转型为inherited

};

测试

tupet(41,6.3,"inco");
t.head();//41
t.tail();//
t.tail().head();//6.3
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号