简体   繁体   English

C ++在循环中实例化模板

[英]C++ instantiate templates in loop

I have have a factory class, which needs to instantiate several templates with consecutive template parameters which are simple integers. 我有一个工厂类,需要使用连续的模板参数来实例化几个模板,这些参数是简单的整数。 How can I instantiate such template functions without unrolling the entire loop? 如何在不展开整个循环的情况下实例化这样的模板函数?

The only thing that can think of is using boost pre-processor. 唯一可以想到的是使用升压预处理器。 Can you recommend something else, which does not depend on the preprocessor? 你能推荐一些不依赖于预处理器的东西吗?

thanks 谢谢

Template parameters have to be compile-time constant. 模板参数必须是编译时常量。 Currently no compiler considers a loop counter variable to be a constant, even after it is unrolled. 目前,即使在展开后,编译器也不会将循环计数器变量视为常量。 This is probably because the constness has to be known during template instantation, which happens far before loop unrolling. 这可能是因为必须在模板即时期间知道常量,这在循环展开之前发生。

But it is possible to construct a "recursive" template and with a specialization as end condition. 但是可以构造一个“递归”模板并将特化作为结束条件。 But even then the loop boundary needs to be compile time constant. 但即使这样,循环边界也需要编译时间常数。

template<int i>
class loop {
    loop<i-1> x;
}

template<>
class loop<1> {
}

loop<10> l;

will create ten template classes from loop<10> to loop<1>. 将从循环<10>到循环<1>创建十个模板类。

It probably could be done using the Boost metaprogramming library . 它可能可以使用Boost元编程库完成 But if you haven't used it before or are used to do excessive template programming it probably won't be worth the amount of work it would need to learn how to do it. 但是如果您之前没有使用它或者习惯于进行过多的模板编程,那么学习如何操作它可能不值得花费大量的工作。

thank you guys. 感谢你们。

dr Hirsch is the closest to what is needed, but in the end the predecessor solution is the cleanest. Hirsch博士最接近所需,但最终解决方案是最干净的。 Let me restate the problem: several templates needed to be instantiated at compile time using constant parameters 让我重申一下这个问题:需要使用常量参数在编译时实例化几个模板

f0 = f<0,0>();
f1 = f<0,1>();
...
fk = f<m,n>();

for any significant number of m, and n unrolling template creation makes code busy. 对于任何大量的m,并且n展开模板创建使代码繁忙。 With boost preprocessor i have done the following: 使用boost预处理器,我已完成以下操作:


#include "boost/preprocessor/seq/for_each_product.hpp"
#include "boost/preprocessor/seq/to_tuple.hpp"


#define SEQ_MASK        (0x1)(0x3)
#define SEQ_M           (1)(2)
#define SEQ_N           (1)(2)

#define INSTANCE(r, tparams) {                                      \
            Kernel kernel = Instance<BOOST_PP_SEQ_ENUM(tparams)>(); \
            kernels[kernel.key()]  = kernel; }

        BOOST_PP_SEQ_FOR_EACH_PRODUCT(INSTANCE, (SEQ_MASK)(SEQ_M)(SEQ_N));

after preprocessor runs, it produces all combinations of three sequences defined. 在预处理器运行之后,它会生成定义的三个序列的所有组合。

I do not think it's possible in run-time, because MyClass<1> and MyClass<2> are absolute different classes. 我不认为它在运行时是可能的,因为MyClass<1>MyClass<2>是绝对不同的类。

But I have one idea: if you know all possible values than you can write a switch in your factory class and pass this integer as a parameter into the factory. 但我有一个想法:如果你知道所有可能的值,你可以在工厂类中编写一个switch ,并将这个整数作为参数传递给工厂。

模板在编译时实例化,而不是在运行时实例化,因此您根本无法在循环中实例化它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM