C++ 算术提升标头的使用

时间:2023-04-23
本文介绍了C++ 算术提升标头的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我一直在使用一组模板来确定给定 C++ 中的两种原始类型的正确提升类型.这个想法是,如果您定义一个自定义数字模板,您可以使用它们来确定返回类型,例如,基于传递给模板的类的 operator+ 函数.例如:

I've been playing around with a set of templates for determining the correct promotion type given two primitive types in C++. The idea is that if you define a custom numeric template, you could use these to determine the return type of, say, the operator+ function based on the class passed to the templates. For example:

// Custom numeric class
template <class T>
struct Complex {
    Complex(T real, T imag) : r(real), i(imag) {}
    T r, i;
// Other implementation stuff
};

// Generic arithmetic promotion template
template <class T, class U>
struct ArithmeticPromotion {
    typedef typename X type;  // I realize this is incorrect, but the point is it would
                              // figure out what X would be via trait testing, etc
};

// Specialization of arithmetic promotion template
template <>
class ArithmeticPromotion<long long, unsigned long> {
    typedef typename unsigned long long type;
}

// Arithmetic promotion template actually being used
template <class T, class U>
Complex<typename ArithmeticPromotion<T, U>::type>
operator+ (Complex<T>& lhs, Complex<U>& rhs) {
    return Complex<typename ArithmeticPromotion<T, U>::type>(lhs.r + rhs.r, lhs.i + rhs.i);
}

如果您使用这些促销模板,您可以或多或少地将您的用户定义类型视为对其应用了相同促销规则的原语.所以,我想我的问题是这会是有用的东西吗?如果是这样,为了便于使用,您希望模板化哪些类型的常见任务?我正在假设仅拥有促销模板不足以实际采用.

If you use these promotion templates, you can more or less treat your user defined types as if they're primitives with the same promotion rules being applied to them. So, I guess the question I have is would this be something that could be useful? And if so, what sorts of common tasks would you want templated out for ease of use? I'm working on the assumption that just having the promotion templates alone would be insufficient for practical adoption.

顺便说一下,Boost 在它的数学/工具/推广头文件中有一些类似的东西,但它实际上更多的是让值准备好传递给标准的 C 数学函数(期望 2 个整数或 2 个双精度数)并绕过所有积分类型.与完全控制对象的转换方式相比,这种简单的事情更可取吗?

Incidentally, Boost has something similar in its math/tools/promotion header, but it's really more for getting values ready to be passed to the standard C math functions (that expect either 2 ints or 2 doubles) and bypasses all of the integral types. Is something that simple preferable to having complete control over how your objects are being converted?

TL;DR:除了执行提升本身的机制之外,您希望在算术提升标头中找到哪些类型的帮助器模板?

TL;DR: What sorts of helper templates would you expect to find in an arithmetic promotion header beyond the machinery that does the promotion itself?

推荐答案

这绝对有用——我们在我工作的数学库中使用这些类型的东西来在表达式中正确键入中间值.例如,您可能有一个模板化的加法运算符:

This is definitely useful -- we use these sorts of things in the math library that I work on for correctly typing intermediate values in expressions. For example, you might have a templated addition operator:

template<typename Atype, typename Btype>
type_promote<Atype, Btype>::type operator+(Atype A, Btype B);

通过这种方式,您可以编写一个通用运算符来处理不同的参数类型,它会返回一个适当类型的值以避免出现它的表达式中的精度损失.它也很有用(在向量和) 用于在这些运算符中正确声明内部变量.

This way, you can write a generic operator that will handle different argument types, and it will return a value of the appropriate type to avoid precision loss in the expression that it appears in. It's also useful (in things like vector sums) for properly declaring internal variables within these operators.

至于这些应该怎么做的问题:我刚刚检查了我们定义它们的源代码,我们所拥有的只是您描述的简单 ArithmeticPromotion 声明——三个通用版本来解决复杂的问题——complex、complex-real 和 real-complex 变体使用特定的实-实变体,然后是实-实变体——总共大约 50 行代码.我们没有任何其他帮助模板,而且(从我们的使用情况来看)看起来我们不会使用任何自然的模板.

As for the question of what ought to go with these: I just checked in our source code where we define them, and all we have there are just the simple ArithmeticPromotion declaration you describe -- three generic versions to resolve the complex-complex, complex-real, and real-complex variants using the specific real-real ones, and then a list of real-real ones -- about 50 lines of code in all. We don't have any other helper templates with them, and it doesn't (from our usage) look like there are any natural ones that we'd use.

(FWIW,如果你不想自己写这个,请从 http://www.codesourcery.com/vsiplplusplus/2.2/download.html,并拉出src/vsip/core/promote.hpp.这甚至在我们的部分BSD 许可的库,尽管它实际上并没有在文件本身中这么说.)

(FWIW, if you don't want to write this yourself, download our source from http://www.codesourcery.com/vsiplplusplus/2.2/download.html, and pull out src/vsip/core/promote.hpp. That's even in the part of our library that's BSD-licensed, though it doesn't actually say so in the file itself.)

这篇关于C++ 算术提升标头的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:参数包必须位于参数列表的末尾...何时以及为什 下一篇:具有附加可选模板参数的标准库容器?

相关文章

最新文章