C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sort with compare as nested class, legal?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Michael Albert
Guest





PostPosted: Sat Dec 18, 2004 12:33 pm    Post subject: sort with compare as nested class, legal? Reply with quote



Greetings and salutations!

Is the following legal?
---------------------------------sort01.cc----------------------------------
#include <algorithm>
#include <vector>
using namespace std;

int main(int argc, char *argv[])
{
vector<double> v;
struct Sorter{
bool operator()(const double& a, const double& b)
const{ return a<b; }
};

sort(v.begin(), v.end(), Sorter() );
}
--------------------------------------------------------------------------

g++-3.4.2 says (slightly reformated):
sort01.cc:13: error: no matching function for call to
`sort(__gnu_cxx::__normal_iterator std::allocator >, __gnu_cxx::__normal_iterator<double*,
std::vector >, main(int,
char**)::Sorter)'

Thank you!

Sincerely,
Mike Albert


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Max Zelinski
Guest





PostPosted: Sun Dec 19, 2004 1:52 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote



Hello, Michael!
You wrote on 18 Dec 2004 07:33:36 -0500:

MA> Is the following legal?
Short answer: no.

MA> g++-3.4.2 says (slightly reformated):
MA> sort01.cc:13: error: no matching function for call to
MA> `sort(__gnu_cxx::__normal_iterator<double*,
MA> std::vector<double,
MA> std::allocator<double> > >,
MA> __gnu_cxx::__normal_iterator<double*,
MA> std::vector<double, std::allocator >, main(int,
MA> char**)::Sorter)'
I knew that error explanation in g++ is bad, but not THAT BAD!
My Visual Studio 7.1 compiler says:
error C2918: '(int,char *[])main::Sorter' : illegal use of local type in
template instantiation

I think that explain everything.

Quote from the Standart p. 14.3.1., page 241:
"A local type, a type with no linkage, an unnamed type or type compounded from
any of these
type shall not be used as a template-argument for a template type-parameter.
[Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3;
X<S*> x4;
}
-end example]"

MA> Thank you!
You welcome.

With best regards, Max Zelinski.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Branimir Maksimovic
Guest





PostPosted: Sun Dec 19, 2004 1:53 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote




Michael Albert wrote:
Quote:
Greetings and salutations!

Is the following legal?

---------------------------------sort01.cc----------------------------------
#include <algorithm
#include using namespace std;

int main(int argc, char *argv[])
{
vector struct Sorter{
bool operator()(const double& a, const double& b)
const{ return a<b; }
};

sort(v.begin(), v.end(), Sorter() );
}

--------------------------------------------------------------------------

This is not legal as local classes can't be used as
template parameters, though I don't know why.
Perhaps someone will explain that.
But you have workaround for this:

int main()
{
vector struct Sorter{
static bool less(const double& a, const double& b)
{ return a };

sort(v.begin(), v.end(), &Sorter::less );
}

Greetings, Bane.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alberto Barbati
Guest





PostPosted: Sun Dec 19, 2004 1:54 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Michael Albert wrote:
Quote:

Is the following legal?
---------------------------------sort01.cc----------------------------------
#include <algorithm
#include using namespace std;

int main(int argc, char *argv[])
{
vector struct Sorter{
bool operator()(const double& a, const double& b)
const{ return a };

sort(v.begin(), v.end(), Sorter() );
}

Unfortunately, it's illegal. It violates §14.3.1/2: "A local type [...]
shall not be used as a template-argument for a template type-parameter."

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Seungbeom Kim
Guest





PostPosted: Sun Dec 19, 2004 9:57 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Branimir Maksimovic wrote:
Quote:
But you have workaround for this:

int main()
{
vector<double> v;
struct Sorter{
static bool less(const double& a, const double& b)
{ return a };

sort(v.begin(), v.end(), &Sorter::less );
}

You're losing the benefit of the template approach, because with the
function pointer the calls to the function will not be inlined.
I would just declare Sorter outside main(), and call sort with Sorter().

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Sun Dec 19, 2004 9:59 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Michael Albert wrote:

Quote:
Is the following legal?
---------------------------------sort01.cc----------------------------------
#include <algorithm
#include using namespace std;

int main(int argc, char *argv[])
{
vector struct Sorter{
bool operator()(const double& a, const double& b)
const{ return a };

sort(v.begin(), v.end(), Sorter() );
}
--------------------------------------------------------------------------

It's ill-formed. sort() is a function template that deduces its functor
type template parameter, but current standard states that local classes
can not be template parameters.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Doug Harrison
Guest





PostPosted: Sun Dec 19, 2004 10:00 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Michael Albert wrote:

Quote:
Greetings and salutations!

Is the following legal?
---------------------------------sort01.cc----------------------------------
#include <algorithm
#include using namespace std;

int main(int argc, char *argv[])
{
vector struct Sorter{
bool operator()(const double& a, const double& b)
const{ return a<b; }
};

sort(v.begin(), v.end(), Sorter() );
}
--------------------------------------------------------------------------

g++-3.4.2 says (slightly reformated):
sort01.cc:13: error: no matching function for call to
`sort(__gnu_cxx::__normal_iterator std::allocator >, __gnu_cxx::__normal_iterator<double*,
std::vector >, main(int,
char**)::Sorter)'

Thank you!

Template type parameters must have linkage, but local classes have no
linkage, so gcc is right to complain. Boost.lambda can help you here.

--
Doug Harrison
[email]dsh (AT) mvps (DOT) org[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Mon Dec 20, 2004 12:41 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Branimir Maksimovic wrote:
Quote:

Michael Albert wrote:
Greetings and salutations!

Is the following legal?

---------------------------------sort01.cc----------------------------------
#include <algorithm
#include using namespace std;

int main(int argc, char *argv[])
{
vector struct Sorter{
bool operator()(const double& a, const double& b)
const{ return a };

sort(v.begin(), v.end(), Sorter() );
}

--------------------------------------------------------------------------
This is not legal as local classes can't be used as
template parameters, though I don't know why.
Perhaps someone will explain that.
snip


Instantiations of templates and their members must be uniquely
identifiable and distinguishable throughout the program by their
template arguments. For template type arguments, this practically
requires that the types have names that are unique throughout the
program; this property is called "external linkage". Local classes do
not have such unique names; they have "internal linkage".

Note however that the standard requires anonymous namespaces to be
treated as if they have automatically-assigned unique names, allowing
their members to have external linkage. This could have been required
of local classes and I don't know why it wasn't.

--
Ben Hutchings
Kids! Bringing about Armageddon can be dangerous. Do not attempt it in
your own home. - Terry Pratchett and Neil Gaiman, `Good Omens'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sharad Kala
Guest





PostPosted: Mon Dec 20, 2004 10:42 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote


"Ben Hutchings" <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote in message

Quote:
Local classes do
not have such unique names; they have "internal linkage".

I think they have no linkage. As you must be already knowing that there is a
difference -

(3.5/2)
- When a name has internal linkage, the entity it denotes can be referred to
by names from other scopes in the same translation unit.
- When a name has no linkage, the entity it denotes cannot be referred to by
names from other scopes.

Sharad




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Tue Dec 21, 2004 10:26 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Sharad Kala wrote:
Quote:

"Ben Hutchings" <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote in message

Local classes do
not have such unique names; they have "internal linkage".

I think they have no linkage.
snip


You're right. I remembered this after I posted.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Dec 21, 2004 10:35 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Ben Hutchings wrote:
Quote:
Branimir Maksimovic wrote:

[...]
Quote:
Instantiations of templates and their members must be uniquely
identifiable and distinguishable throughout the program by their
template arguments. For template type arguments, this practically
requires that the types have names that are unique throughout the
program; this property is called "external linkage". Local classes
do
not have such unique names; they have "internal linkage".

Note however that the standard requires anonymous namespaces to be
treated as if they have automatically-assigned unique names, allowing
their members to have external linkage. This could have been
required
of local classes and I don't know why it wasn't.

Probably because the rules for linkage of local class definitions were
written long before anonymous namespaces (or even namespaces in
general)
existed.

As far as I know, there would be no implementation problem allowing
this
for normal functions. The anonymous namespace wouldn't suffice in
itself of course, since you can have two local classes with the same
name in different blocks. But it wouldn't be two difficult to mangle
in
the number of the block.

I imagine that things class declarations like inline functions or
template functions would be a bit more difficult. But still doable.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Dec 22, 2004 8:59 am    Post subject: Re: sort with compare as nested class, legal? Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
Ben Hutchings wrote:
Branimir Maksimovic wrote:

[...]
Instantiations of templates and their members must be uniquely
identifiable and distinguishable throughout the program by their
template arguments. For template type arguments, this practically
requires that the types have names that are unique throughout the
program; this property is called "external linkage". Local classes
do not have such unique names; they have "internal linkage".

Note however that the standard requires anonymous namespaces to be
treated as if they have automatically-assigned unique names, allowing
their members to have external linkage. This could have been
required of local classes and I don't know why it wasn't.

Probably because the rules for linkage of local class definitions were
written long before anonymous namespaces (or even namespaces in
general) existed.

As far as I know, there would be no implementation problem allowing
this for normal functions. The anonymous namespace wouldn't suffice
in itself of course, since you can have two local classes with the
same name in different blocks. But it wouldn't be two difficult to
mangle in the number of the block.

As I understand it, an anonymous namespace is treated like a namespace
with a reserved name that is specific to the translation unit (this is
even implied in the standard), e.g. when compiling /source/foo/bar.cpp
an implementation could treat the anonymous namespace as if it is
called _ANS_2Fsource_2Ffoo_2Fbar_2Ecpp. Local classes could be
treated for purposes of name mangling as if they belong to a namespace
with a reserved name that is specific to the function, e.g. for the
4th function in /source/foo/bar.cpp that has a local class, an
implementation could use _ANS_2Fsource_2Ffoo_2Fbar_2Ecpp__4 (or, where
the function has external linkage, the function's mangled name could
perhaps be used instead of a mangled namespace name).

Quote:
I imagine that things class declarations like inline functions or
template functions would be a bit more difficult. But still doable.

Why would those be a special problem? Their mangled names already
need to include the mangled class name, and we know how to make that
unique.

--
Ben Hutchings
I'm not a reverse psychological virus. Please don't copy me into your sig.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Dec 23, 2004 9:42 pm    Post subject: Re: sort with compare as nested class, legal? Reply with quote

Ben Hutchings wrote:
Quote:
kanze (AT) gabi-soft (DOT) fr wrote:
Ben Hutchings wrote:
Branimir Maksimovic wrote:

[...]
Instantiations of templates and their members must be uniquely
identifiable and distinguishable throughout the program by their
template arguments. For template type arguments, this practically
requires that the types have names that are unique throughout the
program; this property is called "external linkage". Local
classes
do not have such unique names; they have "internal linkage".

Note however that the standard requires anonymous namespaces to be
treated as if they have automatically-assigned unique names,
allowing their members to have external linkage. This could have
been required of local classes and I don't know why it wasn't.

Probably because the rules for linkage of local class definitions
were written long before anonymous namespaces (or even namespaces
in
general) existed.

As far as I know, there would be no implementation problem allowing
this for normal functions. The anonymous namespace wouldn't
suffice
in itself of course, since you can have two local classes with the
same name in different blocks. But it wouldn't be two difficult to
mangle in the number of the block.

As I understand it, an anonymous namespace is treated like a
namespace
with a reserved name that is specific to the translation unit (this
is
even implied in the standard), e.g. when compiling
/source/foo/bar.cpp
an implementation could treat the anonymous namespace as if it is
called _ANS_2Fsource_2Ffoo_2Fbar_2Ecpp. Local classes could be
treated for purposes of name mangling as if they belong to a
namespace
with a reserved name that is specific to the function, e.g. for the
4th function in /source/foo/bar.cpp that has a local class, an
implementation could use _ANS_2Fsource_2Ffoo_2Fbar_2Ecpp__4 (or,
where
the function has external linkage, the function's mangled name could
perhaps be used instead of a mangled namespace name).

That's why I said that the anonymous namespace wouldn't suffice *in*
*itself*. If nothing else, you mangle in the number of opening brace
symbols which proceded the definition in the source file. Or whatever.
I trust our implementors to find something:-).

Quote:
I imagine that things class declarations like inline functions or
template functions would be a bit more difficult. But still
doable.


Quote:
Why would those be a special problem? Their mangled names already
need to include the mangled class name, and we know how to make that
unique.

For the love of me, I can't remember what I was thinking of either.
(My
sentence looks a bit mangled, too.) A priori, if we are dealing with
something not in anonymous namespace, we have a global mangling,
starting with enclosing namespaces and type. For things in anonymous
namespace, we start with its synthesized name. And for something
defined in a static function in global namespace, we use the name of
the
file (or the complete pathname?). After that, we mangle in all nested
namespaces, then the class name, the function name (including the
instantiation parameters if it is a template function), the block
number
in the function, and finally the block number in the function. And of
course, the name of the class itself. Without any compression, that
could make for some pretty long names, but that's not my problem: I
shouldn't have to look at them:-).

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.