Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   New template rules is bad (http://www.velocityreviews.com/forums/t460161-new-template-rules-is-bad.html)

Grizlyk 01-30-2007 10:53 PM

New template rules is bad
 

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



Victor Bazarov 01-30-2007 11:03 PM

Re: New template rules is bad
 
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



Thomas Tutone 01-30-2007 11:04 PM

Re: New template rules is bad
 
On Jan 30, 5:53 pm, "Grizlyk" <grizl...@yandex.ru> 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



red floyd 01-30-2007 11:10 PM

Re: New template rules is bad
 
Victor Bazarov wrote:

> Friendship is overrated.


Oh yeah? Try bumming a beer from your enemies!

:-)

Ian Collins 01-30-2007 11:23 PM

Re: New template rules is bad
 
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.

Kai-Uwe Bux 01-30-2007 11:54 PM

Re: New template rules is bad
 
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



Grizlyk 01-31-2007 09:33 AM

Re: New template rules is bad
 
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 01-31-2007 09:34 AM

Re: New template rules is bad
 
Victor Bazarov wrote:
>
> Friendship is overrated.
>



What does it mean?

--
Maksim A Polyanin



Grizlyk 01-31-2007 09:42 AM

Re: New template rules is bad
 
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



Greg Herlihy 01-31-2007 12:36 PM

Re: New template rules is bad
 



On 1/30/07 2:53 PM, in article epoi7n$gt8$1@aioe.org, "Grizlyk"
<grizlyk1@yandex.ru> 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



All times are GMT. The time now is 11:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57