How would you do something like this?

 
template <class A, class B> void funk(A<B> C);


where A is a user defined templated class of type B.

I get the error 'A' is not a template when trying to compile.

Edit: @firedraco: Cool, 3333, not as cool as 7777 though. ;=)
Last edited on
Try:
template <class A, class B> void func(A<typename B> C);

Heh, 3333 posts.
Last edited on
using (A<typename B> C)

error: 'A' is not a template
error: variable of field 'funk' declared void
error: expected primary expression after 'C';
My first instinct would be to throw the template keyword in there:

1
2
3
4
5
6
7
8
// either here:
template <template class A, class B> void funk(A<B> C);

// or here:
template <class A, class B> void funk(template A<B> C);

// or both:
template <template class A, class B> void funk(template A<B> C);


Can't compile to test though because I'm at work, so these are all shots in the dark. =(
Interesting one....

Can you have A<B> ??

In a template class - the whole class type is ClassName< template param>
where ClassName is an identifier not a type - :
Example
vector<int> //the whole type is vector<int> - the vector word does not name a seperate type.


So what he is trying to do is something like vector<int><string> - which would make no sense.

????????????????
Last edited on
Well whatever I'm doing I'm probably doing it wrong I know that.

What I was trying to do was rework on my resize function in my matrix class so it can resize both matrices of type <int> and of type <dlist> with a single function call. So I been reading up on template specialization. But am fairly new and still don't understand it all and can't get any of the code i've written so far to compile without taking the resize function out of the class and declaring it globally. that part i've been able to accomplish, or at least the prototypes anyway.



dlist (and zlist as well) both have a function with the same name: resize.

oh heck I'm having too much difficulty explaining it.


here is my matrix.cpp relevant code:
1
2
3
4
5
6
7
template <class itemType>
void matrix<itemType>::resize(int newRows, int newCols)
{
    myRows = newRows;
    myCols = newCols;
    return;
}


But for a NxN sudoku, I need to also maintain a matrix of possibile candidates for each space in the main grid, hence my dlist class, which is a list of N bits.

So ideally what I would like to do is override the resize function in the event that the calling object is of type dlist or of type zlist to something like:
1
2
3
4
5
6
7
8
9
10
template <class itemType>
void matrix<itemType>::resize(int newRows, int newCols)
{
    myRows = newRows;
    myCols = newCols;
    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myCols; ++j)
            myMatrix(i, j).resize(myRows);
    return;
}


But only execute this function for matrices of types that have a resize function defined, because it wouln't make any sense to resize an int.
Last edited on
To make A a template class, try a template template parameter:
1
2
3
template< template< class AA > class A,
          class B >
void f( const A< B > & ab ) {}

Last edited on
maybe I could write a wrapper in my class that calls a globally declared overridden function?


P.S. Am I using this terminology correctly?


such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void funk(dlist & a, int newRows, int newCols)
{ //yadayadayada }
void funk(zlist & z, int newRows, int newCols)
{ //etc etc etc }
//and then have another  "default" function 

void funk(/*don't know what, if anything goes here yet*/int newRows, int newCols){}


template <class itemType>
void matrix<itemType>::resize(int newRows, int newCols)
{
    if (itemType == dlist)
        return resize(*this(0,0), newRows, newCols);
    if (itemType == zlist)
        return resize(*this(0,0), newRows, newCols); 
    else 
    myRows = newRows;
    myCols = newCols;
    return;
}
Last edited on
Perhaps you could write a wrapper class and then use partial specialization. Here's an example that you may be able to modify to get started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <list>

//
// template class wrapper
//
// accepts a template template parameter
// and a "contained" template parameter
//
template< template< typename AA > class A,
          typename B >
struct Wrapper
{
    void operator()() const {
        std::cout << "normal" << std::endl;
    }
};
//
// partial specialization for Wrappers that
// "contain" int
//
template< template< typename AA > class A >
struct Wrapper< A, int >
{
    void operator()() const {
        std::cout << "special" << std::endl;
    }
};

// example class template for custom container
template< typename T > struct MyList { T t; };


int main()
{
    Wrapper< std::list, double > f1;
    f1(); // outputs normal

    Wrapper< MyList, int > f2;
    f2(); // outputs special

    return 0;
}


Hope this helps.
Last edited on
ok I think I got it:
would something like this work?

in matrix.h but before declaration of class matrix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <class scheisse> inline void resize(scheisse ficken, matrix & m, int newRows, int newCols)
{
    m.myRows = newRows; m.myCols = newCols;
    return;
}

template <> inline void resize(const dlist ficken, matrix & m, int newRows, int newCols)
{
    m.myRows = newRows; m.myCols = newCols;
    for (int i = 0; i < ficken.length)
        ficken.resize(newRows);
    return;
}


//and the matrix member resize function:
template <class itemType>
void matrix<itemType>::resize(int newRows, int newCols)
{
    resize(myMatrix[0][0], *this, newRows, newCols);
    return;
}

the myMatrix[0][0] is just to pass an instance of the itemType so the compiler can pick which of the overridden functions to execute. *this is passed so the non-member functions will have access to myRows and myCols

It sounds good on paper... but will it work....
(Don't ask me... if it fails to compile on my end it probably just means I did something ELSE ENTIRELY DIFFERENT wrong...)

edit: The problem with this approach is that matrix has not been defined for usage as a parameter. Would I be able to use the extern keyword?

@Moorecm could you explain to me what 'template< template< class AA > class A,
class B >'


Last edited on
Ok I think I got it finally.... @Moorecm I am still gonna look at your code though later, because I am still intrigued how do you get the itemtype of a templated class instance....


part of my matrix.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class scheisse, class shit> inline void RESIZE(scheisse ficken, shit & m, int newRows, int newCols)
{
    m.myRows = newRows; m.myCols = newCols;
    return;
}

template <class shit> inline void RESIZE(dlist ficken, shit & m, int newRows, int newCols)
{
    m.myRows = newRows; m.myCols = newCols;
    for (int i = 0; i < newRows; ++i)
        for (int j = 0; j < newCols; ++j)
            m(i, j).resize(newRows);
    return;
}

class matrix { //yada yada yada};
#include matrix.cpp


and part of my matrix.cpp file:
1
2
3
4
5
6
template <class itemType>
void matrix<itemType>::resize(int newRows, int newCols)
{

    return RESIZE(myMatrix[0][0], *this, newRows, newCols);
}


Does anybody know if there's anything significantly wrong to this approach? And isn't there a way to define a class template for any number of different types?
Since I have 2 list classes, which are practically identical to each other except one is 0 inclusive, the other isn't, can't I write a template specialization just once for both lists? It would be a pain to have write code for say if I had 10 different list classes...
Last edited on
Thats a very rude matrix
Topic archived. No new replies allowed.