浮点类型为非类型的模板专业化 [英] Template specialization with float as non type

查看:107
本文介绍了浮点类型为非类型的模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


C ++范本的第4.3节
状态不能使用
浮点文字(和简单的
常数浮点表达式)
作为模板参数具有历史
原因。'


同样,


$ 14.1 /类型
模板参数不应该是
声明为浮点,
类或void类型。[示例:




  template< double d> class X; // error 
template< double * pd> class Y; // OK
template< double& ; rd> class Z; // OK





  1. 看看为什么Y和Z是有效的而不是X,是整个与非浮动类型的模板参数相关的挑战需要做什么与指针/引用?


  2. 为什么模板非类型参数不能是类类型?



解决方案

这可能很难选择正确的范本实例化,因为可能的舍入误差。



请考虑以下内容:

  template< float n& 
void f(n){...} //版本1

模板< 0.3333>
void f(){...} // Version 2:Specialization for 0.3333

f(1/3); - >将调用哪个版本?



p>

 模板< float f> class foo {...}; 
foo< 1E6 + 1E-6> my_foo;

编译器应该生成什么?编​​译器必须知道目标的细节
浮点架构能够运行实例化模板
如果编译器在目标
体系结构上运行,这很容易,它可以只是做计算并找出答案,但
如果你是交叉编译,编译器必须能够合成
每个目标架构的浮点行为。
幸运的是,标准委员会决定这是不合理的。 / p>

无耻地从此处


为什么模板非类型参数不能是类类型


根据我的理解非类型参数不能是类类型,因为可能有一个类的多个实现。例如

  template< typename T> 
class demo {...};

模板<>
class demo< int> {...};


template< typename T,demo d> // which demo ?? demo< T>或demo< int>
class Example {...};

本地类不能用作模板参数,因为他们没有外部链接


Section 4.3 of C++ Templates states 'Not being able to use floating-point literals (and simple constant floating-point expressions) as template arguments has historical reasons.'

Similarly,

$14.1/7 states - "A non-type template-parameter shall not be declared to have floating point, class, or void type. [ Example:

template<double d> class X; // error
template<double* pd> class Y; // OK
template<double& rd> class Z; // OK"

  1. What is the historical reason that is being talked about in the book in the above quote?

  2. Looking at why Y and Z are valid but not X, is the whole challenge related to having non type template parameters of floating type got to do anything with pointers/references?

  3. Why template non type parameters can not be of class type?

解决方案

It could be hard to pick the right template instantitiation, because of possible roundoff errors .

Consider the following:

template<float n> 
void f(n) {...}  //Version 1

template<0.3333> 
void f() { ...} // Version 2:Specialization for 0.3333 

f(1/3); -> Which version would be called?

Consider the following code:

template <float f> class foo { ... }; 
foo<1E6 + 1E-6> my_foo; 

" What should the compiler generate? The compiler has to know about the details of target floating-point architecture to be able to run instantiate the template. This is easy enough if the compiler is running on the target architecture, it can just do the calculation and find out the answer, but if you are cross-compiling, the compiler would have to be able to synthesise the floating point behaviour of every intended target architecture. And thankfully the Standards Committee decided that this would be unreasonable. "

Shamelessly copied from here.

Why template non type parameters can not be of class type

As per my understanding a non-type paramater cannot be of class type because there may be more than one implementation of a class. For example

template <typename T>   
class demo{...};

template <>    
class demo<int>{...};


template <typename T, demo d> //which demo?? demo<T> or demo<int>
class Example{...};

Local classes cannot be used as template parameters because they don't have external linkage.

这篇关于浮点类型为非类型的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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