检查模板功能中的整数。 [英] Check for whole number in template function.

查看:52
本文介绍了检查模板功能中的整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的新闻组。


我想写一个模板函数,它接受整数或

浮点数。

如果某个结果不是一个整数,并且模板

参数是一个整数,它应该返回false,但是如果参数是一个浮点数它应该正常工作




为了说明这一点,我附上了一个最小的例子。


背景:

我有一个变量(在这种情况下,temp)保存一些

计算的结果并以毫秒为单位表示时间。

然而,输出应以秒为单位,并且功能应该只有

成功返回,如果值可以用秒表示

(没有任何舍入)。


代码示例下面做了所有这些,但我想知道是否有更好的方法来实现它,如果这个代码适用于所有系统。


是否有可能那个if&q uot; -clause被优化掉了吗?

我使用带有-O3选项的gcc编译它仍然有效。


有何评论?


欢呼,

马蒂亚斯


=================== ===================

#include< iostream>


模板< typename T>

bool minimal(T&输出)

{

T temp = 500; //改变这个值!

if(temp / 1000 * 1000!= temp)返回false;

else output = temp / 1000;

}


int main()

{

int i;

if(minimal( i))std :: cout<< " int works("<< i<<"))!\ n" ;;

float f;

if(minimal(f) )std :: cout<< float works("<< f<<")!\ n";

}

解决方案

Matthias写道:


亲爱的新闻组。


我想写一个接受整数的模板函数或者

浮点数。

如果某个结果不是整数,并且模板

参数是一个整数,它应该返回false,但是如果参数是float,它应该正常工作




也许你最好提供你的功能专业化

模板或只是两个重载函数?


>

为了说明这一点,我附上了一个最小的例子。


背景:

我有一个变量(在这种情况下是temp),其中包含一些

计算的结果,并以毫秒为单位表示时间。

然而,输出应为如果价值可以用秒表示

(没有任何舍入),那么该函数应该只有几秒钟,并且该函数应该只有

成功返回。


下面的代码示例完成了所有这些,但我想知道是否有更好的方法来执行此操作以及此代码是否适用于所有系统。


是否有可能将if-clause优化掉了?

我使用带有-O3选项的gcc编译它并且它仍然有效。


有何评论?

欢呼,

Matthias


================ ======================

#include< iostream>


模板< typename T>

bool minimal(T&输出)

{

T temp = 500; //改变这个值!

if(temp / 1000 * 1000!= temp)返回false;

else output = temp / 1000;

}



所以,有什么问题,比如说


bool minimal(int&){return false; }

bool minimal(float& f){f = 0.5;返回true; }




>

int main()

{

int i;

if(minimal(i))std :: cout<< " int works("<< i<<"))!\ n" ;;

float f;

if(minimal(f) )std :: cout<< float works("<<<<<")!\ n" ;;

}



V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问


11月6日,8:22 * pm,Matthias< Matthias.Ge ... @ gmail.comwrote:


亲爱的新闻组。


我想写一个模板函数,它接受整数或

浮点数数字。

如果某个结果不是一个整数,并且模板

参数是一个整数,它应该返回false,但它应该工作

通常如果参数是浮点数。


为了说明这一点,我附上了一个最小的例子。


背景:

我有一个变量(在这种情况下是temp),其中包含一些

计算结果并表示磨机中的时间iseconds。

然而,输出应该是以秒为单位的,如果值可以用秒表示,该函数应该只返回成功返回


(没有任何舍入)。


下面的代码示例完成所有这些,但我想知道是否有更好的方法来实现它,如果有的话这段代码适用于所有系统。


是否有可能将if-clause优化掉了?

我用gcc编译它 - O3选项,它仍然有效。



您可以查看限制标题中的函数,例如:


#include< limits>

if(numeric_limits< T> :: is_integer)

....


Joe Cook


11月7日凌晨3:13,joseph cook< joec ... @ gmail.comwrote:


11月6日, 8:22 pm,Matthias< Matthias.Ge ... @ gmail.comwrote:


我想要写一个模板函数,接受

整数或浮点数。

如果某个结果不是整数,那么

模板参数是一个整数,它应该返回false,

但是如果参数是float,它应该正常工作。


为了说明这一点,我附上了一个最小的例子。


背景:

我有一个变量(在此casetemp)保存结果

的一些计算并用

毫秒表示时间。然而,输出应该以秒为单位,并且

如果值

可以用秒表示(没有任何舍入),则该函数应该只能成功返回。


下面的代码示例完成所有这些,但我想知道

如果有更好的方法,并且此代码适用于所有系统的b / b


是否有可能将if-clause优化掉?我用bcc用-O3选项编译了它,它仍然有效。


您可以查看限制标题中的函数,如:


#include< limits>

if(numeric_limits< T> :: is_integer)

....



请注意,numeric_limits< T> :: is_integer不是函数,而

标准要求它可用作整数

常量表达。所以你可以专注于它,然后写上

之类的东西:


template< bool是struct Discriminater {};


template< typename T>

bool

functionHelper(T in,Discriminater< true)

{

//句柄整数案例......

}


模板< typename T>

bool

functionHelper(T in,Discriminater< false)

{

//句柄浮点...

}


模板< typename T>

bool

函数(T in)

{

// ...

functionHelper(

in,Discriminater< std :: numeric_limits< T> :: is_integer


>( ));



// ...

}


(但取决于他''做,一个简单的运行时检查可能

就足够了,而且要简单得多。)


-

James Kanze (GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9个地方Sémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34


Dear newsgroup.

I want to write a template function which accepts either integer or
floating point numbers.
If a certain result is not a whole number and if the template
parameter is an integer, it should return false, but it should work
normally if the parameter is a float.

To illustrate this, I attached a minimal example.

The background:
I have a variable (in this case "temp") holding the result of some
calculations and representing a time in milliseconds.
The output, however, shall be in seconds, and the function should only
return successfully if the value can be represented in seconds
(without any rounding).

The code example below does all that, but I wanted to know if there is
a better way to do it and if this code works on all systems.

Is it possible that the "if"-clause is optimized away?
I compiled it with gcc with the -O3 option and it still works.

Any comments?

cheers,
Matthias

======================================

#include <iostream>

template <typename T>
bool minimal(T& output)
{
T temp = 500; // change this value!
if (temp / 1000 * 1000 != temp) return false;
else output = temp / 1000;
}

int main()
{
int i;
if (minimal(i)) std::cout << "int works (" << i << ")!\n";
float f;
if (minimal(f)) std::cout << "float works (" << f << ")!\n";
}

解决方案

Matthias wrote:

Dear newsgroup.

I want to write a template function which accepts either integer or
floating point numbers.
If a certain result is not a whole number and if the template
parameter is an integer, it should return false, but it should work
normally if the parameter is a float.

Perhaps you''re better off providing specialisations of your function
template or simply two overloaded functions?

>
To illustrate this, I attached a minimal example.

The background:
I have a variable (in this case "temp") holding the result of some
calculations and representing a time in milliseconds.
The output, however, shall be in seconds, and the function should only
return successfully if the value can be represented in seconds
(without any rounding).

The code example below does all that, but I wanted to know if there is
a better way to do it and if this code works on all systems.

Is it possible that the "if"-clause is optimized away?
I compiled it with gcc with the -O3 option and it still works.

Any comments?

cheers,
Matthias

======================================

#include <iostream>

template <typename T>
bool minimal(T& output)
{
T temp = 500; // change this value!
if (temp / 1000 * 1000 != temp) return false;
else output = temp / 1000;
}

So, what''s wrong with, say

bool minimal(int&) { return false; }
bool minimal(float& f) { f = 0.5; return true; }

?

>
int main()
{
int i;
if (minimal(i)) std::cout << "int works (" << i << ")!\n";
float f;
if (minimal(f)) std::cout << "float works (" << f << ")!\n";
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Nov 6, 8:22*pm, Matthias <Matthias.Ge...@gmail.comwrote:

Dear newsgroup.

I want to write a template function which accepts either integer or
floating point numbers.
If a certain result is not a whole number and if the template
parameter is an integer, it should return false, but it should work
normally if the parameter is a float.

To illustrate this, I attached a minimal example.

The background:
I have a variable (in this case "temp") holding the result of some
calculations and representing a time in milliseconds.
The output, however, shall be in seconds, and the function should only
return successfully if the value can be represented in seconds
(without any rounding).

The code example below does all that, but I wanted to know if there is
a better way to do it and if this code works on all systems.

Is it possible that the "if"-clause is optimized away?
I compiled it with gcc with the -O3 option and it still works.

You could look at functions from the limits header like:

#include <limits>
if (numeric_limits<T>::is_integer)
....

Joe Cook


On Nov 7, 3:13 am, joseph cook <joec...@gmail.comwrote:

On Nov 6, 8:22 pm, Matthias <Matthias.Ge...@gmail.comwrote:

I want to write a template function which accepts either
integer or floating point numbers.
If a certain result is not a whole number and if the
template parameter is an integer, it should return false,
but it should work normally if the parameter is a float.

To illustrate this, I attached a minimal example.

The background:
I have a variable (in this case "temp") holding the result
of some calculations and representing a time in
milliseconds. The output, however, shall be in seconds, and
the function should only return successfully if the value
can be represented in seconds (without any rounding).

The code example below does all that, but I wanted to know
if there is a better way to do it and if this code works on
all systems.

Is it possible that the "if"-clause is optimized away? I
compiled it with gcc with the -O3 option and it still works.

You could look at functions from the limits header like:

#include <limits>
if (numeric_limits<T>::is_integer)
....

Note that numeric_limits<T>::is_integer is NOT a function, and
that the standard requires it to be usable as an integral
constant expression. So you can specialize on it, and write
something like:

template< bool Is struct Discriminater {} ;

template< typename T >
bool
functionHelper( T in, Discriminater< true )
{
// handle integer case...
}

template< typename T >
bool
functionHelper( T in, Discriminater< false )
{
// handle floating point...
}

template< typename T >
bool
function( T in )
{
// ...
functionHelper(
in, Discriminater< std::numeric_limits< T >::is_integer

>() ) ;

// ...
}

(But depending on what he''s doing, a simple runtime check might
be sufficient, and is a lot simpler.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于检查模板功能中的整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆