Variadic模板模板和完美的转发 [英] Variadic template templates and perfect forwarding

查看:144
本文介绍了Variadic模板模板和完美的转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题 对象生成器模式让我想到了自动化它的方法。

This question on the object generator pattern got me thinking about ways to automate it.

基本上,我想自动创建 std :: make_pair std :: bind1st std :: mem_fun ,以便不必为每个模板类类型编写不同的函数,您可以编写单个可变参数模板模板函数来处理所有情况立刻。这个函数的用法如下:

Essentially, I want to automate the creation of functions like std::make_pair, std::bind1st and std::mem_fun so that instead of having to write a different function for each template class type, you could write a single variadic template template function that handles all cases at once. Usage of this function would be like:

make<std::pair>(1, 2);         // equivalent to std::make_pair(1, 2)
make<std::binder2nd>(&foo, 3); // equivalent to std::bind2nd(&foo, 3);

可以写这个函数 make ?我试过这个,但它不工作在GCC 4.5或4.6:

Is it possible to write this function make? I have tried this, but it doesn't work in GCC 4.5 or 4.6:

template <template <typename...> class TemplateClass, typename... Args>
TemplateClass<Args...> make(Args&&... args)
{
    return TemplateClass<Args...>(std::forward<Args>(args)...);
}



如果我尝试调用(例如) make< std :: pair>(1,2)我只是得到

error: no matching function for call to 'make(int, int)'

>
或者这是正确的,GCC是错误的?

或者这在C ++ 0x中根本不可能?

Have I got the syntax wrong anywhere here?
Or is this right and GCC is wrong?
Or is this just fundamentally impossible in C++0x?

]

提案 N2555 似乎暗示这是允许的,并且 GCC声称已在GCC4.4中实施它

Proposal N2555 seems to suggest that this is allowed and GCC claims to have implemented it in GCC4.4.

推荐答案

这是完全正确的。我希望它工作。所以我认为GCC是错误的拒绝。 FWIW:

That's exactly right. I would expect it to work. So I think that GCC is in error with rejecting that. FWIW:

#include <utility>

template <template <typename...> class TemplateClass, typename... Args>
TemplateClass<Args...> make(Args&&... args)
{
    return TemplateClass<Args...>(std::forward<Args>(args)...);
}

int main() {
  make<std::pair>(1, 2);
}


// [js@HOST2 cpp]$ clang++ -std=c++0x main1.cpp
// [js@HOST2 cpp]$

这篇关于Variadic模板模板和完美的转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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