由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ template preprocessor
相关主题
这个结果是啥,为什么呢?what kind of language this is and how to change it? (转载)
template关于程序必须支持win and linux, 可不可以用class而不是#ifdef WIN32 ?
问个g++的问题深情的呼唤师傅们!C++做题做不出来啦!
数组问题how to apply OOD to a code for both win and linux platform ?
where to define my template function问低级问题
C++ class template specialization questionC++的一个问题
One c++ non-type template questionquestion about mock
How to tell gcc stop compiling.boost serialization的问题
相关话题的讨论汇总
话题: pt话题: void话题: takedouble话题: takefloat
进入Programming版参与讨论
1 (共1页)
h**********c
发帖数: 4120
1
Try to figure it out but failed,as the following:
void takefloat (float * ) {}
void takedouble (double *) {}
template
void dosomething (T * pt)
{
#if sizeof(T) == sizeof(float)
takefloat (pt);
#else
takedouble(pt);
#endif
}
Compilation failed.
A**u
发帖数: 2458
2
我觉得,你这个不行的原因在
#if sizeof(T) == sizeof(float)
sizeof(T)要运行时才知道
你改成
#if XXX
#else
#endif
编译时候加上 g++ -DXXX 选择float, g++ 选择double
或者
你改成void take (float * ) {}
void take (double *) {}
template
void dosomething (T * pt)
{
take(pt);
}
这样可行吗

【在 h**********c 的大作中提到】
: Try to figure it out but failed,as the following:
: void takefloat (float * ) {}
: void takedouble (double *) {}
: template
: void dosomething (T * pt)
: {
: #if sizeof(T) == sizeof(float)
: takefloat (pt);
: #else
: takedouble(pt);

h*******s
发帖数: 8454
3
好像这样就可以了
怎么还用#来注释啊。。。

takefloat ((float *)pt);
takedouble((double *)pt);

【在 h**********c 的大作中提到】
: Try to figure it out but failed,as the following:
: void takefloat (float * ) {}
: void takedouble (double *) {}
: template
: void dosomething (T * pt)
: {
: #if sizeof(T) == sizeof(float)
: takefloat (pt);
: #else
: takedouble(pt);

t****t
发帖数: 6806
4
for metaprogramming, there is no "if". condition is always implicit. for
your example (as a simple function), you should write:
void dosomething(float* pt) { takefloat(pt); }
template void dosomething(T* pt) { takedouble(pt); }
although you may really want to write
void dosomething(float* pt) { takefloat(pt); }
void dosomething(double* pt) { takedouble(pt); }

【在 h**********c 的大作中提到】
: Try to figure it out but failed,as the following:
: void takefloat (float * ) {}
: void takedouble (double *) {}
: template
: void dosomething (T * pt)
: {
: #if sizeof(T) == sizeof(float)
: takefloat (pt);
: #else
: takedouble(pt);

h**********c
发帖数: 4120
5
Try to figure it out but failed,as the following:
void takefloat (float * ) {}
void takedouble (double *) {}
template
void dosomething (T * pt)
{
#if sizeof(T) == sizeof(float)
takefloat (pt);
#else
takedouble(pt);
#endif
}
Compilation failed.
A**u
发帖数: 2458
6
我觉得,你这个不行的原因在
#if sizeof(T) == sizeof(float)
sizeof(T)要运行时才知道
你改成
#if XXX
#else
#endif
编译时候加上 g++ -DXXX 选择float, g++ 选择double
或者
你改成void take (float * ) {}
void take (double *) {}
template
void dosomething (T * pt)
{
take(pt);
}
这样可行吗

【在 h**********c 的大作中提到】
: Try to figure it out but failed,as the following:
: void takefloat (float * ) {}
: void takedouble (double *) {}
: template
: void dosomething (T * pt)
: {
: #if sizeof(T) == sizeof(float)
: takefloat (pt);
: #else
: takedouble(pt);

h*******s
发帖数: 8454
7
好像这样就可以了
怎么还用#来注释啊。。。

takefloat ((float *)pt);
takedouble((double *)pt);

【在 h**********c 的大作中提到】
: Try to figure it out but failed,as the following:
: void takefloat (float * ) {}
: void takedouble (double *) {}
: template
: void dosomething (T * pt)
: {
: #if sizeof(T) == sizeof(float)
: takefloat (pt);
: #else
: takedouble(pt);

t****t
发帖数: 6806
8
for metaprogramming, there is no "if". condition is always implicit. for
your example (as a simple function), you should write:
void dosomething(float* pt) { takefloat(pt); }
template void dosomething(T* pt) { takedouble(pt); }
although you may really want to write
void dosomething(float* pt) { takefloat(pt); }
void dosomething(double* pt) { takedouble(pt); }

【在 h**********c 的大作中提到】
: Try to figure it out but failed,as the following:
: void takefloat (float * ) {}
: void takedouble (double *) {}
: template
: void dosomething (T * pt)
: {
: #if sizeof(T) == sizeof(float)
: takefloat (pt);
: #else
: takedouble(pt);

h**********c
发帖数: 4120
9
Thanks!
Overload take(...) may save some time.
1 (共1页)
进入Programming版参与讨论
相关主题
boost serialization的问题where to define my template function
C++ template problemC++ class template specialization question
一个C++ template的问题One c++ non-type template question
师傅们,C++概念题,弟子有礼了How to tell gcc stop compiling.
这个结果是啥,为什么呢?what kind of language this is and how to change it? (转载)
template关于程序必须支持win and linux, 可不可以用class而不是#ifdef WIN32 ?
问个g++的问题深情的呼唤师傅们!C++做题做不出来啦!
数组问题how to apply OOD to a code for both win and linux platform ?
相关话题的讨论汇总
话题: pt话题: void话题: takedouble话题: takefloat