在C ++中进行N阶编译时间的3种不同/相同方式 [英] 3 different / same ways of doing N-factorial compile time in C++

查看:123
本文介绍了在C ++中进行N阶编译时间的3种不同/相同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模板元编程,constexpr和constexpr(如果使用constexpr),并想出了3种不同的方法来进行N递归/N因子运算.

I am trying to play with template metaprogramming, constexpr and if constexpr and have come up with 3 different ways of doing a N-recursive / N-factorial operation.

这三个示例都是我在SO上或通过网上搜索找到的一些示例,然后对其进行了修改,因此它们也是如此

第一个示例使用模板元编程:示例1

The first example is using template metaprogramming: example 1

template<int N>
struct NGenerator
{
    static const int result = N + NGenerator<N-1>::result;
};

template<>
struct NGenerator<0>
{
    static const int result = 1; 
};

static int example1 = NGenerator<5>::result;

第二个仍在使用模板,但是我在以下位置抛出了constexpr:示例2

The second one is still using a template, but I've thrown a constexpr in: example 2

template<int N>
constexpr int example2() 
{
return N + example2<N - 1>();
}


template<>
constexpr int example2<0>() 
{ 
    return 1;
}

static int ex2 = example2<5>();

第三个,是我删除模板并仅使用constexpr的地方:示例3

The third one, is where I've removed the template and "only" use constexpr: example 3

constexpr int generator(int n)
{
    return (n <= 1) ? 1 : n + generator(n - 1);
}

static int ex3 = generator(5);

在我看来,当输入数字是编译时间常数时,所有这三个函数都相同.这三个都是递归的,所有三个都在编译时工作.

In my opinion all three do the same - when the input number is a compile time constant. All three are recursive, all three work compile-time.

我的问题是-这三个之间有什么区别?哪个是最可取的?

My question is - what is the difference between the three? Which one is most preferable?

最后-我想实现"if constexpr" ,但是还不能实现,所以我的解决方法是在示例3中执行"if-statement"-并不是一个真正的if语句,但是如果-在任何方面都是相同的,我不确定是否可以最接近编译时.

And lastly - I would like to implement the "if constexpr" but haven't been able to, so my workaround has been to do the "if-statement" in example 3 - which isn't really a true if-statement, but the closest I could get to a compile-time if - if it is in any way the same, which I am not sure of.

推荐答案

使用类似这样的玩具示例,不会有太大的区别.对于更复杂的示例,递归模板实例化实际上具有与所有模板实例化名称(包括自变量)的长度之和成比例的内存成本.这真的很容易炸毁.

With toy examples like this there won't be much of a difference. With more complex examples, recursove template instantiation practically has memory costs proportional to the sum of the lengths of all of the templates instantiated names, including arguments. This is really easy to blow up.

根据我的经验,constexpr函数倾向于更快地编译.

In my experience constexpr functions tend to compile faster.

所有3个都可能由编译器存储;但是constexpr非模板功能会减少符号(噪声)的产生.

All 3 will be likely memoized by the compiler; but the constexpr non-template function will spew way fewer symbols (noise).

比所有这些更好的是带有循环的constexpr函数.

Better than all of these would be a constexpr function with a loop.

由于您不必坚持编译时间评估,因此编译器可以在运行时自由选择大多数选项.将static int替换为constexpr int.

Compilers are free to do most of the options at runtime as you did not insist on compile time evaluation. Replace static int with constexpr int.

这篇关于在C ++中进行N阶编译时间的3种不同/相同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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