This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Namespace and Specialization Trouble moving 2.95.2 code to 3.0.
- To: Benjamin Scherrey <scherrey at innoverse dot com>
- Subject: Re: Namespace and Specialization Trouble moving 2.95.2 code to 3.0.
- From: Phil Edwards <pedwards at disaster dot jaj dot com>
- Date: Fri, 10 Aug 2001 16:00:49 -0400
- Cc: gcc at gcc dot gnu dot org
- References: <01081015213806.11754@incognito.innoverse.com>
On Fri, Aug 10, 2001 at 03:21:38PM -0400, Benjamin Scherrey wrote:
> It complains that I'm "specializing 'struct std::less<Armour::VHandle<T> >'
> in different namespace from definition of 'template<class _Tp> struct
> std::less'.
Yeah... this may be cheesy, and your code may actually be valid -- I have
not checked the standard -- but you need to reopen the std namespace in
order for your specialization to show up in the right place. (Maybe that's
just our implementation, I dunno.)
Thus:
-----------------------------------------------------------------------
#include <functional>
#include <iostream> // cerr
#include <algorithm> // sort
using namespace std;
namespace Armour
{
template < typename T > class VHandle
{
public:
int ref() const { std::cerr << "got it\n"; return 42; }
//^^^^^^^
};
}
namespace std
{
// Users are allowed to reopen 'namespace std' specifically to perform
// specializations of standard templates.
template < typename T > struct less< Armour::VHandle<T> >
: binary_function< Armour::VHandle<T>, Armour::VHandle<T>, bool >
{
bool operator()( const Armour::VHandle<T>& a, const Armour::VHandle<T>& b
) const
{
return a.ref() < b.ref();
}
};
}
// test it
int main()
{
typedef Armour::VHandle<int> Blurb;
Blurb ar[4];
sort(ar, ar+4, less<Blurb>());
}
-----------------------------------------------------------------------
With 3.0, this produces:
% g++ -Wall -W spec.cc
% ./a.out
got it
got it
got it
got it
got it
got it
got it
got it
got it
got it
got it
got it
%
Luck++;
Phil
--
Would I had phrases that are not known, utterances that are strange, in
new language that has not been used, free from repetition, not an utterance
which has grown stale, which men of old have spoken.
- anonymous Egyptian scribe, c.1700 BC