Topic: template question
Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/02/11 Raw View
"Bill Campbell" <neus@wolfenet.com> writes:
>if i have a template class declared as:
>
>template<class T, const char* typeName>
>class AddType{
>public:
> AddType(ifstream& in)
> ....
>
>where typeName is the name of the current instantiation of the class
>what is the syntax used to create an instantiation of the class. i've been
>trying the following without luck:
>
>ifstream in(int.txt);
That should be
ifstream in("int.txt";
of course.
>const char* name = "Int file";
>AddType<int, name> currentAdd(in);
To make this syntax legal, your template needs to take a (const)
_reference to_ a pointer to const char:
template<class T, char const * const & typeName>
class AddType {
...
}
The reason is the following:
| 14.1 Template parameters [temp.param]
|
| 3 A non-type template-parameter shall have one of the following (option-
| ally cv-qualified) types:
[...]
| --pointer to object, accepting an address constant expression desig-
| nating a named object with external linkage,
|
| --reference to object, accepting an lvalue expression designating a
| named object with external linkage,
[...]
In your original code, the second template parameter `name' was the
*value* of a named object with external linkage, not the address of one.
Note that with this definition of `AddType', if you define
const char* foo1 = "foo";
const char* foo2 = "foo";
then `AddType<int, foo1>' will be a different type to `AddType<int, foo2>'.
Another potential alternative to declare name as an array, rather
than as a pointer
const char name[] = "Int file";
and to the define the AddType template as in your original example:
template<class T, int N, const char *typeName>
class AddType {
...
}
However, as I read the draft, this alternative will not work, because
the result of the implicit array-to-pointer conversion is a pointer to
the first element of the array, which is an unnamed subobject, and
which hence does not satisfy the requirement of being an address
constant which designates a named object with external linkage.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bill Campbell" <neus@wolfenet.com>
Date: 1997/02/11 Raw View
[ moderator's note: The moderators debated whether to allow this
question in the newsgroup, since it is a "how do I program this
in C++?" kind of question. The answer is a bit subtle and will
probably involve "why?" follow-ups which do belong here. In the
end, I decided to allow it. -sdc
]
if i have a template class declared as:
template<class T, const char* typeName>
class AddType{
public:
AddType(ifstream& in)
....
where typeName is the name of the current instantiation of the class
what is the syntax used to create an instantiation of the class. i've been
trying the following without luck:
ifstream in(int.txt);
const char* name = "Int file";
AddType<int, name> currentAdd(in);
replies to my home email would be appreciated. thank you.
neus@wolfenet.com
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: nj@miller.cs.uwm.edu (Naveen Jamal)
Date: 11 Nov 1994 16:31:45 GMT Raw View
hi...
compiler: g++2.6.0
environment: alpha, DEC OSF/1 V3.0
question:
in class A, i need a method that returns a list of As.
to save work (and thereby realize the OO dream), i figured i should
use a list template to do it. but ofcourse, :<
here is what i have :
----------------------
#include <LEDA/list.h>
class A
{
.
.
.
list <A> createAlist ();
.
.
};
----------------------
(im using the LEDA list template.)
problem:
at compile time i get errors (in list.h) saying class A is an
"incomplete type".
i could just declare createAlist() as a global function,
( it would then become: )
( list <A> createAlist (A &) )
and declare it after Poly _has_ been fully declared, but is there
is a more elegant way....? is this a "bug" with g++2.6.0?
does any compiler handle this kind of situation?
any suggestions ? please email nj@cs.uwm.edu
thanks in advance,
-naveen