|
-
August 17th, 2012, 04:27 AM
#1
template classes and inline methods
hi,
i have a template class which defines a few heavy methods. For now, they are defined in the same .h file as the class definition, but i`d like to have them in a separate .cpp file.
A situation i find you describe in the FAQs arises:
http://forums.codeguru.com/showthrea...-template-code
Problem: the export keyword has been deprecated in c++0x, if i recall correctly, and has never been implemented in any of the compilers i am using (msvc, gcc).
(maybe one could mention that in the FAQ...)
#Including the the .cpp file after the class definition (as described in the second post of the FAQ) works, but i`m not a huge fan of it.
Is there any other way?
another question: i have methods that dont use any template code. Can i somehow declare them as such? (more of an esthecial question, which would make it easier to distinguish between template and non.template code).
thanks.
Last edited by tuli; August 23rd, 2012 at 05:59 AM.
Reason: tried to mark as solved
-
August 17th, 2012, 10:28 AM
#2
Re: template classes and inline methods
 Originally Posted by tuli
Is there any other way?
yes and no
you can explicitly instantiate templates ( function, classes or single members ) and, in c++11, you can inhibit the instantiation of a template via an extern template declaration ( termed "explicit instantiation declaration" in the c++11 lingo ). However, you need to know the to be instantiated types in advance in order for this to work; moreover, explicit instantiation is not the same of an implicit instantiation for a given code involving class templates, in general.
So, there's no real separate compilation model for c++ templates ( well, ignoring "export"; but, as you noted, it's been deprecated for good reasons ... ).
as a side note, IMHO "cpp" files should be left to real source files; some alternative extension would be more appropriate ( personally, I like the "inl" extension ).
BTW, the thread title says "template classes and inline methods" but nowhere you speak about inline's ... just to be clear, note that just including the template members definitions as a separate file does not make them "inline", which is an entirely different matter ( in this respect the FAQ seems incorrect by suggesting to inline the function ... ).
 Originally Posted by tuli
another question: i have methods that dont use any template code. Can i somehow declare them as such? (more of an esthecial question, which would make it easier to distinguish between template and non.template code).
what do you mean ? if a method doesn't "use any template code" then why is it a member of a class template ?
-
August 18th, 2012, 10:47 AM
#3
Re: template classes and inline methods
ok. Looks like i`ll have to do with a massive header file.
I took a look at other sources i have around, and most of them just dump member definitions in the .h file. well.
You are, ofcourse, right about the inline keyword. There was another issues that magically resolved itself while i described it. 
if a method doesn't "use any template code" then why is it a member of a class template ?
How could it not be, if it`s a member of "all" classes generated from the template.?
ie
Code:
template<typename T>
class foo
{
...
static const int MyConst(){return 1234;}
...}
2)
while we`re at it, another small issue:
i want to create wrapper classes for old, plain structs. (yes, it has to be a wrapper).
There are two versions of most structs, a 32bit one and a 64bit one. Very rarely members have been added/deprecated between the 32/64 versions.
eg
Code:
struct a32
{
long x;
long y;
long z;
};
struct a64
{
//x extendted to longlong, z deprecated, w introduced
LONGLONG x;
long y;
long w;
};
template<class Tc, typename Tsize>
class myclass
{
Tc m;
Tc* getptr(){return &m;}
Tsize getx(){return m.x;}
};
This works just fine:
Code:
myclass<a32> x;
myclass<a64> x;
but when i bundle multiple structs in one class, it gets messy:
Code:
myclass<a32,b32,c32,d32,e32> x;
etc.
Can i simplify this somehow, as there are only two possibilities (32bit vs 64bit)? (besides typedef myclass<...> myclass32).
I´d love to do something like
myclass<32> x;
And then handle that somehow in the template decleration. I am sure i have seen this before somehow somewhere, but i cant seem to find it right now...will continue searching, though.
Thanks for your help!
-
August 19th, 2012, 11:45 AM
#4
Re: template classes and inline methods
 Originally Posted by tuli
How could it not be, if it`s a member of "all" classes generated from the template.?
yes, I understand how it could be so, the problem is why it should be so ? if a member does not depend somehow on the template parameters then probably it should not be there in the first place, it's a design issue and a possible symptom of templates abuse ( yes, sometimes one could write a class template to fulfill some prescribed requirements that happen to be trivial, being implemented by some "fixed" function as in your code snippet above. Even then, such a case is an implementation detail that should not be exposed at the interface level ).
Of course, your specific situation could have a good rationale ... but your invoking a way of systematically "declaring methods that don't use any template code" made me suspect of a too common occurrence of such use cases, that would surely be a symptom of bad design ...
 Originally Posted by tuli
Can i simplify this somehow, as there are only two possibilities (32bit vs 64bit)? (besides typedef myclass<...> myclass32).
something like
Code:
// aX.h
struct a32
{
long x;
long y;
long z;
};
struct a64
{
//x extendted to longlong, z deprecated, w introduced
LONGLONG x;
long y;
long w;
};
template< int Ver >
struct aX {};
template<>
struct aX<32> { typedef a32 type; };
template<>
struct aX<64> { typedef a64 type; };
//main.cpp
#include <aX.h>
template< int Ver >
class wrapper
{
typename aX<Ver>::type a;
};
wrapper<32> w;
note that if they are just pod's, you can derive ( or compose with ) aX specializations from their corresponding structs versions, in order to simplify "typename aX<Ver>::type a1;" to just "aX<Ver> a;". Moreover, in view of possible future changes/extensions, it may be better to use a type to hold the version information, in place of the "int" non-type template parameter ...
-
August 19th, 2012, 02:22 PM
#5
Re: template classes and inline methods
 Originally Posted by tuli
How could it not be, if it`s a member of "all" classes generated from the template.?
In that case. You should wonder if that member should really be part of the template, or if it should be part of a non-templated base class that all the templated classes are derived from.
-
August 20th, 2012, 10:14 AM
#6
Re: template classes and inline methods
 Originally Posted by tuli
How could it not be, if it`s a member of "all" classes generated from the template.?
ie
Code:
template<typename T>
class foo
{
...
static const int MyConst(){return 1234;}
...}
In this case "MyConst" will probably get inlined anyways, but imagining that MyConst was complex, then the usual method is to create a "non template X" where x is one of:
a)Base class
b)Standalone function
c)Member variable
and defer the call to that. Which is the best depends on context as always. :/
For example:
Code:
class base_foo
{
static const int MyConst(){return 1234;}
}
template<typename T>
class foo : public base_foo
{
...
}
By doing this, you can defer anything and everything that does not depend on T inside base_foo. Implementations can then go inside base_foo.cpp (or any other .cpp) of course.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
August 23rd, 2012, 05:58 AM
#7
Re: template classes and inline methods
ok, i now have all MyConst-like functions in base class, which the class template derives from.
Works perfectly, thanks for all the answers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|