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

Is there a way to span a suite of explicit instantiations automatically?

P: n/a
Hi,

I am creating a template library for a template class with two template
arguments.
Is there a way to span a suite of explicit instantiation, instead of
having to specify all the combinations of the template arguments
specifically?

Say I have a class

template <class T1, class T2>
class Foo
{
T1 t1;
T2 t2;
}

with T1, T2 having the following possibilities:

T1 - unsigned char, short, unsigned short, long
T2 - unsigned char, short, float, double

I was thinking of some way like a function that scans all the
combinations and constructs the class for each of the combinations.
Maybe having the function as static will force the generation of the
code, which will force the generation of the class code for each
combination of the template arguments.
Something like:

template <class S1, class D>
static void foo()
{
loop on all T1 possibilities
{
loop on all T2 possibilities
{
Foo<T1, T2> *p = new Foo<T1, T2>;
}
}
}
Thanks,
Avner

Jun 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Avner wrote:
I am creating a template library for a template class with two
template arguments.
Is there a way to span a suite of explicit instantiation, instead of
having to specify all the combinations of the template arguments
specifically?
Create an XML file and then XSLT for it to span the sets. Output C++
code from it. Then find a command-line XSLT applier (translator) and
insert it into your build process.
[...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '06 #2

P: n/a
Avner wrote:
Hi,

I am creating a template library for a template class with two template
arguments.
Is there a way to span a suite of explicit instantiation, instead of
having to specify all the combinations of the template arguments
specifically?

Say I have a class

template <class T1, class T2>
class Foo
{
T1 t1;
T2 t2;
}

with T1, T2 having the following possibilities:

T1 - unsigned char, short, unsigned short, long
T2 - unsigned char, short, float, double

I was thinking of some way like a function that scans all the
combinations and constructs the class for each of the combinations.
Maybe having the function as static will force the generation of the
code, which will force the generation of the class code for each
combination of the template arguments.
Something like:

No, templates are instantiated at compile time, so you can't instantiate
them at run time.

Just write a simple code generator to spit out a test file and add it as
a step in your build process.

--
Ian Collins.
Jun 27 '06 #3

P: n/a
Ian Collins wrote:
No, templates are instantiated at compile time, so you can't instantiate
them at run time.

Just write a simple code generator to spit out a test file and add it as
a step in your build process.

--
Ian Collins.


Can yoy give me an example of how the simple code generator should look
like and explain how I should add it into the build process?

Thanks,
Avner

Jun 27 '06 #4

P: n/a
Ian Collins wrote:
No, templates are instantiated at compile time, so you can't instantiate
them at run time.

Just write a simple code generator to spit out a test file and add it as
a step in your build process.

--
Ian Collins.


Can yoy give me an example of how the simple code generator should look
like and explain how I should add it into the build process?

Thanks,
Avner

Jun 27 '06 #5

P: n/a
Avner wrote:
Ian Collins wrote:
No, templates are instantiated at compile time, so you can't instantiate
them at run time.

Just write a simple code generator to spit out a test file and add it as
a step in your build process.

--
Ian Collins.

Can yoy give me an example of how the simple code generator should look
like and explain how I should add it into the build process?

You should set your news reader not to quote sigs (the bit below the
'--'). This is normally the default.

The code generator would look very much like you original function
pseudo code, except rather than

loop on all T2 possibilities
{
Foo<T1, T2> *p = new Foo<T1, T2>;
}

It would have something like
loop on all T2 possibilities
{
out << "Foo<" << T1 << ',' << T2 << "> *p = new Foo<" << T1 <<','
<< T2 <<">;" << std::endl;
}

where out is an ostream&

How you add it to your build process depends on what that process is!

--
Ian Collins.
Jun 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.