C ++自动检测模板参数? [英] C++ auto detection of template arguments?
问题描述
我试图定义一个递归构造像任务农业。这里,我试图两个操作数递归地可以工作任何数量的操作数,因为它可以嵌套自己。
template<类型名T1,类型名T2>
class Farm
{
private:
T1 * task1;
T2 * task2;
public:
//保存它们,以便在调用操作符
时可以使用它们farm(T1 * _t1,T2 * _t2):task1(_t1),task2(_t2){}
void operator()()
{
//调用调用操作符,意味着一个场可以是一个任务(任意嵌套)
(* task1) ;
(* task2)();
}
};
int main()
{
...创建两个指针(A * a,B * b ...)
Farm(a,b); //错误:缺少模板参数''('token
Farm< A,B>(a,b); //在这个作品中,它工作
}
问题是模板参数的自动检测在这种情况下不工作。我通过gcc编译器实现了这个模板参数隐式检测。
谢谢!
类/构造函数不会像函数那样自动检测类型,您需要编写一个包装函数来创建您的类。
这样做的方法如下, 对象生成器模式(感谢@Itjax!)
template< typename T1,typename T2>
Farm< T1,T2> makeFarm(T1 * a,T2 * b){
return Farm< T1,T2>(a,b);
}
//愚蠢的例子
Farm< T1,T2> farm = makeFarm(a,b);
//更好的例子
template< typename T>
void plow(T& farm){farm.applyTractor(...); }
void blah(){
plow(makeFarm(b,a))
}
当使用lambda / bind / foreach和类似的部分,当你想创建一个带有一些参数的模板类的临时对象,并避免指定它们的类型时,这种模式出现了很多发送到另一个模板函数(
std :: for_each
)或多态对象(std :: function
)。 p>
注意:生成器函数通常内联并且使用copy-elision优化,可能不会在代码中调用任何复制构造函数。如果你不能复制你的对象,makeFarm()应该返回一个智能指针(
std :: unique_ptr
是现代C ++的首选)。I am trying to define a recursive construct like a task farming. Here, I am trying for two operands which recursively can work for any number of operands as it can nest itself.
template <typename T1, typename T2> class Farm { private: T1 *task1; T2 *task2; public: // save them so that I can use them when invoking call operator Farm(T1 *_t1, T2 *_t2): task1(_t1), task2(_t2) { } void operator()() { // invoke call operator, meaning a farm could be a task (arbitrary nesting) (*task1)(); (*task2)(); } }; int main() { ... create two pointer(A *a, B *b...) Farm(a,b); // error: missing template arguments before ‘(’ token Farm<A, B>(a,b); // in this works, it works }
The problem is with auto-detection of template arguments which is not working in this case. What am I doing wrong and how could I achieve this template parameters implicit detection by gcc compiler.
Thanks!
解决方案Classes/constructors don't autodetect types like functions do. You need to write a wrapper function to create your class.
This is done as follows and called the Object Generator pattern. (thanks @Itjax!)
template <typename T1, typename T2> Farm<T1, T2> makeFarm(T1* a, T2* b) { return Farm<T1,T2>(a,b); } // silly example Farm<T1,T2> farm = makeFarm(a,b); // better example template<typename T> void plow(T& farm) { farm.applyTractor(...); } void blah() { plow(makeFarm(b,a)) }
This pattern emerges quite a lot when using lambda/bind/foreach and similar parts, when you want to create a temporary object of a templated class with some arguments and avoid specifying their type, usually sending it into another template function (
std::for_each
) or polymorphic object (std::function
).Note: The generator function usually inlines and with copy-elision optimization there will probably be no copy-constructor(s) called in your code at all. If you cannot copy your object, makeFarm() should return a smart pointer instead (
std::unique_ptr
is preferred in modern C++).这篇关于C ++自动检测模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!