473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

template parameter friend, it this valid ?

The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1
dies complaining about somthing <unknown>.

Is this valid ?

More to the point, is there any way of doing this that is supported
across all 3 compilers ? I want a template to take a parameter and
make it it's friend.

<code>
struct x
{
template <typename T>
static void X( T t )
{
delete t;
}
};
template <typename T=x>
class Foo
{
template <typename T2>
friend void T::X( T2 t );

~Foo(){}
public:

void FinishMe()
{
T::X( this );
}
};
int main()
{
Foo<> * zz = new Foo<>;

zz->FinishMe();
}
</code>
Jul 22 '05 #1
5 1622

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:48**************************@posting.google.c om...
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1
dies complaining about somthing <unknown>.

Is this valid ?

More to the point, is there any way of doing this that is supported
across all 3 compilers ? I want a template to take a parameter and
make it it's friend.

<code>
struct x
{
template <typename T>
static void X( T t )
{
delete t;
}
};
template <typename T=x>
class Foo
{
template <typename T2>
friend void T::X( T2 t );

~Foo(){}
public:

void FinishMe()
{
T::X( this );
}
};
int main()
{
Foo<> * zz = new Foo<>;

zz->FinishMe();
}
</code>


I personally think MSVC can't do much well with templates. I have MSVC++ 6.0
and it dies when I do various things, such as including a #pragma in a
template definition (C1001: INTERNAL COMPILER ERROR, or compiler crashes,
depending on build settings).

Probably it's legal (I didn't get around to checking) but M$ is too stupid
to get around to fixing it (yes I'm a linux guy).

-- Matt
Jul 22 '05 #2
>
I personally think MSVC can't do much well with templates. I have MSVC++ 6.0 and it dies when I do various things, such as including a #pragma in a
template definition (C1001: INTERNAL COMPILER ERROR, or compiler crashes,
depending on build settings).

Gianni is talking about MSVC 7.1, which is much much better than MSVC 6 at
templates.
Probably it's legal (I didn't get around to checking) but M$ is too stupid
to get around to fixing it (yes I'm a linux guy).


Obviously you're so wrapped up in linux that you are unaware that MS have
'fixed it'. Why don't you try it out, seeing as it's free. I don't know
about #pragma in a template but you'll find most template code is handled
correctly and VC 7.1 is now one of the most standard compliant compilers
around.

john
Jul 22 '05 #3
gi*******@mariani.ws (Gianni Mariani) wrote in message news:<48**************************@posting.google. com>...
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1
dies complaining about somthing <unknown>.

Is this valid ?


I believe that VC 2003 thinks that on that line, T is a type - not a
template parameter. I realize this doesn't help you much ...

Maybe my reasoning is bad, but I determined this after swapping x for
T --

friend void T::X( T2 t ); --> friend void x::X( T2 t );

-------------------------------------------------------
The following code builds fine with that little change:
--------------------------------------------------------

#include "stdafx.h"

struct x
{
template <typename T>
static void X( T t )
{
delete t;
}
};
template <typename T=x>
class Foo
{
template <typename T2>
friend void x::X( T2 t );

~Foo(){}
public:

void FinishMe()
{
T::X( this );
}
};
int _tmain(int argc, char** argv)
{
Foo<> * zz = new Foo<>;

zz->FinishMe();
}

-----------------------------------------------------------------------------
**** in fact, this simpler example breaks VC 2003 with the same error
and the same use of 'T' ****
-----------------------------------------------------------------------------

#include "stdafx.h"

struct x
{
static void X() { }
};

template <typename T>
class Foo
{
friend void T::X(); // <-- error C2063: '<Unknown>' : not a
function
};

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

------------------------------------------------------------------------------
* Using typedef's, one can work around the simpler problem I've
described - but that gets away from templates ... not sure what your
constraints are or if something like

template<class T> class Foo;
typedef void (*XTC)(Foo<x>*);
XTC xtc = x::X<Foo<x>* >;*
...

is even reasonable for you. I'm sure someone else may have a better
idea.
------------------------------------------------------------------------------

#include "stdafx.h"

struct x
{
static void X() { }
};

typedef void (*TX)();
TX tx = x::X;

template <typename T>
class Foo
{
friend void (tx)();
};

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

----------

Hth,

-Luther
Jul 22 '05 #4
Gianni Mariani wrote:

The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1
dies complaining about somthing <unknown>.

Is this valid ?

More to the point, is there any way of doing this that is supported
across all 3 compilers ? I want a template to take a parameter and
make it it's friend.

<code>
struct x
{
template <typename T>
static void X( T t )
{
delete t;
}
};

template <typename T=x>
class Foo
{
template <typename T2>
friend void T::X( T2 t );

~Foo(){}
public:

void FinishMe()
{
T::X( this );
}
};

int main()
{
Foo<> * zz = new Foo<>;

zz->FinishMe();
}
</code>

I couldn't find anything wrong with your example. For the record, it
compiles with gcc 3.3.3 too.

As to a work-around, perhaps an easier job for things like MSVC++ 7.1
would be to handle a non-member function template:

struct x {
};

template <class S, typename T>
void X(T t) {
delete t;
}
template <typename T=x>
class Foo
{
//assuming only this specialisation needs to be a friend:
friend void X<T, Foo*>(Foo*);

~Foo(){}
public:

void FinishMe()
{
X<T, Foo*>(this);
}
};

I think void X(T t) should be able to use whatever definitions of x (via the
template parameter S) it needs in the same way as in your original version.
Another alternative that compiles with gcc as well (not sure about MSVC)
is a template <typename T> struct x.

Denis
Jul 22 '05 #5
Gianni Mariani wrote:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1
dies complaining about somthing <unknown>.

Is this valid ?

More to the point, is there any way of doing this that is supported
across all 3 compilers ? I want a template to take a parameter and
make it it's friend.

.....

Thanks for the responses. It turns out that this is definitly an area
of weakness.

I also have a problem with somthing like:

template <typename T1>
struct A
{
friend class B;

private:
T1 x;
};
struct B
: A<int>
{

using A<int>::x;
};
VC7.1 does not like this.

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gianni Mariani | last post by:
Does anyone know the wisdom behind this ? I want to make a template whose parameter needs to be a friend. What's the right(TM) way to do this ? template <typename T, typename TF> class Boo...
2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
4
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
5
by: John Harrison | last post by:
Why isn't this legal code and is there any other way to achieve the effect I want (i.e. make a template parameter the friend of a template class). template <class X> class I { friend class...
9
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.