Template within a template | | |
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 | | | | re: Template within a template
Steve wrote:[color=blue]
> 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[/color]
Wait, but a<2> is not a template. It's a concrete class...
[color=blue]
>
> Any ideas?[/color]
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 | | | | re: Template within a template
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:[color=blue]
> Steve wrote:[color=green]
> > 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[/color]
>
> Wait, but a<2> is not a template. It's a concrete class...
>[color=green]
> >
> > Any ideas?[/color]
>
> 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[/color] | | | | re: Template within a template
Steve wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>>Steve wrote:
>>[color=darkred]
>>>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[/color]
>>
>>Wait, but a<2> is not a template. It's a concrete class...
>>
>>[color=darkred]
>>>Any ideas?[/color]
>>
>>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[/color][/color]
Don't top-post. Rearranged:
[color=blue]
> Thanks for the help,
>
> Unfortunately, for legacy reasons, the format I have to use is:
>
> a< b<2> >[/color]
That "format" (syntax) may not give you what you want. What is 'b'?
How is it defined?
[color=blue]
> For example, it would look something like this:
>
> sc_in< sc_lv<8> > p;[/color]
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.
[color=blue]
> 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> >[/color]
I showed you the available syntax. 'class T<class N>' is incorrect.
[color=blue]
> void function( T<N> in )
> {
> // Store the fact that a type T was passed, and store the fact that
> it was of type N[/color]
It might be possible to interrogate the 'T' _if_ it has appropriate
members that somehow keep 'N' and are willing to expose it.
[color=blue]
> }
>
> 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[/color]
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.
[color=blue]
> }
>
> I'm not really seeing how C++ can do this, with my knowledge at least.[/color]
I believe you.
[color=blue]
> 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?[/color]
It might be possible, depending on what those types you're going to use
are.
V | | | | re: Template within a template
Victor Bazarov wrote:[color=blue]
> Steve wrote:[color=green]
> > Victor Bazarov wrote:
> >[color=darkred]
> >>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[/color][/color]
>
> Don't top-post. Rearranged:
>[color=green]
> > Thanks for the help,
> >
> > Unfortunately, for legacy reasons, the format I have to use is:
> >
> > a< b<2> >[/color]
>
> That "format" (syntax) may not give you what you want. What is 'b'?
> How is it defined?
>[color=green]
> > For example, it would look something like this:
> >
> > sc_in< sc_lv<8> > p;[/color]
>
> 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.
>[color=green]
> > 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> >[/color]
>
> I showed you the available syntax. 'class T<class N>' is incorrect.
>[/color]
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.
[color=blue][color=green]
> > void function( T<N> in )
> > {
> > // Store the fact that a type T was passed, and store the fact that
> > it was of type N[/color]
>
> It might be possible to interrogate the 'T' _if_ it has appropriate
> members that somehow keep 'N' and are willing to expose it.
>[color=green]
> > }
> >
> > 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[/color]
>
> 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.[/color]
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.
[color=blue]
>[color=green]
> > }
> >
> > I'm not really seeing how C++ can do this, with my knowledge at least.[/color]
>
> I believe you.
>[/color]
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.
[color=blue][color=green]
> > 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?[/color]
>
> It might be possible, depending on what those types you're going to use
> are.
>
> V[/color] | | | | re: Template within a template
X-no-archive: yes
Steve wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>>Steve wrote:[color=darkred]
>> > I'm not really seeing how C++ can do this, with my knowledge at least.[/color]
>>
>>I believe you.
>>[/color]
>
> So far you haven't provided any reasonable suggestions either, [..][/color]
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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|