62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
#include <iostream>
|
|
|
|
template < typename T >
|
|
int spaceOf ( T x ) {
|
|
int bytes = sizeof(x);
|
|
return bytes/4 + (bytes%4>0); // size of memory cell is 4 bytes
|
|
}
|
|
/*
|
|
template < typename T >
|
|
int spaceOf () {
|
|
int bytes = sizeof(T);
|
|
return bytes/4 + (bytes%4>0);
|
|
}
|
|
*/
|
|
/*
|
|
template < typename T1, typename T2 >
|
|
void f ( T1 v1, T2 v2 ) {
|
|
std::cout << v1 << " " << v2 << std::endl;
|
|
}
|
|
*/
|
|
/*
|
|
template < int N, typename T >
|
|
T power ( T v ) {
|
|
T res = v;
|
|
for ( int i=1; i<N; i++ )
|
|
res *= v;
|
|
return res;
|
|
}
|
|
*/
|
|
|
|
|
|
int main() {
|
|
int x1=199;
|
|
double x2=2.99;
|
|
|
|
// implicit template instantiation
|
|
std::cout << spaceOf(x1) << std::endl;
|
|
std::cout << spaceOf(x2) << std::endl;
|
|
std::cout << "-------------" << std::endl;
|
|
/*
|
|
// explicit template instantiation
|
|
std::cout << spaceOf<int>() << std::endl;
|
|
std::cout << spaceOf<double>() << std::endl;
|
|
std::cout << "-------------" << std::endl;
|
|
std::cout << spaceOf<int>(x1) << std::endl;
|
|
std::cout << spaceOf<double>(x2) << std::endl;
|
|
*/
|
|
/*
|
|
f(2.2,1); // implicit
|
|
f<double,int>(2.2,1); // explicit complete
|
|
f<double>(2.2,1); // explicit incomplete
|
|
f<int,int>(2.2,1); // explicit complete
|
|
*/
|
|
/*
|
|
std::cout << power<3>(2) << std::endl;
|
|
std::cout << power<5, int>(1.2) << std::endl;
|
|
std::cout << power<5>(1.2) << std::endl;
|
|
*/
|
|
return 0;
|
|
}
|
|
|