Domanda

Take the following code snippet:

struct whatever {};

template < template <typename, typename> class FOX, typename bat>
struct wulf {};

template <typename A, typename B, typename C = whatever>
struct box;

template <typename A, typename B>
struct box<A, B, whatever> {};

template <typename A, typename B, typename C>
struct box : wulf<box, C> {};

int main(void)
{
return 0;
}

It compiles fine under gcc 4.1.2, but yields the following error when compiling under gcc 4.7.2:

main.cpp:14:25 error: type/value mismatch at argument 1 in template parameter list for 'template<template<class,class> class FOX, class bat> struct wulf'
main.cpp:14:25 error: expected a template of type 'template<class, class> FOX', got 'template<class A, class B, class C> struct box'

This is the smallest example code snippet with which I seem to be able to reproduce this error and I don't know what is happening. Why is the code being rejected and is there a correct way of doing this which will compile under both?

È stato utile?

Soluzione

Your wulf class template accepts, as its first template template-argument, a class template that takes two type parameters.

Here, you are trying to provide as a corresponding argument a class template (box) that takes three type parameters:

template <typename A, typename B, typename C>
struct box : wulf<box, C> {};
//                ^^^

This is illegal. It doesn't matter if a default type argument is specified for the third type parameter of the box class template: the type and number of template-arguments must match exactly.

To fix the problem, change the definition of your wulf class template as follows:

template < template <typename, typename, typename> class FOX, typename bat>
//                                       ^^^^^^^^
struct wulf {};

Here is a live example that shows your code compiling with the above fix.

Altri suggerimenti

Using C++11, you can solve the problem by changing wulf to:

template < template <typename...> class FOX, typename bat>
struct wulf {};

although GCC 4.1 won't accept it I'm afraid...


One more trick I sometimes use: Add a wrapper class for box:

struct whatever {};

template < template <typename,typename> class FOX, typename bat>
struct wulf {};

template <typename A, typename B, typename C = whatever>
struct box;

template <typename A, typename B>
struct box<A, B, whatever> {};

template <typename A, typename B>
struct box2 : box<A, B> {};

template <typename A, typename B, typename C>
struct box : wulf<box2, C> {};

int main(void)
{
    return 0;
}

I don't know if this (loosing the third argument for box within wulf) is a problem in your case, but I had some use-cases in the past where this technique helped.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top