By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

How can we pass a template in a template function

P: n/a
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].
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ioannis Vranos schrieb:
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
Jul 23 '05 #2

P: n/a
On 2005-03-28, Ioannis Vranos <iv*@remove.this.grad.com> wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3

P: n/a
On 2005-03-28, Donovan Rebbechi <ab***@aol.com> wrote:
On 2005-03-28, Ioannis Vranos <iv*@remove.this.grad.com> wrote:
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;
}
};


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

--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.