This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Namespace and Specialization Trouble moving 2.95.2 code to 3.0.


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]