New template rules is bad

Discussion in 'C++' started by Grizlyk, Jan 30, 2007.

  1. Grizlyk

    Grizlyk Guest

    New C++ rules are always trying to do "fast" compilation - to compile code,
    which must be compiled at point of instance only (not during declaration
    stage). The behaviour is intoducing many wrong limitations.

    Look at this strange limitation:

    1. Can not declare templated friend

    template< class T>
    class X
    {
    friend class T;
    //error: using template type parameter 'T' after 'class'
    //error: friend declaration does not name a class or function
    };

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 30, 2007
    #1
    1. Advertising

  2. Grizlyk wrote:
    > New C++ rules are always trying to do "fast" compilation - to compile
    > code, which must be compiled at point of instance only (not during
    > declaration stage). The behaviour is intoducing many wrong
    > limitations.
    > Look at this strange limitation:
    >
    > 1. Can not declare templated friend
    >
    > template< class T>
    > class X
    > {
    > friend class T;
    > //error: using template type parameter 'T' after 'class'
    > //error: friend declaration does not name a class or function
    > };


    Friendship is overrated.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 30, 2007
    #2
    1. Advertising

  3. On Jan 30, 5:53 pm, "Grizlyk" <> wrote:

    > New C++ rules are always trying to do "fast" compilation - to compile code,
    > which must be compiled at point of instance only (not during declaration
    > stage). The behaviour is intoducing many wrong limitations.
    >
    > Look at this strange limitation:
    >
    > 1. Can not declare templated friend
    >
    > template< class T>
    > class X
    > {
    > friend class T;
    > //error: using template type parameter 'T' after 'class'
    > //error: friend declaration does not name a class or function
    >
    > };


    Why do you call these "new" template rules? As far as I know, this
    has been the standard behavior since the standard was adopted almost
    ten years ago.

    Best regards,

    Tom
     
    Thomas Tutone, Jan 30, 2007
    #3
  4. Grizlyk

    red floyd Guest

    Victor Bazarov wrote:

    > Friendship is overrated.


    Oh yeah? Try bumming a beer from your enemies!

    :)
     
    red floyd, Jan 30, 2007
    #4
  5. Grizlyk

    Ian Collins Guest

    Grizlyk wrote:
    > New C++ rules are always trying to do "fast" compilation - to compile code,
    > which must be compiled at point of instance only (not during declaration
    > stage). The behaviour is intoducing many wrong limitations.
    >
    > Look at this strange limitation:
    >
    > 1. Can not declare templated friend
    >
    > template< class T>
    > class X
    > {
    > friend class T;
    > //error: using template type parameter 'T' after 'class'
    > //error: friend declaration does not name a class or function
    > };
    >

    I'd always put this down to gcc compiling a template when it shouldn't.
    But I can't find the relevant section of the standard that says how a
    compiler should treat an uninstantiated template.

    --
    Ian Collins.
     
    Ian Collins, Jan 30, 2007
    #5
  6. Grizlyk

    Kai-Uwe Bux Guest

    Grizlyk wrote:

    >
    > New C++ rules are always trying to do "fast" compilation - to compile
    > code, which must be compiled at point of instance only (not during
    > declaration stage). The behaviour is intoducing many wrong limitations.
    >
    > Look at this strange limitation:
    >
    > 1. Can not declare templated friend
    >
    > template< class T>
    > class X
    > {
    > friend class T;
    > //error: using template type parameter 'T' after 'class'
    > //error: friend declaration does not name a class or function
    > };


    What you encounter here is not some general rule about template
    instantiation. It is in fact a consequence of [7.1.5.3/2] and the syntax
    specification that a friend declaration needs an elaborate-type-specifier.
    In my opinion, these rules could be relaxed without negatively affecting
    the complexity of compilation. In fact, due to a bug, g++ accepts the
    following invalid code, which circumvents the provision:


    template < typename T >
    class identity {
    public:

    typedef T me;

    };

    template < typename T >
    class my_friend {
    private:

    friend class identity< T >::me;

    char x;

    };

    class The_T {
    public:

    static
    char & peek_friend ( my_friend< The_T > & f ) {
    return( f.x );
    }

    };

    int main (void) {
    my_friend< The_T > x;
    The_T::peek_friend( x );
    }

    (See also: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21498)


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 30, 2007
    #6
  7. Grizlyk

    Grizlyk Guest

    Thomas Tutone wrote:
    >
    > Why do you call these "new" template rules? As far as I know, this
    > has been the standard behavior since the standard was adopted almost
    > ten years ago.


    Because it can be easy compiled by bcc32 (version 5.5.1)
    and probably by g++ (version 2.95).

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 31, 2007
    #7
  8. Grizlyk

    Grizlyk Guest

    Victor Bazarov wrote:
    >
    > Friendship is overrated.
    >



    What does it mean?

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 31, 2007
    #8
  9. Grizlyk

    Grizlyk Guest

    Kai-Uwe Bux wrote:
    >
    > What you encounter here is not some general rule about template
    > instantiation. It is in fact a consequence of [7.1.5.3/2] and the syntax
    > specification that a friend declaration needs an elaborate-type-specifier.
    > In my opinion, these rules could be relaxed without negatively affecting
    > the complexity of compilation. In fact, due to a bug, g++ accepts the
    > following invalid code, which circumvents the provision:


    As i can understand, it is even not extention, just error, so can not be
    used.

    I think, I will make dummy classes

    template<class T>class dummy;

    template<class T>
    class X
    {
    friend class dummy<T>;
    };

    template<class T>
    class dummy
    {
    //pass all X::private into dummy::public
    };

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 31, 2007
    #9
  10. Grizlyk

    Greg Herlihy Guest

    On 1/30/07 2:53 PM, in article epoi7n$gt8$, "Grizlyk"
    <> wrote:

    >
    > New C++ rules are always trying to do "fast" compilation - to compile code,
    > which must be compiled at point of instance only (not during declaration
    > stage). The behaviour is intoducing many wrong limitations.
    >
    > Look at this strange limitation:
    >
    > 1. Can not declare templated friend
    >
    > template< class T>
    > class X
    > {
    > friend class T;
    > //error: using template type parameter 'T' after 'class'
    > //error: friend declaration does not name a class or function
    > }


    You have it backwards. The new rules for friend declarations (specifically,
    "extended friend declarations") make the friend T declaration above legal in
    the next C++ Standard.

    Most C++ compilers have not yet been updated to recognize this kind of
    friend declaration (though I believe VC++ 2005 is an exception). So until
    your C++ compiler adds support for extended friend declarations, your C++
    program will be governed by the "old rules", and not by the new ones.

    Greg
     
    Greg Herlihy, Jan 31, 2007
    #10
  11. Grizlyk wrote:
    > Victor Bazarov wrote:
    >>
    >> Friendship is overrated.
    >>

    >
    >
    > What does it mean?


    It means that in many cases whatever you need 'friend' for
    can be accomplished without it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 31, 2007
    #11
  12. Grizlyk

    Grizlyk Guest

    Victor Bazarov wrote:
    >>>
    >>> Friendship is overrated.

    >>
    >> What does it mean?

    >
    > It means that in many cases whatever you need 'friend' for
    > can be accomplished without it.


    If keyword "friend" is exist, language must support it completely, because
    inheritance must not be only design way.

    Composition for example often requires protected interface for non-inherited
    classes, that can be easy implemented with friend.

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 31, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    886
  2. Eric Anderson

    Bad Transform or Bad Engine?

    Eric Anderson, Oct 4, 2005, in forum: XML
    Replies:
    1
    Views:
    378
    Peter Flynn
    Oct 5, 2005
  3. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    604
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
  4. Gene

    Re: Bad code or bad compiler?

    Gene, Jun 1, 2008, in forum: C Programming
    Replies:
    3
    Views:
    282
    Barry Schwarz
    Jun 1, 2008
  5. rantingrick
    Replies:
    44
    Views:
    1,226
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page