Jump to content
  • Advertisement
Sign in to follow this  
ProPuke

specify template parameters for ctor

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

__ ____ ____ ____ ____
 _|__ _|__ _|__ _|__ _|
|____|_C++|____|____|_
__|____|WALL|____|____|
|____|____|____|____|
I have a situation where a templated ctor is required The type itself needs to allocate an abstract type upon initialisation, so the ctor doesn't have any parameters dependant on the template If this were the case with an ordinary function:   template <typename Type> void function() Then I'd obviously just explicitly specify the template parameters:   function<int>() The big question is, ofcourse, how do I specify template parameters when calling a ctor? I'll probally write a static function to take the place of the constructor for the moment, but is there a syntaxically correct way of achieving this? I'm using gcc version 3.4.2 (mingw-special)

Share this post


Link to post
Share on other sites
Advertisement
You can't use explicit template instantiation with constructors. One option is to use a type-type parameter. ex:


template <typename T>
struct TypeType {
typedef T type;
};

Share this post


Link to post
Share on other sites
Thanks SiCrane

For this particular instance it makes more sense to use a static method in place of this "special" ctor
But that typedef trick is something I wouldn't of thought of :] cheers

so many oddities... :/ why does c++ allow you to declare them if you can't call them?

Share this post


Link to post
Share on other sites
This is the problem, right?


class foo {
template <typename T>
foo() {
delete new T(); // stupid example
}
};

int main() {
foo<int> f; // oops, tries to make a foo<int>, rather than telling the foo ctor to
// allocate and then deallocate an int.
}

// bar.cpp: In function `int main()':
// bar.cpp:9: error: `foo' is not a template
// bar.cpp:9: error: no matching function for call to `foo::foo()'
// bar.cpp:1: note: candidates are: foo::foo(const foo&)



If I've got you this far... this begs the question, why *isn't* the class templated on that type? What can you do that's meaningful with the templated ctor if there's no templated storage within the class (which would require a class template) and there are no ctor parameters dependant on the template? O_O

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
What can you do that's meaningful with the templated ctor if there's no templated storage within the class (which would require a class template) and there are no ctor parameters dependant on the template? O_O

Lots of things, for instance the template parameter could be a child type of a type whose associated pointer type is a datamember. The constructor can initialize the pointer datamember to a newly allocated instance of the child type. Also, the template parameter can be used as a form of "named constructor." I.E. Perhaps sometimes you wish to construct an object in a certain way which takes no runtime datamembers and other times you wish to initialize it in another fasion. Neither may logically have to take function arguments -- what you really want is two separately named constructors, which isn't possible. Your alternative is to template the constructor, make a non-template function argument which determines which way to initialize the object, or use entirely separate functions to create and return the object (which would either require the object to have a copy constructor or be dynamically allocated).

Share this post


Link to post
Share on other sites
stored as abstract base class ;]

extra:

hmm, forgot that the template type could only be created once the first type had been allocated - so the static is out
I've switched to using a real ctor & the template method that you suggested SiCrane.. but with one exception:

  template <typename Type> struct Template_type {};

no need for a typedef :P

  template <typename Type> Structure::Structure(Template_type<Type> type)

thanks all

[Edited by - ProPuke on September 11, 2005 12:25:33 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!