Topic: how to invoke templated constructor if no arguments?
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 10 Feb 2001 12:20:00 -0500 Raw View
siemelnaran@my-deja.com wrote:
>
> struct Junk
> {
> template <class T> Junk();
> };
>
> How do I create a Junk object initialized with Junk::Junk() with T==int.
You can't. As section 14.8.1p5 says: "[_Note:_ because the explicit
template argument list follows the function template name, and because
conversion member function templates and constructor member function
templates are called without using a function name, there is no way to
provide an explicit template argument list for these function
templates.]"
What you need is to give it an argument which allows the template type
to be deduced implicitly:
template <class T> Junk(const T&);
The constructor doesn't have to do anything with the argument, it just
needs to have it. Then you can invoke it with, for instance,
Junk junk((int)5);
If there's some major overhead to creating an object of type T, such
that you don't want to have to create one just for the purpose of
constructing a Junk<T>, then an alternative would be to use a helper
template, whose sole purpose is to abstract out the type information for
a given type:
template <class T> Abstract
{
typedef T type; // Not actually used in this example
// Default constructor implicitly declared and
// implicitly defined as a no-op.
};
template <class T> Junk(const Abstract<T>&);
Then you can create a Junk() using the 'int' version of the constructor
by writing the following:
Junk junk(Abstract<int>());
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@spamcop.net>
Date: 10 Feb 2001 12:21:12 -0500 Raw View
siemelnaran@my-deja.com wrote:
>
> struct Junk
> {
> template <class T> Junk();
> };
>
> How do I create a Junk object initialized with Junk::Junk() with T==int.
>
> Junk<int> junk; // but the outer object has Junk<int>
>
> Junk junk<int>; // could this be it?
>
You can't do this at all. What on earth does this mean? You can provide
multiple constructors only when they have differening arguments.
It's not clear what you are trying to do. I suspect maybe the following
construct may work for you:
struct CommonJunk {
// most of what is in your junk class.
};
template <typename T> struct Junk : CommonJunk {
Junk() { };
};
You can then use the specialized Junks to initialize objects
of type CommonJunk (or references/pointers to them).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Sebastian Moleski \(SurakWare\)" <smoleski@surakware.com>
Date: 10 Feb 2001 12:21:48 -0500 Raw View
<siemelnaran@my-deja.com>:
> struct Junk
> {
> template <class T> Junk();
> };
>
> How do I create a Junk object initialized with Junk::Junk() with T==int.
>
> Junk<int> junk; // but the outer object has Junk<int>
>
> Junk junk<int>; // could this be it?
Junk junk = Junk<T>();
sm
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: 10 Feb 2001 17:31:08 -0500 Raw View
<siemelnaran@my-deja.com> wrote...
> struct Junk
> {
> template <class T> Junk();
> };
>
> How do I create a Junk object initialized with Junk::Junk() with
T==int.
>
> Junk<int> junk; // but the outer object has Junk<int>
>
> Junk junk<int>; // could this be it?
I am sorry, I don't have an answer yet. But I have a question
for you: why a templated c-tor with no arguments in a non-templated
class?
Victor
--
Please remove capital A's from my address when replying by mail
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Torsten Robitzki <Torsten@Robitzki.de>
Date: 10 Feb 2001 17:32:04 -0500 Raw View
hi,
siemelnaran@my-deja.com wrote:
>
> struct Junk
> {
> template <class T> Junk();
> };
>
> How do I create a Junk object initialized with Junk::Junk() with T==int.
>
> Junk<int> junk; // but the outer object has Junk<int>
>
> Junk junk<int>; // could this be it?
>
i would say it's impossible. The constructor of a class is strictly
bound to the type (class) to construct. What do you want do gather
from this construct?
regards
Torsten
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: qrczak@knm.org.pl (Marcin 'Qrczak' Kowalczyk)
Date: 11 Feb 01 07:52:54 GMT Raw View
10 Feb 2001 12:20:00 -0500, James Kuyper Jr. <kuyper@wizard.net> pisze:
> If there's some major overhead to creating an object of type T, such
> that you don't want to have to create one just for the purpose of
> constructing a Junk<T>, then an alternative would be to use a helper
> template, whose sole purpose is to abstract out the type information for
> a given type:
Or just the null pointer of type T.
--
__("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
\__/
^^ SYGNATURA ZAST PCZA
QRCZAK
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: siemelnaran@my-deja.com
Date: 11 Feb 01 10:28:58 GMT Raw View
In article <9619ol$sv0$1@nnrp.atgi.net>,
"Victor Bazarov" <vAbazarov@dAnai.com> wrote:
> I am sorry, I don't have an answer yet. But I have a question
> for you: why a templated c-tor with no arguments in a non-templated
> class?
To add creatable objects to my registry before entering main(). I
would write this in the respective cpp files --
Registry::Add add<Check>;
Registry::Add add<Withdrawal>;
I ended up just making the whole class Registry::Add into a template,
and I made it's only 2 functions (the constructor and the destructor)
inline and defined in the header file.
--
------------
Siemel Naran
Sent via Deja.com
http://www.deja.com/
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: siemelnaran@my-deja.com
Date: 11 Feb 01 10:30:10 GMT Raw View
In article <3A83F25D.C80F7FDE@wizard.net>,
"James Kuyper Jr." <kuyper@wizard.net> wrote:
> The constructor doesn't have to do anything with the argument, it just
> needs to have it. Then you can invoke it with, for instance,
>
> Junk junk((int)5);
>
> If there's some major overhead to creating an object of type T, such
> that you don't want to have to create one just for the purpose of
> constructing a Junk<T>, then an alternative would be to use a helper
> template, whose sole purpose is to abstract out the type information
for
> a given type:
No. Use a pointer. I did this for my dynamic_ptr class because MSVC
doesn't support the .template notation.
template <class T>
template <class U>
U * dynamic_ptr<T>::dynamic_release(U *) {
U * out=dynamic_cast<U *>(this->d_ptr);
assert(out);
this->d_ptr=0;
return out;
}
--
------------
Siemel Naran
Sent via Deja.com
http://www.deja.com/
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 11 Feb 01 10:30:52 GMT Raw View
"Sebastian Moleski (SurakWare)" wrote:
>
> <siemelnaran@my-deja.com>:
> > struct Junk
> > {
> > template <class T> Junk();
> > };
> >
> > How do I create a Junk object initialized with Junk::Junk() with T==int.
> >
> > Junk<int> junk; // but the outer object has Junk<int>
> >
> > Junk junk<int>; // could this be it?
>
> Junk junk = Junk<T>();
Not quite. See 14.8.1p5, which I've already quoted in another message on
this thread.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: siemelnaran@my-deja.com
Date: 09 Feb 01 07:21:31 GMT Raw View
struct Junk
{
template <class T> Junk();
};
How do I create a Junk object initialized with Junk::Junk() with T==int.
Junk<int> junk; // but the outer object has Junk<int>
Junk junk<int>; // could this be it?
--
------------
Siemel Naran
Sent via Deja.com
http://www.deja.com/
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 10 Feb 01 09:27:26 GMT Raw View
In article <95t9vd$top$1@nnrp1.deja.com>, siemelnaran@my-deja.com writes
>struct Junk
>{
> template <class T> Junk();
>};
>
>How do I create a Junk object initialized with Junk::Junk() with T==int.
>
>Junk<int> junk; // but the outer object has Junk<int>
>
>Junk junk<int>; // could this be it?
Indeed, it is the constructor that is templated, not the type.
--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]