I have a problem using template and typedef, someone can tell me what is wrong !

M

marco

the problem:

I use a typedef inside a class template, than I use this type
(dim_v<N1>::Type) to define the argument of a template function f
but when I call this function from main, the compiler (gcc 3.4.1)
tell me: "no matching function found".
If someone, more expert than me, could tell me what is wrong
I would be very happy



the code:

===============================================================
file: scratch.h
---------------------------------------------------------------


#include <iostream>

typedef int dim_t;
typedef unsigned int rank_t;

template<typename T, rank_t N>
class V
{
private :
T dm1;
rank_t dm2;

public :
V(T p1, rank_t p2) :
dm1(p1), dm2(p2+N)
{
}

rank_t get_2()
{
return dm2;
}
};

template<rank_t N1>
class dim_v
{
public :
typedef V<dim_t, N1> Type;
};

template<rank_t P>
void f(typename dim_v<P>::Type v)
{
std::cout << std::endl << v.get_2() << std::endl;
}

===============================================================

===============================================================
file: main.cpp
---------------------------------------------------------------

#include "scratch.h"


int main( int argc, char * argv[] )
{
dim_t type = 5;
rank_t val = 10;
const rank_t r = 3;
dim_v<r>::Type v(type, val); // OK
f(v); // ERROR !

return 0;
}

===============================================================


# compile command :
g++ -c -o /mnt/TEMP/cpp_projects/SymbolArray/linux/Debug_Build/main.o -g2
-O0 -MD -I/usr/include -I/usr/local/include -I/usr/include/c++/3.4.1
main.cpp

# compile output :
main.cpp: In function `int main(int, char**)':
"main.cpp": main.cpp error: no matching function for call to `f(V<dim_t,
3u>&)' at line 111
GNU C++ Compiler exited with error code: 1

# compiler version :
gcc 3.4.1 (I get the same error also with version 3.3.2)

# machine :
processor: amd athlonxp 1700+
memory: 512MB
os: linux mandrake 10.0
 
D

David Hilsee

marco said:
the problem:

I use a typedef inside a class template, than I use this type
(dim_v<N1>::Type) to define the argument of a template function f
but when I call this function from main, the compiler (gcc 3.4.1)
tell me: "no matching function found".
If someone, more expert than me, could tell me what is wrong
I would be very happy



the code:

===============================================================
file: scratch.h
---------------------------------------------------------------


#include <iostream>

typedef int dim_t;
typedef unsigned int rank_t;

template<typename T, rank_t N>
class V
{
private :
T dm1;
rank_t dm2;

public :
V(T p1, rank_t p2) :
dm1(p1), dm2(p2+N)
{
}

rank_t get_2()
{
return dm2;
}
};

template<rank_t N1>
class dim_v
{
public :
typedef V<dim_t, N1> Type;
};

template<rank_t P>
void f(typename dim_v<P>::Type v)
{
std::cout << std::endl << v.get_2() << std::endl;
}

I don't think this is allows template argument deduction. The compiler
cannot deduce what "P" should be because it would have to figure things out
in reverse. That is, it would have to figure out what every possible "Type"
is and determine which dim_v<P> holds the V<dim_t, 3> you provided. While
it is obvious which template you mean, when you consider that there could be
a specialization of dim_v<2> that also had a "Type" of V<dim_t,3>, it is not
so obvious. To use argument deduction, try writing it as

template<rank_t P>
void f(V<dim_t,P> v)
{
std::cout << std::endl << v.get_2() << std::endl;
}


===============================================================
file: main.cpp
---------------------------------------------------------------

#include "scratch.h"


int main( int argc, char * argv[] )
{
dim_t type = 5;
rank_t val = 10;
const rank_t r = 3;
dim_v<r>::Type v(type, val); // OK
f(v); // ERROR !

return 0;
}

If you leave the function template as it is, you should write

f<3>(v);
 
M

marco

I don't think this is allows template argument deduction. The compiler
cannot deduce what "P" should be because it would have to figure things
out in reverse. That is, it would have to figure out what every
possible "Type" is and determine which dim_v<P> holds the V<dim_t, 3>
you provided. While it is obvious which template you mean, when you
consider that there could be a specialization of dim_v<2> that also had
a "Type" of V<dim_t,3>, it is not so obvious.

I have understand what is the problem thank you for the explanation.
I am still a beginner with template so I was not sure what was wrong.


To use argument deduction, try writing it as
template<rank_t P>
void f(V<dim_t,P> v)
{
std::cout << std::endl << v.get_2() << std::endl;
}

Oh yes this work, I already know, but my goal was to get abstraction
on the effectively used type, perhaps it is better to wrap
the whole class V<dim_t, T> in an ad hoc class.


If you leave the function template as it is, you should write

f<3>(v);

Thank you again for help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top