Template specialisation lookup.

P

pauljwilliams

Consider the following scenario:

A template class Diag is defined for a generic class T, with a single
member function, State(), returning a boolean, defined as returning
false.

For some types T, the template is specialized such that State returns
true. In this sense, the template class as akin to a policy class.

At compile time, how does the compiler search the source tree for all
possible specialisations?

If i have a line:

if ( Diag<MyType>().State() )
{
....do something...
}

How will the compiler know that there is (or isnt) a specialisation of
Diag for MyType? How far will it look?

Thanks in advance
 
V

Victor Bazarov

Consider the following scenario:

A template class Diag is defined for a generic class T, with a single
member function, State(), returning a boolean, defined as returning
false.

For some types T, the template is specialized such that State returns
true. In this sense, the template class as akin to a policy class.

At compile time, how does the compiler search the source tree for all

What's "the source tree"?
possible specialisations?

If i have a line:

if ( Diag<MyType>().State() )
{
...do something...
}

How will the compiler know that there is (or isnt) a specialisation of
Diag for MyType? How far will it look?

The only way for the compiler to know that there is a specialisation is
if the specialisation is _declared_ and that declaration is visible to the
compiler. If it isn't visible, the complier will create one (and if the
definition is visible, it will instantiate it), and later the linker will
probably be responsible for resolving the multiplicity of template
instances.

V
 

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,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top