Topic: What is explicit instantiation for?
Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1998/08/10 Raw View
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>scott douglass <sdouglass%_%junk@_.arm.com> writes:
>
>>> hiding its implementation, you can restrict the set of
>>> specializations that can be used.
>
>> But the same thing doesn't seem to apply to template classes which have
>> to be defined to be used in ways that require them to ever be
>> instantiated
>
>That's right. Just after pressing the send button I thought I should
>have made that remark, that the statement was only valid for template
>functions.
Not true:
X.h:
template <class T> class X
{
public:
X(T); // Constructor
T f();
// ...
};
// NOTE: No implementation specified for X<T>::f() or X<T>::g()
a.C
#include "X.h"
X<int> xi;
// ...
X.C
#include "X.h"
template <class T> X<T>::X(T t) { /* ... */ }
template <class T> T X<T>::f() { /* ... */ }
// Explicit instantiations:
template X<signed char>;
template X<signed short>;
template X<signed int>;
template X<signed long>;
Since the implementation of X<T> is not exported, only the explicit
instantiations will have definitions for the constructor. Thus, only the
explicitly instantiated versions can be used. In the example above,
X<T> can only be used with the built-in signed integral types. The
situation is exactly the same as the case of other template functions,
except that these happen to be member functions instead of global
functions.
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
I am self-employed. Therefore, my opinions *do* represent
those of my employer.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/07/25 Raw View
scott douglass wrote:
> Hello,
>
> I'm trying to figure out what explicit instantiation if for.
>
Explicit instantation is a mechanism used to improve compile time speeds.
According to Stroustrup's "The C++ Programming Language" 3rd ed., "Explicit
instantiation is an optional mechanism for optimization and manual control of
the compile and link process.
> Can you give me an example of a program that won't work without
> explicit instantiation?
>
Stroustrup says that expicit instantiation can be used to force compilation
errrors to occur at predictable timees rather tan occurring whenever and
implementation determines the need to generate a specialization and also
gives an example for it's use as a constraints check:
template<class T> class Calls_foo {
void constrains(T t) { foo(t); } // call from every constructor
};
template class Calls_foo<int>; // error: foo(int) undefined
template Calls_foo<Shape*>::constraints(); // errorfoo(Shape*) undefined
> Is it legal to explicitly instantiate a template in one compilation
> unit and implicitly instantiate the same template in another?
> I would expect it was illegal but I could not find any prohibition
> in the draft standard.
>
> Similarly, is it legal to explicitly specialize a template in one
> compilation unit and implicitly instantiate the same template in
> another?
>
"It is an error to have two definitions for the same specialization. It does
not mater if such multiple specializations are user defined, implicitly
generated or explicitly requested." p.867
Lastly, I think, but am very unsure, that one can use explicit instantation
to cut compile time by explicitly instatiating templates and then link in the
actual code by either linking in a library or through the use of a dll or
shared library.
Hope this was helpful,
Terence Kelling
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: kkelley@mit.edu (Ken Kelley)
Date: 1998/07/27 Raw View
In article <6p7q5g$i6n@sis.cambridge.arm.com>,
scott douglass <sdouglass%_%junk@_.arm.com> wrote:
>Hello,
>
>I'm trying to figure out what explicit instantiation if for.
>
>Can you give me an example of a program that won't work without
>explicit instantiation?
>
Not exactly, but I can give you an example of code that will
compile (but not work) without explicit instantiation.
class point {
public:
int x;
int y;
point(int tx = 0; int ty = 0);
bool operator==(const point& b) const
{return ((this->x==b.x)&&(this->y==b.y))};
}
int foo(point& p) {
int x = 7;
if (p == x) // Should read "if (p.x == x)"
p.y = 9;
}
There does not exist an operator==(point, int), so
the compiler looks for the nearest match. It creates
a temporary point object with values (7,0), and compares
that object with p. The intended code was to compare
p.x with x. If the constructor were declared explicit,
then the compiler would not be allowed to create a temporary
in this instance, and the code would fail to compile.
Of course, it is possible to modify this example slightly
(for example, by defining an int() method for point) which
leads to ambiguities if implicit construction is allowed,
but would compile fine if the constructor is defined explicitly.
That would give yet another behaviour for the above routine.
>Is it legal to explicitly instantiate a template in one compilation
>unit and implicitly instantiate the same template in another?
>
>I would expect it was illegal but I could not find any prohibition
>in the draft standard.
>
>Similarly, is it legal to explicitly specialize a template in one
>compilation unit and implicitly instantiate the same template in
>another?
>
I'm not sure whether it should be legal or not. On the one hand
whether it's declared explicit in one module doesn't affect
how another module is compiled. It only affects whether the
compiler is allowed to automatically generate temporaries as above.
But there might be bigger issues, like whether doing so would
be considered "good code" or "bad code".
Ken
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: scott douglass <sdouglass%_%junk@_.arm.com>
Date: 1998/07/29 Raw View
Alexandre Oliva wrote:
>
> scott douglass <sdouglass@_%junk%_.arm.com> writes:
>
> > I'm trying to figure out what explicit instantiation if for.
>
> There are two situations in which explicit instantiation is useful:
>
> 1) for programmers to control in which object file they intend a
> certain symbol to be defined. By carefully selecting places in which
> to instantiate templates, it may be possible to reduce the
> dependencies between modules of a program, causing some modules not to
> be needlessly included in an executable.
I don't think I follow this. If possible could you show a small
example?
> 2) in order to allow templates to be defined separately from their
> declarations, because, if the definition of a template is not provided
> in every translation unit in which it is used, it must be explicitly
> instantiated in some translation unit. This is not equivalent to
> exporting a template definition; exporting has to do with *all*
> possible specializations of a template, whereas by explicitly
> instantiating a template for a particular set of types, then hiding
> its implementation, you can restrict the set of specializations that
> can be used.
Ok, I can see how this could be used for template functions because you
can use a template function that is declared but not defined, e.g:
file1.c:
template <class T> void f(/*...*/);
void g() { f(/*...*/); }
file2.c:
template <class T> void f(/*...*/) { /* ... */ }
template void f<int>(/*...*/);
But the same thing doesn't seem to apply to template classes which have
to be defined to be used in ways that require them to ever be
instantiated, e.g:
file3.c:
template <class T> struct X;
void h() { X<int> x; } // illegal
void h2() { X<int>* p; } // doesn't need X<int> instantiated
file4.c:
template <class T> struct X { /* ... */ }
template struct X<int>;
Is that correct or am I missing some corresponding way to do it with
template classes?
> > Similarly, is it legal to explicitly specialize a template in one
> > compilation unit and implicitly instantiate the same template in
> > another?
>
> If you really meant `specialize' in the sentence above, then it is
> illegal, because it violates the ODR, but no diagnostic is required.
Thanks. But by my reading the ODR doesn't prohibit it. However, I
withdraw the question because I found this text in 14.7.3:
:If a template is explicitly specialized then that specialization shall
:be declared before the first use of that specialization that would
:cause an implicit instantiation to take place, in every translation
:unit in which such a use occurs.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/07/29 Raw View
scott douglass <sdouglass%_%junk@_.arm.com> writes:
> Alexandre Oliva wrote:
>> By carefully selecting places in which to instantiate templates, it
>> may be possible to reduce the dependencies between modules of a
>> program, causing some modules not to be needlessly included in an
>> executable.
> I don't think I follow this. If possible could you show a small
> example?
This problem is the same as reducing cross-dependency in static
libraries: if the implementation of every method of a class is stored
in a single object file of a library, whenever your program refers to
a symbol defined in that object file of the library, the whole object
file will be inserted in your executable. Then, the dependencies of
the imported object file will also have to be satisfied. If you're
not careful about the way you define the library, you may end up
with a copy of the whole library in every executable linked with it.
However, if you had defined each method in a separate object file,
only methods that were actually needed would be copied into the
executable.
Of course shared libraries and smart linkers can help, but, if none of
these are available, explicitly instantiating templates allows you to
create, say, one object file for each template specialization, causing
only the needed template definitions to be copied into the executable.
>> hiding its implementation, you can restrict the set of
>> specializations that can be used.
> But the same thing doesn't seem to apply to template classes which have
> to be defined to be used in ways that require them to ever be
> instantiated
That's right. Just after pressing the send button I thought I should
have made that remark, that the statement was only valid for template
functions.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/07/27 Raw View
scott douglass <sdouglass%_%junk@_.arm.com> writes:
> I'm trying to figure out what explicit instantiation if for.
There are two situations in which explicit instantiation is useful:
1) for programmers to control in which object file they intend a
certain symbol to be defined. By carefully selecting places in which
to instantiate templates, it may be possible to reduce the
dependencies between modules of a program, causing some modules not to
be needlessly included in an executable.
2) in order to allow templates to be defined separately from their
declarations, because, if the definition of a template is not provided
in every translation unit in which it is used, it must be explicitly
instantiated in some translation unit. This is not equivalent to
exporting a template definition; exporting has to do with *all*
possible specializations of a template, whereas by explicitly
instantiating a template for a particular set of types, then hiding
its implementation, you can restrict the set of specializations that
can be used.
> Can you give me an example of a program that won't work without
> explicit instantiation?
The second item above, in which a template is used in several
translation units, but is defined in only one of them, is one such
case.
> Is it legal to explicitly instantiate a template in one compilation
> unit and implicitly instantiate the same template in another?
Yes.
> Similarly, is it legal to explicitly specialize a template in one
> compilation unit and implicitly instantiate the same template in
> another?
If you really meant `specialize' in the sentence above, then it is
illegal, because it violates the ODR, but no diagnostic is required.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: scott douglass <sdouglass%_%junk@_.arm.com>
Date: 1998/07/23 Raw View
Hello,
I'm trying to figure out what explicit instantiation if for.
Can you give me an example of a program that won't work without
explicit instantiation?
Is it legal to explicitly instantiate a template in one compilation
unit and implicitly instantiate the same template in another?
I would expect it was illegal but I could not find any prohibition
in the draft standard.
Similarly, is it legal to explicitly specialize a template in one
compilation unit and implicitly instantiate the same template in
another?
Thanks,
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]