The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

C++ template question (just out of curiousity) by blowfish2007-05-06 08:53:11
  Maybe... by fuzzyeric 2007-05-06 15:43:53
If all specializations and finalizations of foo<> in the translation unit have a common base class, then any &foo<> can be held in a pointer to that base. Note that only methods and members in that base are accessible by that pointer. However, the cast back into the derived form is going to be very tricky.

Note that until foo<> is instantiated with completed types, it doesn't exist. This is the difference between a "type" and an "instance of that type". Types don't have addresses and instances do. You can't have an instance until you have a completed type. A completed template requires specifying its arguments.

You may engage in partial specialization of a template type, but this just turns a type into a type. No instance is made.

In your second example, "foo<i,t> list; foo &item = list;", either "i" and "t" are completed types or they are not. If they are completed, then the declaration of "list" is of a completed type ("foo<i,t>") and the second use of "foo" is gibberish. If they are not completed types, then the attempt to declare "list" is gibberish because you can't make instances of incomplete types.

I'm just belaboring this point because it's very easy to think that the solution is to use incomplete types and there's no way to get there from here.

Further, you seem to want to hold an instance of a foo<> *without* knowing its final type. There are two methods for doing this: either know a base type that you can cast down to, or use a void pointer. These are pretty much the ways that one can ignore the type of an rvalue (... how you're using "list" in your assignment).

I know that usually, the short examples one provides in questions are too short to really get the point across, but, ...

foo<i,t> list, &item = list;

would be fine. But you're re-using your information about the final type of "list" here, and your question seems to assume that this isn't possible.

Finally, you might be able to templatize the functions that you want to *use* your foo<> type. For instance

template<typename i, typename t>
int bar(list &foo<i,t>) {...}

This gives you a function that can act on an instance of foo<> that you don't have to explicitly specify its template parameters. You can access foo<>'s members and methods in bar(). Much like template class member functions, bar() is automatically instantiated for every completed usage that appears in its translation unit. So, either this code appears in a header in every file that uses bar(), or you specifically instantiate bar() in its file(s) so that the linker can find a function to link to your uses elsewhere.
[ Reply ]

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)