Topic: local class in template function?
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/08/30 Raw View
Is it possible to use a local class in a template function? It's
pretty convenient for defining simple functors. My compiler
doesn't like it, though. It says that local structs can't be
used in template functions. Eg.
void action(const std::list<Type>& L, const std::string& something)
{
struct Function
{
std::string s;
Function(const std::string&);
bool operator()(const Type&);
};
find_if(L.begin(),L.end(),Function(something));
}
If I move the definition of Function to namespace or global scope,
no problem.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/08/30 Raw View
In article <slrn6uh5tl.1cn.sbnaran@localhost.localdomain>, Siemel Naran
<sbnaran@localhost.localdomain> writes
>Is it possible to use a local class in a template function? It's
>pretty convenient for defining simple functors. My compiler
>doesn't like it, though. It says that local structs can't be
>used in template functions. Eg.
If I remember correctly, the problem is that instantiations of templates
belong in the scope where the template definition is found. Your local
types (class, enum etc.) will not be in the right scope. In general
template type arguments need extern linkage.
I think that this is one of the things that seems overly restrictive to
many programmers but implementors would find allowing local types as
template arguments difficult if not impossible.
--
Francis Glassborow
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "John M. Dlugosz" <john@dlugosz.com>
Date: 1998/08/31 Raw View
Siemel Naran wrote in message ...
>Is it possible to use a local class in a template function? It's
>pretty convenient for defining simple functors. My compiler
>doesn't like it, though. It says that local structs can't be
>used in template functions. Eg.
The spec specifically says you can't do that:
"A local type, a type with no linkage or an unnamed type shall not be
used as a template-argument for a template type-parameter"
Other replies give the reasons why.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/01 Raw View
John M. Dlugosz wrote:
>
> Siemel Naran wrote in message ...
> >Is it possible to use a local class in a template function? It's
> >pretty convenient for defining simple functors. My compiler
> >doesn't like it, though. It says that local structs can't be
> >used in template functions. Eg.
>
> The spec specifically says you can't do that:
>
> "A local type, a type with no linkage or an unnamed type shall not be
> used as a template-argument for a template type-parameter"
>
> Other replies give the reasons why.
His example did not involve using the local type as a template argument,
nor as a template type-parameter. It did involve defining a local type
inside a template function. That may be prohibited, but I'm not sure
where.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jason Merrill <jason@cygnus.com>
Date: 1998/09/01 Raw View
>>>>> James Kuyper <kuyper@wizard.net> writes:
> John M. Dlugosz wrote:
>>
>> "A local type, a type with no linkage or an unnamed type shall not be
>> used as a template-argument for a template type-parameter"
> His example did not involve using the local type as a template argument,
> nor as a template type-parameter.
Actually, it did:
>>> void action(const std::list<Type>& L, const std::string& something)
>>> {
>>> struct Function
>>> {
>>> std::string s;
>>> Function(const std::string&);
>>> bool operator()(const Type&);
>>> };
>>>
>>>
>>> find_if(L.begin(),L.end(),Function(something));
>>> }
It's a bit subtle, but after you do template arg deduction for the call to
find_if, you end up trying to instantiate it with the local class Function.
Jason
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/09/01 Raw View
Francis Glassborow wrote:
> In article <slrn6uh5tl.1cn.sbnaran@localhost.localdomain>, Siemel Naran
> <sbnaran@localhost.localdomain> writes
> >Is it possible to use a local class in a template function? It's
> >pretty convenient for defining simple functors. My compiler
> >doesn't like it, though. It says that local structs can't be
> >used in template functions. Eg.
> If I remember correctly, the problem is that instantiations of templates
> belong in the scope where the template definition is found. Your local
> types (class, enum etc.) will not be in the right scope. In general
> template type arguments need extern linkage.
> I think that this is one of the things that seems overly restrictive to
> many programmers but implementors would find allowing local types as
> template arguments difficult if not impossible.
Well, it can't be impossible, since I read in the "egcs-1.1 C++
Features"
web page (http://egcs.cygnus.com/egcs-1.1/c++features.html):
+ local classes in templates are supported.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/09/01 Raw View
James Kuyper <kuyper@wizard.net> writes:
> John M. Dlugosz wrote:
>> "A local type, a type with no linkage or an unnamed type shall not be
>> used as a template-argument for a template type-parameter"
> His example did not involve using the local type as a template argument,
Yes, it did. find_if is a template function, so it cannot be
specialized for a local type.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jason Merrill <jason@cygnus.com>
Date: 1998/09/01 Raw View
>>>>> Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> Francis Glassborow wrote:
>> I think that this is one of the things that seems overly restrictive to
>> many programmers but implementors would find allowing local types as
>> template arguments difficult if not impossible.
> Well, it can't be impossible, since I read in the "egcs-1.1 C++
> Features"
> web page (http://egcs.cygnus.com/egcs-1.1/c++features.html):
> + local classes in templates are supported.
That note doesn't mean allowing them as template arguments, just allowing
them within template functions.
Jason
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/09/01 Raw View
On 1 Sep 1998 05:58:06 GMT, Jason Merrill <jason@cygnus.com> wrote:
>It's a bit subtle, but after you do template arg deduction for the call to
>find_if, you end up trying to instantiate it with the local class Function.
Let's see if I understand this.
A local struct has internal linkage, meaning that that no function
other than the function that defines it can access it.
In general, functions are non-inline -- inlining is only an
optimization -- so the compiler treats find_if as if it were
non-inline. Hence it does not (yet) expand the inline function where
it is used. That is,
iterator pos=find_if(d_list.begin(),d_list.end(),LocalStruct());
does not yet become
iterator end=d_list.end();
LocalStruct L;
for (iterator iter=d_list.begin(); iter!=end; ++iter)
if (L(*iter)) break;
iterator pos=iter;
The compiler supposes that the template will be instantiated at
link time, as this is the most general policy. However, it can't
possibly instantiate the template at link time as the definition
of struct LocalStruct is inaccessible.
Therefore, using a local arg is a template function is not allowed.
(If we expand the template manually, then there should be no
problem. There is no template to instantiate at link time.)
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hiram Berry <burningb@burningbridges.com>
Date: 1998/09/02 Raw View
John M. Dlugosz wrote:
>
> Siemel Naran wrote in message ...
> >Is it possible to use a local class in a template function? It's
> >pretty convenient for defining simple functors. My compiler
> >doesn't like it, though. It says that local structs can't be
> >used in template functions. Eg.
>
> The spec specifically says you can't do that:
>
> "A local type, a type with no linkage or an unnamed type shall not be
> used as a template-argument for a template type-parameter"
Does "local type" mean local classes of a function only? That would
imply that in the original example, a slight rewrite of action() from
function to functor would make the expression legal:
class ActionType{
struct Function{
std::string s;
Function(const std::string&);
bool operator()(const Type&);
};
public:
ActionType(){}
void operator()(const std::list<Type>& L, const std::string&
something){
find_if(L.begin(),L.end(),Function(something));
}
} action;
Is that right, or am I missing something else here? It seems strange
that the language would allow an action to be performed through
overloaded operator() but not via the corresponding function.
Hiram Berry
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/02 Raw View
Jason Merrill wrote:
>
> >>>>> James Kuyper <kuyper@wizard.net> writes:
>
> > John M. Dlugosz wrote:
> >>
> >> "A local type, a type with no linkage or an unnamed type shall not be
> >> used as a template-argument for a template type-parameter"
>
> > His example did not involve using the local type as a template argument,
> > nor as a template type-parameter.
>
> Actually, it did:
>
> >>> void action(const std::list<Type>& L, const std::string& something)
> >>> {
> >>> struct Function
> >>> {
> >>> std::string s;
> >>> Function(const std::string&);
> >>> bool operator()(const Type&);
> >>> };
> >>>
> >>>
> >>> find_if(L.begin(),L.end(),Function(something));
> >>> }
>
> It's a bit subtle, but after you do template arg deduction for the call to
> find_if, you end up trying to instantiate it with the local class Function.
Sorry, you're right, and it's not all that subtle :-(.
So the answer to Siemel Naran's original question is "yes, you can use a
local class in a template function". However, you can't instantiate
another template function using that local class.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/09/02 Raw View
Jason Merrill wrote:
> >>>>> Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> > Francis Glassborow wrote:
> >> I think that this is one of the things that seems overly restrictive to
> >> many programmers but implementors would find allowing local types as
> >> template arguments difficult if not impossible.
> > Well, it can't be impossible, since I read in the "egcs-1.1 C++
> > Features"
> > web page (http://egcs.cygnus.com/egcs-1.1/c++features.html):
> > + local classes in templates are supported.
> That note doesn't mean allowing them as template arguments, just allowing
> them within template functions.
But local classes in template functions seem to work already in g++
2.7.2.1,
so that wouldn't be a new feature.
Are you shure that is the intended meaning?
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jason Merrill <jason@cygnus.com>
Date: 1998/09/03 Raw View
>>>>> Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> Jason Merrill wrote:
>> >>>>> Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
>> > + local classes in templates are supported.
>> That note doesn't mean allowing them as template arguments, just allowing
>> them within template functions.
> But local classes in template functions seem to work already in g++
> 2.7.2.1, so that wouldn't be a new feature. Are you shure that is the
> intended meaning?
Since I wrote the code in question, I'm quite sure. I guess that note was
unnecessary.
Jason
g++ maintainer
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/09/04 Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> Francis Glassborow wrote:
>> I think that this is one of the things that seems overly restrictive to
>> many programmers but implementors would find allowing local types as
>> template arguments difficult if not impossible.
> Well, it can't be impossible, since I read in the "egcs-1.1 C++
> Features"
> web page (http://egcs.cygnus.com/egcs-1.1/c++features.html):
> + local classes in templates are supported.
This is a completely different animal. egcs 1.0 used to crash when
presented code such as:
template <typename T> void foo() {
struct local {}; // crash, class local to template function
}
But it will correctly reject code such as:
#include <vector>
void foo() {
struct T {}; // local class
std::vector<T>(); // invalid: template of local class
}
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]