l********n 发帖数: 54 | 1 来自主题: JobHunting版 - C++问题 great! Then, following code should work.
#include
#include
using namespace std;
template
inline void PrintSize()
{
cout<<"size of "<< typeid(T).name()<<" is " << sizeof(T)<
}
struct A
{
int a;
double b;
};
int main(void)
{
PrintSize();
PrintSize();
PrintSize();
return 1;
} |
|
h*****f 发帖数: 248 | 2 1. Compile time because template initialization is done during the compile
time and the initialization is based on the "known" template parameter(s)
during the compile time.
2. A way to prove:
#include
#include
template class Buffer {
char internal[max];
public:
void printSize() {
printf("buffer size=%lu\n", sizeof(internal));
}
};
int main() {
Buffer<512> my512;
my512.printSize();
size_t v;
std::cin >> v;
printf("my input=%lu\... 阅读全帖 |
|
m*****n 发帖数: 2152 | 3 来自主题: JobHunting版 - C++问题 如下的macro怎么用inline function改写?
#define PrintSize(type) \
cout << "size of " << #type << " is " << sizeof(type) << endl;
example:
PrintSize(char); will show "size of char is 1" . |
|
l********n 发帖数: 54 | 4 来自主题: JobHunting版 - C++问题 用template只能做到这样,不过#type想不出如何打印出来
#include
using namespace std;
template
inline void PrintSize()
{
cout<<"sizeof "<<" is " << sizeof(T)<
}
int main(void)
{
PrintSize();
return 1;
} |
|
M********5 发帖数: 715 | 5 来自主题: JobHunting版 - C++问题 #define PrintSize(type) \
do{ \
cout << "size of " #type << " is " << sizeof(type) << endl; \
} while(0) |
|