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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章