Template template parameters and variadic templates with gcc 4.4

前端 未结 2 1655
情深已故
情深已故 2021-01-11 12:47

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

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

// Args l         


        
相关标签:
2条回答
  • 2021-01-11 13:48

    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>();
    }
    
    0 讨论(0)
  • 2021-01-11 13:53

    I don't think variadic template parameters can match non-variadic arguments in g++4.4, so you need to overload your foo function with a non-variadic version.

    Also keep in mind that map actually has more than two template parameters, and therefor wont match the new foo-function either.

    This addition to your example should clarify it:

    #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;
    }
    
    template<typename T, template<typename, typename> class C, typename Arg>
    C<T, Arg> foo() {
      return C<T, Arg>();
    }
    
    template<typename T, typename... Args> class A {};
    
    template<typename T, typename Arg> class B {};
    
    int main(void) {
      map<string, int> a = foo<string, map, int>(); // fails.
      A<string, int> x = foo<string, A, int>();
      B<string, int> y = foo<string, B, int>();
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题