6

I'm using gcc/4.7 and I need to instantiate a class with a template-template argument in a template function (or member function). I receive the following error

test.cpp: In function 'void setup(Pattern_Type&)':
test.cpp:17:34: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class C> struct A'
test.cpp:17:34: error:   expected a class template, got 'typename Pattern_Type::traits'
test.cpp:17:37: error: invalid type in declaration before ';' token
test.cpp:18:5: error: request for member 'b' in 'a', which is of non-class type 'int'

By commenting the two lines marked in the snippet the code runs, so A a can be instantiated in 'main' but not in 'setup'. I think this would be of interest also for others, and I would be really happy to understand the reason why the code does not work. Here's the code

struct PT {
  template <typename T>
  struct traits {
    int c;
  };
};

template <template <typename> class C>
struct A {
  typedef C<int> type;
  type b;
};

template <typename Pattern_Type>
void setup(Pattern_Type &halo_exchange) {
  A<typename Pattern_Type::traits> a; // Line 17: Comment this
  a.b.c=10; // Comment this
}

int main() {
  A<PT::traits> a;

  a.b.c=10;

  return 0;
}

Thanks for any suggestion and fix! Mauro

1 Answers1

10

You need to mark Pattern_Type::traits as a template:

A<Pattern_Type::template traits> a;

This is needed because it is dependent on the template parameter Pattern_Type.

You also shouldn't use typename there because traits is a template, not a type.

interjay
  • 107,303
  • 21
  • 270
  • 254
  • That is a combination I did not try: template without typename. Still not clear to me why in 'main' I do not need to specify 'template'. Thanks for the super quick answer! – user1919074 Dec 20 '12 at 15:00
  • 1
    @user1919074: It's because in `main` it's not dependent on a template parameter (i.e. the contents of `PT` are already known, but the contents of `Pattern_Type` are not known because it could be anything). For more information, see [Where and why do I have to put the “template” and “typename” keywords?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – interjay Dec 20 '12 at 15:01