功能模板检查 [英] Function template checking
问题描述
大家好,
我写了一个功能模板。说
模板< class T>
fn(T var){...}
是有什么办法,从函数内部,我们可以检查我们传递给函数的那个类型的
参数(double,float等)??
提前致谢,
苏里亚
Hi all,
I''ve written a function template. say
template <class T>
fn (T var) { ... }
Is there any way, from within the function, can we check what type of
argument we''ve passed on to the function (double, float etc) ??
Thanks in advance,
Surya
推荐答案
2003年9月16日01:23:30 -0700,< a href =mailto:sk*@fluent.co.in> sk *@fluent.co.in (Surya Kiran)写道:
On 16 Sep 2003 01:23:30 -0700, sk*@fluent.co.in (Surya Kiran) wrote:
有什么办法吗? ,从函数中,我们可以检查我们传递给[模板]函数(double,float等)的参数是什么类型的?
Is there any way, from within the function, can we check what type of
argument we''ve passed on to [a template] function (double, float etc)?
是的。
我认为最简单的是指针的dynamic_cast,在这个
上下文中不会是运行时操作。
但是你想要做什么呢?
几乎100%肯定有更简单的方法可以做任何事情。
一般建议:当某些事情变得困难时,请考虑这种方法。
Yes.
The easiest I think would be dynamic_cast of pointers, which in this
context would not be a runtime operation.
But what is it that you want to do that for?
It''s nearly 100% certain that there are easier ways to do whatever it is.
General advice: when something is "hard", consider the approach.
Surya Kiran写道:
Surya Kiran wrote:
大家好,
我写了一个功能模板。说
模板< class T>
fn(T var){...}
有没有办法,从函数内部,我们可以检查我们传递给函数的是什么类型的
参数(double,float等)??
Hi all,
I''ve written a function template. say
template <class T>
fn (T var) { ... }
Is there any way, from within the function, can we check what type of
argument we''ve passed on to the function (double, float etc) ??
通常你不会这样做,但你专门为你的模板中的类型
问题。
-
Attila又名WW
Usually you don''t do that, but you specialize your template for the type in
question.
--
Attila aka WW
" Alf P. Steinbach" <人*** @ start.no>在消息中写道
news:3f **************** @ News.CIS.DFN.DE ...
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On 2003年9月16日01:23:30 -0700, sk *@fluent.co.in (Surya Kiran)写道:
On 16 Sep 2003 01:23:30 -0700, sk*@fluent.co.in (Surya Kiran) wrote:
有没有办法,从函数中,我们可以检查我们传递给[模板]函数(double,float等)的论证类型是什么?
Is there any way, from within the function, can we check what type of
argument we''ve passed on to [a template] function (double, float etc)?
是的。
我认为最简单的是指针的dynamic_cast,在这个上下文中不会是运行时操作。
Yes.
The easiest I think would be dynamic_cast of pointers, which in this
context would not be a runtime operation.
不,dynamic_cast仅适用于多态类型,不适用于双打,浮点数等;
但是,typeid适用于编译时非多态类型:
if(typeid(foo)== typeid(float))
doSomething();
这是一种方法做到这一点。我同意另一张海报,专业是
首选。
No, dynamic_cast is only for polymorphic types, not doubles, floats, etc;
however, typeid works for nonpolymorphic types at compile time:
if (typeid(foo) == typeid(float))
doSomething();
This is one way to do it. I agree with another poster that specialization is
preferable.
这篇关于功能模板检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!