New template rules is bad

G

Grizlyk

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
};
 
V

Victor Bazarov

Grizlyk said:
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
 
T

Thomas Tutone

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
 
I

Ian Collins

Grizlyk said:
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.
 
K

Kai-Uwe Bux

Grizlyk said:
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
 
G

Grizlyk

Thomas said:
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).
 
G

Grizlyk

Kai-Uwe Bux said:
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
};
 
G

Greg Herlihy

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
 
G

Grizlyk

Victor said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top