天天看点

sizeof 可变参数的模版特化 C++ templa

1 sizeof 测量函数返回值大小的时候,是不执行函数的

2 可变参数列表可以没有一个参数,只是不能用参数了(这句不知到对不对)

3 可变参数列表再模版实例化过程中比较弱;

#include <iostream>

using namespace std;



long long fun1(...)
{
	cout << "in fun1 class" << endl;
	return 3l;
}

class MyClass{
public:
	typedef int x;
};



template <typename T>
char tmpfun1(typename T::x);

template <typename T>
long long tmpfun1(...);

#define has_member_of_X(T) (sizeof(tmpfun1<T>(0)) == 1)

int main()
{
	fun1(1);
	fun1("adfdf");
	cout << "sizeof(fun1) = " << sizeof(fun1(2)) << endl;

	if(has_member_of_X(int))
		cout << "int has type member x" << endl;
	else
		cout << "int has not member x" << endl;

	if(has_member_of_X(MyClass))
		cout << "MyClass has type member x"<< endl;
	else
		cout << "MyClass has not type member x" << endl;

	int i;
	cin >> i;
}
           
sizeof 可变参数的模版特化 C++ templa
c++

继续阅读