Template function arguments

M

mlt

I have made a class C that contains the following fields:

typedef math::vector_type vector_type;
//ublas vector type
typedef std::vector<std::vector< vector_type > > container;

In a function I would like an argument to be the type of the elements that
the container contains:

template<C>
void fun(typename C::container::value_type::value_type & p) {

// do things with p
}


But when I compile I get the error:

error C2770: invalid explicit template argument(s)


It works fine if I just specify:

C::value_type::value_type p(3);

in the program, but I get the error when I pass p to the function 'fun':

fun<C>(p);


Any hints appreciated!
 
M

mzdude

I have made a class C that contains the following fields:

typedef math::vector_type                                   vector_type;
//ublas vector type
typedef std::vector<std::vector< vector_type > > container;

In a function I would like an argument to be the type of the elements that
the container contains:

template<C>
void fun(typename C::container::value_type::value_type & p) {

// do things with p

}

But when I compile I get the error:

error C2770: invalid explicit template argument(s)

It works fine if I just specify:

C::value_type::value_type p(3);

in the program, but I get the error when I pass p to the function 'fun':

    fun<C>(p);

Any hints appreciated!

why so complex? why not just

template<typename C>
void fun(C &p)
{
// do stuff with p
}


fun<vector_type>( p );
 
I

Ian Collins

mlt said:
I have made a class C that contains the following fields:

typedef math::vector_type vector_type;
//ublas vector type
typedef std::vector<std::vector< vector_type > > container;

In a function I would like an argument to be the type of the elements
that the container contains:

template<C>

What's that for? Are you specialising a function template?
 
M

mlt

Ian Collins said:
What's that for? Are you specialising a function template?



Yes all function are written as "free" template functions. For some reason
calling fun like:

C::container::value_type::value_type p(3);
fun<C>(p);


where fun() is:

template<C>
void fun(typename C::container::value_type::value_type & p) {
// do things with p
}

gives the described error, even though:

C::container::value_type::value_type p(3);

actually is a ublas vector type!


But maybe its a visual studio issue, can't seem to replicate the error using
cygwin
 
N

Noah Roberts

mlt said:
I have made a class C that contains the following fields:

typedef math::vector_type vector_type;
//ublas vector type
typedef std::vector<std::vector< vector_type > > container;

In a function I would like an argument to be the type of the elements
that the container contains:

template<C>
void fun(typename C::container::value_type::value_type & p) {

// do things with p
}

The problem here (besides the typo in the template declaration) is that
your actual error is not simply "invalid explicit template argument" but:

invalid explicit template argument(s) for 'void f(C::value_type::{ctor} &)'

That should clue you into what is happening here. For the same reason
you need typename on the first value_type (so the compiler knows it's a
type name and not a variable or function) you also need it on the second
value_type. Unfortunately I can't think of any syntactically valid way
for you to do that except for using metafunctions:

template < typename C > struct value_type { typedef typename
C::value_type type; };

template < typename C >
void f(typename value_type< typename value_type<C>::type >::type & p)
{
}

I attempted various alternative versions using () to try and specify
which type the "typename" applied to, and such, but was unsuccessful.
There may be a legal way, I just can't think of one.

Here's the validation program:

#include <iostream>
#include <vector>

typedef std::vector<int> vector_type;
typedef std::vector<std::vector< vector_type> > container;

template < typename C > struct value_type { typedef typename
C::value_type type; };

template < typename C >
void f(typename value_type< typename value_type<C>::type>::type & )
{
}

int main()
{
container::value_type::value_type p(3);

f<container>(p);

std::cout << "DONE\n";
std::cin.get();
}
 

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,780
Messages
2,569,611
Members
45,267
Latest member
WaylonCogb

Latest Threads

Top