Topic: Defect report: errors in 14.6.5 example


Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Wed, 30 Oct 2002 10:13:25 +0000 (UTC)
Raw View
Aleksey Gurtovoy wrote:
> Proposed resolution: change the example to read:
>
>     template<typename T> class number {
>     public:
>         number(int);
>         //...
>         friend number gcd(number& x, number& y);

 I also suggest to use const references ;)

>         //...
>     };
>
>     void g()
>     {
>         number<double> a(3), b(4);
>         //...
>         a = gcd(a,b);   // finds gcd because number<double> is an
>                         // associated class, making gcd visible
>                         // in its namespace (global scope)
>         b = gcd(3,4);   // ill-formed; gcd is not visible

                          // ill-formed: can't bind number(3) to number&

>     }
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.net

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: agurtovoy@meta-comm.com (Aleksey Gurtovoy)
Date: 27 Oct 02 12:11:34 GMT
Raw View
 [Moderator's note: this defect report has been
 forwarded to the C++ committee. -moderator.]

The example in 14.6.5 [temp.inject] paragraph 2 is incorrect:

    template<typename T> class number {
        number(int);
        //...
        friend number gcd(number& x, number& y) { /* ... */ }
        //...
    };

    void g()
    {
        number<double> a(3), b(4);
        //...
        a = gcd(a,b);   // finds gcd because number<double> is an
                        // associated class, making gcd visible
                        // in its namespace (global scope)
        b = gcd(3,4);   // ill-formed; gcd is not visible
    }

Regardless of the last statement ("b = gcd(3,4);"), the above code is
ill-formed:

  a) number's constructor is private;
  b) the definition of (non-void) friend 'gcd' function does not
contain a return statement.

Proposed resolution: change the example to read:

    template<typename T> class number {
    public:
        number(int);
        //...
        friend number gcd(number& x, number& y);
        //...
    };

    void g()
    {
        number<double> a(3), b(4);
        //...
        a = gcd(a,b);   // finds gcd because number<double> is an
                        // associated class, making gcd visible
                        // in its namespace (global scope)
        b = gcd(3,4);   // ill-formed; gcd is not visible
    }


Aleksey
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]