函数检查整数类型是否可以适合可能不同(整数)类型的值 [英] Function checking if an integer type can fit a value of possibly different (integer) type

查看:122
本文介绍了函数检查整数类型是否可以适合可能不同(整数)类型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个模板函数来检查基本数据类型是否可以适合潜在不同的基本数据类型的值?让我们暂时将范围限制为整数类型。



更精确地说:可以创建一个一个适合所有模板函数,但没有得到编译器警告(布尔表达式总是真/假,有符号/无符号比较,未使用的变量),并且不禁用编译器警告检查?这些函数应该尽可能地限制在运行时的检查(在编译时应该排除所有琐碎的情况)。如果可能,我宁愿避免使用来自C ++ 11等的扩展(除非存在旧C ++的快速替换)。



注意:值在编译时是未知的,只有它的类型。



预期行为的示例:

  int main(int argc,char ** argv){
for(int i = 1; i const int value = atoi(argv [一世]);
std :: cout<<值<< :;
std :: cout<< CanTypeFitValue< int8_t>(value)<< ;
std :: cout<< CanTypeFitValue< uint8_t>(value)<< ;
std :: cout<< CanTypeFitValue< int16_t>(value)<< ;
std :: cout<< CanTypeFitValue< uint16_t>(value)<< ;
std :: cout<< CanTypeFitValue< int32_t>(value)<< ;
std :: cout<< CanTypeFitValue< uint32_t>(value)<< ;
std :: cout<< CanTypeFitValue< int64_t>(value)<< ;
std :: cout<< CanTypeFitValue< uint64_t>(value)<< std :: endl;
}

}



./a.out 6 1203032847 2394857 -13423 9324 -192992929

6:1 1 1 1 1 1 1 1

1203032847:0 0 0 0 1 1 1 1

2394857:0 0 0 0 1 1 1 1

-13423:0 0 1 0 1 0 1 0

9324:0 0 1 1 1 1 1 1

-192992929:0 0 0 0 1 0 1 0

测试您的代码此处此处。 / p>

检查生成的程序集此处



此问题的灵感来自此帖



更紧凑,我第一个解决方案,效率相同。



缺点:包括一个额外的标题。

  #include< limit> 
#include< stdint.h>

使用std :: numeric_limits;

template< typename T,typename U>
bool CanTypeFitValue(const U value){
const intmax_t botT = intmax_t(numeric_limits< T> :: min());
const intmax_t botU = intmax_t(numeric_limits< U> :: min());
const uintmax_t topT = uintmax_t(numeric_limits< T> :: max());
const uintmax_t topU = uintmax_t(numeric_limits< U> :: max());
return((botT> botU&& value< static_cast< U>(botT))||(topT< topU&& value> static_cast& ;
}

生成汇编代码(可以更改T和U类型)



正确性测试



注意:写了一个 constexpr版本,但是显然它有一些问题。请参阅此处此处


Is it possible to create a templated function that checks if a primitive data type can fit a value of potentially different primitive data type? Let's limit the scope to integer types for the moment.

More precisely: Is it possible to create a "one fit all" templated functions yet without getting compiler warnings (boolean expression always true/false, signed/unsigned comparison, unused variable) and without disabling compiler warning checks? The functions should also limit as much as possible checks at runtime (all trivial cases should be excluded at compile time). If possible, I would prefer avoiding using extensions from C++11 and the like (unless a "quick" replacement for "old" C++ exists).

Note: "value" is not known at compile time, only its type.

Example of expected behaviour:

int main(int argc, char** argv) {
    for (int i = 1; i < argc; i++) {
        const int value = atoi(argv[i]);
        std::cout << value << ": ";
        std::cout << CanTypeFitValue<int8_t>(value) << " ";
        std::cout << CanTypeFitValue<uint8_t>(value) << " ";
        std::cout << CanTypeFitValue<int16_t>(value) << " ";
        std::cout << CanTypeFitValue<uint16_t>(value) << " ";
        std::cout << CanTypeFitValue<int32_t>(value) << " ";
        std::cout << CanTypeFitValue<uint32_t>(value) << " ";
        std::cout << CanTypeFitValue<int64_t>(value) << " ";
        std::cout << CanTypeFitValue<uint64_t>(value) << std::endl;
        }

}



./a.out 6 1203032847 2394857 -13423 9324 -192992929

6: 1 1 1 1 1 1 1 1

1203032847: 0 0 0 0 1 1 1 1

2394857: 0 0 0 0 1 1 1 1

-13423: 0 0 1 0 1 0 1 0

9324: 0 0 1 1 1 1 1 1

-192992929: 0 0 0 0 1 0 1 0

Test your code here or here.

Check the assembly generated here.

This question was inspired by this post

解决方案

Using numeric_limits and types defined in stdint.h

More compact that my first solution, same efficiency.

Drawback: one additional header to be included.

#include <limits>
#include <stdint.h>

using std::numeric_limits;

template <typename T, typename U>
    bool CanTypeFitValue(const U value) {
        const intmax_t botT = intmax_t(numeric_limits<T>::min() );
        const intmax_t botU = intmax_t(numeric_limits<U>::min() );
        const uintmax_t topT = uintmax_t(numeric_limits<T>::max() );
        const uintmax_t topU = uintmax_t(numeric_limits<U>::max() );
        return !( (botT > botU && value < static_cast<U> (botT)) || (topT < topU && value > static_cast<U> (topT)) );        
    }

Assembly code generated (you can change T and U types)

Correctness test


Note: a constexpr version was written, but apparently it has some problems. See here and here.

这篇关于函数检查整数类型是否可以适合可能不同(整数)类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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