 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christof Warlich Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: templates and local types |
|
|
Hi,
Consider:
template<typename T> class Z {};
int doSomething(void) {
class X {};
Z<X> z;
}
Any idea how this can be made compilable while keeping class X local?
The example has been simplified significantly. The reason why class X
has to be local is because doSomething() is a template function and
class X needs to be instantiated with the same type as doSomething().
Thanks for any help,
Christof |
|
| Back to top |
|
 |
Christof Warlich Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: templates and local types |
|
|
John Carson wrote:
| Quote: | In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both templates.
Then what is wrong with:
template<typename T> class Z {};
template<typename T> class X {};
template<typename T
int doSomething(void)
{
Z< X<T> > z;
return 0;
}
You are right, I've probably stripped down things too much. But some |
additional explanations may do:
My intention was to separate template declaration and definition (See
FAQ Lite: How can I avoid linker errors with my template functions).
Doing this, I wanted to minimize the lines of code that have to be
written to explicitly instantiate my templates (in this simple case,
just instantiating doSomething() for the required types instead of
having to instantiate doSomething() and class X). |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: templates and local types |
|
|
"Christof Warlich" <cwarlich (AT) gmx (DOT) de> wrote in message
news:44e2c5fc$0$6990$9b4e6d93 (AT) newsspool1 (DOT) arcor-online.net
| Quote: | John Carson wrote:
In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both
templates. Then what is wrong with:
template<typename T> class Z {};
template<typename T> class X {};
template<typename T
int doSomething(void)
{
Z< X<T> > z;
return 0;
}
You are right, I've probably stripped down things too much. But some
additional explanations may do:
My intention was to separate template declaration and definition (See
FAQ Lite: How can I avoid linker errors with my template functions).
Doing this, I wanted to minimize the lines of code that have to be
written to explicitly instantiate my templates (in this simple case,
just instantiating doSomething() for the required types instead of
having to instantiate doSomething() and class X).
|
You could turn your function into a functor, like std::cout. To get this to
work of course, you need to declare an object of the class, either (i) in
client code or (ii) in the implementation file, with an extern declaration
in the header. This gives an extra line (or two), so you may not think this
approach offers an advantage --- though it still could if you had classes A,
B, C... as well as X.
First, a version where it all goes in the header:
template<typename T>
class Z {};
template<typename T>
class doSomething
{
class X {};
public:
int operator()()
{
Z<X> z;
return 0;
}
};
// need to also declare an object of the class
Splitting this into .h and .cpp files, we would have:
////// foo.h file ///////////////////////////
template<typename T>
class doSomething
{
class X;
public:
int operator()();
};
extern doSomething<int> ds;
/////// foo.cpp file //////////////////////
#include "foo.h"
template<typename T>
class Z {};
template<typename T>
class doSomething<T>::X
{};
template<typename T>
int doSomething<T>::operator()()
{
Z<X> z;
return 0;
}
// only one instantiation line required
template class doSomething<int>;
// but need this line unless client code has it
doSomething<int> ds;
//////// main.cpp file /////////////////////
#include "foo.h"
int main()
{
ds();
}
/////////// end files ///////////////////////
The above seems to work with VC++ 2005.
--
John Carson |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: templates and local types |
|
|
"Christof Warlich" <cwarlich (AT) gmx (DOT) de> wrote in message
news:44e2bb52$0$1390$9b4e6d93 (AT) newsspool3 (DOT) arcor-online.net
| Quote: | Hi,
Consider:
template<typename T> class Z {};
int doSomething(void) {
class X {};
Z<X> z;
}
Any idea how this can be made compilable while keeping class X local?
The example has been simplified significantly. The reason why class X
has to be local is because doSomething() is a template function and
class X needs to be instantiated with the same type as doSomething().
|
In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both templates.
Then what is wrong with:
template<typename T> class Z {};
template<typename T> class X {};
template<typename T>
int doSomething(void)
{
Z< X<T> > z;
return 0;
}
--
John Carson |
|
| Back to top |
|
 |
Christof Warlich Guest
|
Posted: Thu Aug 17, 2006 9:10 am Post subject: Re: templates and local types |
|
|
John Carson schrieb:
| Quote: | You could turn your function into a functor, like std::cout. To get this to
work of course, you need to declare an object of the class, either (i) in
client code or (ii) in the implementation file, with an extern declaration
in the header. This gives an extra line (or two), so you may not think this
approach offers an advantage --- though it still could if you had classes A,
B, C... as well as X.
Hi John, |
thanks a lot, that's an interesting approach. I'll go and play around
with it today. But anyhow, I realized that I reached my goal also with
your very first suggestion: It was sufficient to instantiate doSomething
even when class X was not local; class X seems then to be instantiated
as well.
Regards,
Christof |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|