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

Template within a template

P: n/a
Hi,

Does C++ allow the programmer to declare a template with in a template
so that a generic function can instantiate the embedded template? For
example, could code such as this exist:

template< class T<int N> >
int func()
{
int the_n = N;
T the_t(N);

return the_n;
}

cout << func< a<2> >(); // Should print 2

Any ideas?

Many thanks,

Stephen Henry

Aug 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Steve wrote:
Does C++ allow the programmer to declare a template with in a template
so that a generic function can instantiate the embedded template? For
example, could code such as this exist:

template< class T<int N> >
int func()
{
int the_n = N;
T the_t(N);

return the_n;
}

cout << func< a<2> >(); // Should print 2
Wait, but a<2> is not a template. It's a concrete class...

Any ideas?


I am not sure what you need it for, but try this:

template<template<int> class T, int N> int func()
{
T<N> the_t(N); // instantiate the template and the class
return N;
}

template<int N> struct a { a(int) {} }; // the template

#include <iostream>

int main()
{
std::cout << func<a,2>() << std::endl;
std::cout << func<a,10>() << std::endl;
}

V
Aug 18 '05 #2

P: n/a
Thanks for the help,

Unfortunately, for legacy reasons, the format I have to use is:

a< b<2> >

For example, it would look something like this:

sc_in< sc_lv<8> > p;

I would like to be able to somehow defer construction of each class
within that statement, so that I can do something like this:

function( p );

template< class T<class N> >
void function( T<N> in )
{
// Store the fact that a type T was passed, and store the fact that
it was of type N
}

In some other function, I would say:

void function2()
{
// in was of type T with argument N, so construct a new T of type N
}

I'm not really seeing how C++ can do this, with my knowledge at least.
But, I was hoping that their was at least some method somewhere of
which I wasn't aware. I was looking at the BOOST meta programming
library, but to be honest I don't really know much about it, and would
really like to know if such a construction is even possible before I
try to implement it. Is it possible?

Thanks again,

Stephen Henry

Victor Bazarov wrote:
Steve wrote:
Does C++ allow the programmer to declare a template with in a template
so that a generic function can instantiate the embedded template? For
example, could code such as this exist:

template< class T<int N> >
int func()
{
int the_n = N;
T the_t(N);

return the_n;
}

cout << func< a<2> >(); // Should print 2


Wait, but a<2> is not a template. It's a concrete class...

Any ideas?


I am not sure what you need it for, but try this:

template<template<int> class T, int N> int func()
{
T<N> the_t(N); // instantiate the template and the class
return N;
}

template<int N> struct a { a(int) {} }; // the template

#include <iostream>

int main()
{
std::cout << func<a,2>() << std::endl;
std::cout << func<a,10>() << std::endl;
}

V


Aug 18 '05 #3

P: n/a
Steve wrote:
Victor Bazarov wrote:
Steve wrote:
Does C++ allow the programmer to declare a template with in a template
so that a generic function can instantiate the embedded template? For
example, could code such as this exist:

template< class T<int N> >
int func()
{
int the_n = N;
T the_t(N);

return the_n;
}

cout << func< a<2> >(); // Should print 2
Wait, but a<2> is not a template. It's a concrete class...

Any ideas?


I am not sure what you need it for, but try this:

template<template<int> class T, int N> int func()
{
T<N> the_t(N); // instantiate the template and the class
return N;
}

template<int N> struct a { a(int) {} }; // the template

#include <iostream>

int main()
{
std::cout << func<a,2>() << std::endl;
std::cout << func<a,10>() << std::endl;
}

V


Don't top-post. Rearranged:
Thanks for the help,

Unfortunately, for legacy reasons, the format I have to use is:

a< b<2> >
That "format" (syntax) may not give you what you want. What is 'b'?
How is it defined?
For example, it would look something like this:

sc_in< sc_lv<8> > p;
Again, what's 'sc_lv'? How is it defined? Do you have control over it?
Or do you have to deal with a bunch of existing classes/templates?

Post more code. Your questions are too generic. Not enough information.
I would like to be able to somehow defer construction of each class
within that statement, so that I can do something like this:

function( p );

template< class T<class N> >
I showed you the available syntax. 'class T<class N>' is incorrect.
void function( T<N> in )
{
// Store the fact that a type T was passed, and store the fact that
it was of type N
It might be possible to interrogate the 'T' _if_ it has appropriate
members that somehow keep 'N' and are willing to expose it.
}

In some other function, I would say:

void function2()
{
// in was of type T with argument N, so construct a new T of type N
What 'in'? This function doesn't have any 'in'. How are you going to
communicate the information about the argument of one function template to
another function? Are those functions members of the same class? Post
more code to explain.
}

I'm not really seeing how C++ can do this, with my knowledge at least.
I believe you.
But, I was hoping that their was at least some method somewhere of
which I wasn't aware. I was looking at the BOOST meta programming
library, but to be honest I don't really know much about it, and would
really like to know if such a construction is even possible before I
try to implement it. Is it possible?


It might be possible, depending on what those types you're going to use
are.

V
Aug 18 '05 #4

P: n/a

Victor Bazarov wrote:
Steve wrote:
Victor Bazarov wrote:
Steve wrote:

Does C++ allow the programmer to declare a template with in a template
so that a generic function can instantiate the embedded template? For
example, could code such as this exist:

template< class T<int N> >
int func()
{
int the_n = N;
T the_t(N);

return the_n;
}

cout << func< a<2> >(); // Should print 2

Wait, but a<2> is not a template. It's a concrete class...
Any ideas?

I am not sure what you need it for, but try this:

template<template<int> class T, int N> int func()
{
T<N> the_t(N); // instantiate the template and the class
return N;
}

template<int N> struct a { a(int) {} }; // the template

#include <iostream>

int main()
{
std::cout << func<a,2>() << std::endl;
std::cout << func<a,10>() << std::endl;
}

V
Don't top-post. Rearranged:
> Thanks for the help,
>
> Unfortunately, for legacy reasons, the format I have to use is:
>
> a< b<2> >
That "format" (syntax) may not give you what you want. What is 'b'?
How is it defined?
> For example, it would look something like this:
>
> sc_in< sc_lv<8> > p;


Again, what's 'sc_lv'? How is it defined? Do you have control over it?
Or do you have to deal with a bunch of existing classes/templates?

Post more code. Your questions are too generic. Not enough information.
> I would like to be able to somehow defer construction of each class
> within that statement, so that I can do something like this:
>
> function( p );
>
> template< class T<class N> >


I showed you the available syntax. 'class T<class N>' is incorrect.

I am perfectly aware of the fact that such syntax is incorrect,
however, I'm looking for something with similar semantics. What you
suggested does not provide that. Ordinarily, I would have used your
suggested format, but the library I am working with is written in that
specific format and there is absolutely nothing I can do to change
that.
> void function( T<N> in )
> {
> // Store the fact that a type T was passed, and store the fact that
> it was of type N


It might be possible to interrogate the 'T' _if_ it has appropriate
members that somehow keep 'N' and are willing to expose it.
> }
>
> In some other function, I would say:
>
> void function2()
> {
> // in was of type T with argument N, so construct a new T of type N


What 'in'? This function doesn't have any 'in'. How are you going to
communicate the information about the argument of one function template to
another function? Are those functions members of the same class? Post
more code to explain.


If you want me to be pedantic about it: I want to have one function
(function1) collect an argument who's type is defined at run time, but
corresponds to the preceeding templated format. I would then like to
store the fact that a type "T" was passed containing an specialisation
(if that is the correct vernacular) of type "N" and then, in another
function, create objects of type T and N when necessary. C++ doesn't
allow this to happen, as far as my knowledge goes, dynamically, but
from looking over BOOST I've noticed that there are a few people out
there that are able to work wonders such as this with C++ and do it
using static metaprogramming techniques.
> }
>
> I'm not really seeing how C++ can do this, with my knowledge at least.


I believe you.

So far you haven't provided any reasonable suggestions either, so
unless you are able to do, so I suggest climbing down from that high
horse and rid yourself of the superiority complex you are showing to
this admitted learner asking for assistance.
> But, I was hoping that their was at least some method somewhere of
> which I wasn't aware. I was looking at the BOOST meta programming
> library, but to be honest I don't really know much about it, and would
> really like to know if such a construction is even possible before I
> try to implement it. Is it possible?


It might be possible, depending on what those types you're going to use
are.

V


Aug 19 '05 #5

P: n/a
X-no-archive: yes
Steve wrote:
Victor Bazarov wrote:
Steve wrote:
> I'm not really seeing how C++ can do this, with my knowledge at least.


I believe you.


So far you haven't provided any reasonable suggestions either, [..]


You know what, screw you. I tried, but you seem to be unresponsive.
If you are as you claim a "learner", get your head out of your ass
and read my replies again. What you're asking is only possible if
the _class_ you pass to your function can be interrogated about what
_used to be_ a template argument. That means that the _class_ may
have retained that information for others to extract. If it didn't,
you're SOL. And don't get me this "get off high horse crap". If you
have a problem with my posts, don't read them.
Aug 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.