473,405 Members | 2,282 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,405 software developers and data experts.

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

Aug 18 '05 #1
5 2144
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
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
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

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

Similar topics

16
by: Andrea A | last post by:
Hi, i'm developing a website that will have an huge amount of visitors a day --> it will be a contest with 100.000$ of prize. I'm concerning about using a template class (and which one do you...
2
by: Nikolai Borissov | last post by:
Is it possible to specialize a templated class defined within another templeted class, like below: template<typename U> struct A { template<typename T> struct B; }; // Attempt to...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
1
by: suranap | last post by:
I am looking at some examples of template metaprogramming, but I'm not clear on some details of templates. In particular, what kinds of computations will be performed at compile-time? Is it only...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.