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


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

Re: rel_ops issues


Joe Buck <jbuck@racerx.synopsys.com> writes:

[...]

| > Consider what should happen with:
| > 
| > 	#include <stdio.h>
| > 
| > 	namespace Mine
| > 	{
| > 	   template<typename T>
| > 	   bool operator==(const X<T>& a, const X<T>& b)
| > 	   {
| > 	      printf("%p == %p\n", &a, &b);
| > 	      return true;
| > 	   }
| > 
| > 	   template<typename  T>
| > 	   bool operator!=(const X<T>& a, const X<T>& b)
| >            {
| >               printf("%p != %p\n", &a, &b);
| > 	      return false;
| >            }
| > 
| > 	   struct Y : X<int> { };
| > 	}
| 
| So?
| 
| This is not a complete example.

Fair enough

| ...  Please show me where you put the using
| directive,

Inside the function as you were suggesting.

| ... and which code malfunctions, remembering that because of the
| way partial specialization works, bool operator!=(const X<T>& a, const
| X<T>& b) will be chosen over the one in rel_ops (there is no ambiguity).

No. First, there is nothing called function partial specialization.  We
only have function overloading. Second, the example I provided will
involve a conversion so the exact match provided by the general
template will be chosen -- contrary to what you're saying.

	#include <stdio.h>

	namespace standard
	{
	   namespace rel_ops
	   {
	      template<typename T>
              bool operator!=(const T& u, const T& v)
              { return !(u == v); }
           }

           template<typename I>
           void f(I p, I q)
           {
              using rel_ops::operator!=;
              *p == *q;
              *p != *q;
           }
	}

        namespace Mine
        {

          template<typename T> struct X { };

          template<typename T>
          bool operator==(const X<T>& a, const X<T>& b)
          {
             printf("%p == %p\n", &a, &b);
          }

          template<typename  T>
          bool operator!=(const X<T>& a, const X<T>& b)
          {
             printf("%p != %p\n", &a, &b);
          }

          struct Y : X<int> { };
       }

Now the following is intended to show which operator!= is chosen

      int main()
      {
            Mine::Y a, b;
            standard::f(&a, &b);
      }

it will output

    0xbffff3cb == 0xbffff3ca
    0xbffff3cb == 0xbffff3ca


instead of

    0xbffff3cb == 0xbffff3ca
    0xbffff3cb != 0xbffff3ca

-- Gaby


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