specify template parameters for ctor

Started by
6 comments, last by ProPuke 18 years, 8 months ago
__ ____ ____ ____ ____
 _|__ _|__ _|__ _|__ _|
|____|_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)
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Advertisement
Wouldn't you have to implement the whole class as a template class?

ace
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;};
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?
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Quote:Original post by SiCrane
You can't use explicit template instantiation with constructors.

*cough* explicit template argument passing *cough*
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
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).
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]
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python

This topic is closed to new replies.

Advertisement