模板模板参数和可变参数模板与gcc 4.4 [英] Template template parameters and variadic templates with gcc 4.4

查看:131
本文介绍了模板模板参数和可变参数模板与gcc 4.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Debian上使用gcc 4.4。请考虑以下代码。

  #include< map& 
#include< string>
using std :: map;
using std :: string;

// Args允许用户指定额外的显式模板参数
template< typename T,
template< typename T,typename ... Args> class C,
typename ... Args>
C< T,Args ...> foo()
{
C< T,Args ...> X;
return x;
}

int main(void)
{
map< string,int& a = foo< string,map,int>();
}

所以,这里的想法是 code>匹配匹配 string , C ,模板参数包 Args int 匹配。我可能有一些语法错误,请更正如果是这样。特别是,如果想要 class C 中的第一个模板参数匹配 T ,其余的匹配模板参数pack Args template< typename T,typename ... Args>

 

在函数'int main()':
post.cc:18:error:没有匹配的函数调用'foo()'

这看起来类似于可变模板模板和完美转发。这个问题表明这是一个gcc bug,但也许我错误地认为这些问题都是一样的。



请温柔。我对可变模板的了解不到12小时,我只是试图重写一些旧的C ++代码,以减少重复。它也有一段时间,因为我做了任何C ++。如果有解决方法,请让我知道。感谢。



编辑: Ise Wisteria 为我工作的可变模板模板和完美转发建议这是同样的错误。当然,我现在是
(a)想知道这个解决方法是多么脆弱,(b)为什么它的工作,以及什么动机Ise想到它。
虽然我猜只有Ise可以回答最后一位。 : - )

解决方案

正如在编辑中所讨论的,我的问题似乎与链接问题=http://stackoverflow.com/questions/6486432/variadic-template-templates-and-perfect-forwarding>可变模板模板和完美转发。特别是,在链接中给出的解决方法也适用于我的情况。修改后的代码如下:

  #include< map> 
#include< string>
using std :: map;
using std :: string;

template< typename T,
template< typename T,typename ... Args> class C,
typename ... Args>
struct X
{
typedef C< T,Args ...>类型;
};

template< typename T,
template< typename T,typename ... Args> class C,
typename ... Args>
typename X< T,C,Args ...> :: type foo()
{
C< T,Args ...> X;
return x;
}

int main(void)
{
map< string,int> a = foo< string,map,int>();
}


I'm using gcc 4.4 on Debian squeeze. Consider the following code.

#include <map>
#include <string>
using std::map;
using std::string;

// Args lets the user specify additional explicit template arguments
template <typename T,
      template <typename T, typename... Args> class C,
      typename... Args>
C<T, Args...> foo()
{
  C<T, Args...> x;
  return x;
}

int main(void)
{
  map<string, int> a = foo<string, map, int>();
}

So, the idea here is that T matches string, C matches map, and the template parameter pack Args matches int. I may have some of the syntax wrong, please correct if so. In particular, if one wants the first template argument in class C to match T and the rest to match the template parameter pack Args, is template <typename T, typename... Args> class C the correct syntax?

This gives the error

In function 'int main()':
post.cc:18: error: no matching function for call to 'foo()'

This appears to be similar to the question Variadic template templates and perfect forwarding. That question suggests that this is a gcc bug, but maybe I am mistaken in thinking these questions are about the same thing.

Please be gentle. My knowledge of variadic templates is less than 12 hours old; I was just trying to rewrite some old C++ code to reduce duplication. It has also been a while since I did any C++. If there is a workaround, please let me know. Thanks.

EDIT: The workaround suggested in the comments of Variadic template templates and perfect forwarding by Ise Wisteria worked for me, which suggests that this is the same bug. Of course, I'm am now (a) wondering how fragile this workaround is and (b) why it works, and what motivated Ise to think of it. Though I guess only Ise can answer the last bit. :-)

解决方案

As discussed in the edits, my question appears to tickle the same bug as the linked question, Variadic template templates and perfect forwarding. In particular, the workaround given there in a link also works in my case. The modified code that works is as follows:

#include <map>
#include <string>
using std::map;
using std::string;

template <typename T,
      template <typename T, typename... Args> class C,
      typename... Args>
struct X
{
  typedef C<T, Args...> type;
};

template <typename T,
      template <typename T, typename... Args> class C,
      typename... Args>
typename X<T, C, Args...>::type foo()
{
  C<T, Args...> x;
  return x;
}

int main(void)
{
  map<string, int> a = foo<string, map, int>();
}

这篇关于模板模板参数和可变参数模板与gcc 4.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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