How can we pass a template in a template function

I

Ioannis Vranos

Is it possible to pass some template (e.g. vector) as an argument in a function and
instantiate inside the function like this?


T<int> con(1);




--
Ioannis Vranos

http://www23.brinkster.com/noicys

[I am using 90 characters word-wrapping - (800/640) *72= 90 or better described as:
(800/640) *80 - 10 for quotation= 90. If someone finds it inconvenient, please let me know].
 
M

Malte Starostik

Ioannis said:
Is it possible to pass some template (e.g. vector) as an argument in a
function and instantiate inside the function like this?


T<int> con(1);

Sure, with template template parameters:

#include <iostream>
#include <vector>

template< template< typename > class Container, typename T >
Container< T > make_container( const T& value )
{
return Container< T >( 1, value );
}

int main()
{
std::cout << make_container< std::vector >( 42 )[ 0 ] << std::endl;
}

HTH,
Malte
 
D

Donovan Rebbechi

Is it possible to pass some template (e.g. vector) as an argument in a
function and instantiate inside the function like this?

You can do this with a class (or a function object)

template<template<class T> class T1> class B
{
public:

static void foo() {
T1<int> a;
T1<float> b;
}
};


Cheers,
 
D

Donovan Rebbechi

You can do this with a class (or a function object)

template<template<class T> class T1> class B
{
public:

static void foo() {
T1<int> a;
T1<float> b;
}
};

Doh! This works with function templates too, no need to go via a class.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top