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

A

Avner

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
 
V

Victor Bazarov

Avner said:
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
 
I

Ian Collins

Avner said:
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.
 
A

Avner

Ian said:
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.

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
 
A

Avner

Ian said:
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.

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
 
I

Ian Collins

Avner said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top