473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How can we pass a template in a template function

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
3 1195
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Huskier | last post by:
Hi all: I want to pass a class-member function to pthread_create, and my code is in the following, but I don't know how to pass myobj.thread_function to pthread_create function. #include...
6
by: david | last post by:
For example, I can call <xsl:value-of select="concat('name','(',value,')')"/>, is there anyway I can pass the expression concat('name','(',value,')') as a variable from outside the template? ...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
8
by: Leo jay | last post by:
dear all, i want to write log on disk file when macro NEED_DO_LOG is defined. otherwise, do nothing. so i wrote code like this: #ifdef NEED_DO_LOG fstream fsLogFile (...); #else CDummyStream...
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
0
by: dmcomer | last post by:
In the context of parsing and preserving HTML tags in an XSL stylesheet, I understand the difference between <xsl:value-of select="body"/> and <xsl:copy-of select="body> However, I am...
2
by: tomix | last post by:
Hi, I am trying to create a template which fill window list control with items. I would like to ask can i 'pass' to the template the actual function which need to be called on the item in...
7
by: Jess | last post by:
Hello, I learned that when I work with templates in C++, I should have functions that pass arguments by reference because the type of object is not known. Does it mean that if I have a function...
10
by: Bernhard Reinhardt | last post by:
Hi, I read data from a file into a 4 dimensional array. The data has a size of 5MB (could later be up to 500MB). I want to pass the matrix to subroutines. What is the cleverst (and for a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.