"Karthik Kumar" <kaykaylance_nospamplz@yahoo.com> escribió en el mensaje
news:418b458b$1@darkstar...[color=blue]
> Ruben Campos wrote:[color=green]
>> I've found a problem with types defined inside a template. With a
>> non-template class, I can write the following:
>>
>> // MyClass.hpp
>> class MyClass
>> {
>> // ...
>> typedef unsigned int MyType;
>> MyType MyMethod ( /* ... */ );
>> // ...
>> };
>>
>> // MyClass.cpp
>> MyClass::MyType
>> MyClass::MyMethod ( /* ... */ )
>> {
>> // ...
>> }
>>
>> This code works well. Of course, I can include a using declaration "using
>> MyClass::MyType" in MyClass.cpp file, before MyMethod implementation,
>> instead of specifying the path in its return value.
>>
>> Well, my problem is that this don't work with templates:
>>
>> // MyTemplate.hpp
>> template <typename T>
>> class MyTemplate
>> {
>> // ...
>> typedef unsigned int MyType;
>> MyType MyMethod ( /* ... */ );
>> // ...
>> };
>>
>> // MyTemplate.cpp
>> template <typename T>
>> MyTemplate <T>::MyType
>> MyTemplate <T>::MyMethod ( /* ... */ )
>> {
>> // ...
>> }
>>[/color]
>
> Well - the *definition* for the template method ought to be
> in a single header file and you cannot separate the declaration
> and definition of templates, as you would do for classes. (
> at least not until 'export' is widely implemented. Currently,
> there are very few implementations that support that and so,
> never bother anyway).
>
> --
> Karthik.
http://akktech.blogspot.com .
> ' Remove _nospamplz from my email to mail me. '[/color]
You're right. I forgot to say that, in the template case, the .cpp file is
included inside the .hpp file. In fact, when considering optimization with
inline methods, I use a three file (.hpp, .cpp and .inl) scheme like this:
// MyTemplate.hpp
// MyTemplate <T> header file
template <typename T>
class MyTemplate
{
// MyTemplate <T> class declaration ...
};
#ifndef DISABLE_INLINE
#include "MyTemplate.inl"
#endif
#ifndef EXTERNAL_TEMPLATE_DEFINITION
#include "MyTemplate.cpp"
#endif
// MyTemplate.cpp
// MyTemplate <T> non-inline implementation file
#ifdef EXTERNAL_TEMPLATE_DEFINITION
#include "MyTemplate.h"
#endif
// MyTemplate <T> non-inline method implementation...
#ifdef DISABLE_INLINE
#include "MyTemplate.inl"
#endif
// MyTemplate.inl
// MyTemplate <T> inline implementation file
#ifdef DISABLE_INLINE
#define inline
#endif
// MyTemplate <T> inline method implementation, including the 'inline'
keyword ...
#ifdef DISABLE_INLINE
#undef inline
#endif
I use this scheme for both template and non-template classes. Template
method definitions are really included inside header file while remain
physically in a separate file. So this is not a problem.