473,804 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templated friends

I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
...
}

That seems to be what TC++PL SE shows in C.13.2, but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see declaration of
'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?

Thanks!
- Pete
Jul 22 '05 #1
5 5646
"Pete C." <x@x.x> wrote...
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
The definition of this function has no 'Op' used. There is no
way for a compiler to figure out the template argument from the
function arguments. That's why you have to always explicitly
specify it.
...
}

That seems to be what TC++PL SE shows in C.13.2,
I don't have my copy of the SE here (it's at work), are you sure
the example is precisely as you show here?
but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see declaration of 'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?


Perhaps you're not following the rules of syntax. Why do you
think what you wrote should work? Where did you see the syntax
you're using?

'func' is a function _template_. If you want a _template_ to be
a friend (which actually means that _all_possible_i nstantiations_
of that template are friends), you need to begin your declaration
with the keyword "template". If you want one _particular_
instance of that template to be a friend, you need to give the
template _argument_ (put something in the <>).

Of course it's entirely possible that you're not using the right
compiler for the code. Older VC++ can't handle template friends
very well.

Victor
Jul 22 '05 #2
Ian
Pete C. wrote:
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
...
}

That seems to be what TC++PL SE shows in C.13.2, but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see declaration of
'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?

Each instance of a template is unique. So (unfortunately) you have to
be explicit;

class cls;

template< class Op>
cls func(const cls& a, const cls& b)
{
....
}

class cls
{
friend cls func<int> (const cls& a, const cls& b);
};

Ian
Jul 22 '05 #3
Victor Bazarov wrote:
"Pete C." <x@x.x> wrote...
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
The definition of this function has no 'Op' used. There is no
way for a compiler to figure out the template argument from the
function arguments. That's why you have to always explicitly
specify it.
...
}

That seems to be what TC++PL SE shows in C.13.2,


I don't have my copy of the SE here (it's at work), are you sure
the example is precisely as you show here?


template<class T> class Matrix;

template<class T> class Vector {
T v[4];
public:
friend Vector operator*<>(con st Matrix<T>&, const Vector&);
// ...
};

template<class T> class Matrix {
Vector<T> v[4];
public:
friend Vector<T> operator*<>(con st Matrix&, const Vector<T>&);
// ...
};

template<class T> Vector<T> operator*(const Matrix<T>& m, const Vector<T>&
v)
{
// ...
}
but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see
declaration of 'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything
to do with friend.

What am I doing wrong?
Perhaps you're not following the rules of syntax. Why do you
think what you wrote should work? Where did you see the syntax
you're using?

'func' is a function _template_. If you want a _template_ to be
a friend (which actually means that _all_possible_i nstantiations_
of that template are friends), you need to begin your declaration
with the keyword "template". If you want one _particular_
instance of that template to be a friend, you need to give the
template _argument_ (put something in the <>).


I changed it to:
template< template<class> class Op> friend variant binaryoperator (const
variant& a, const variant& b);
and it worked!

I had tried it with 'friend' before 'template<...>' , but that failed. Then I
looked at TC++PL and its complete lack of 'template<...>' made me think that
it was unneccessary.

Thanks again!

Of course it's entirely possible that you're not using the right
compiler for the code. Older VC++ can't handle template friends
very well.
It's VC++ 7.1, so I can pretty much blame any errors on myself ;)

- Pete

Victor

Jul 22 '05 #4
Ian wrote:
Pete C. wrote:
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
...
}

That seems to be what TC++PL SE shows in C.13.2, but the compiler
seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see
declaration of
'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?

Each instance of a template is unique. So (unfortunately) you have to
be explicit;


You can make a template friend. friends are specified as declarations,
so if you can declare it, you can make a friend out of it (in theory).

class cls;

template< class Op>
cls func(const cls& a, const cls& b);

class cls
{
template< class Op>
friend cls func(const cls& a, const cls& b);
};
.... now, that's not a very interesting template, maybe the OP wanted
somthing like this:

class cls;

template<class TA, class TB>
cls func(const TA& a, const TB& b);

class cls
{
template<class TA, class TB>
friend cls func(const TA& a, const TB& b);
};
Jul 22 '05 #5
Gianni Mariani wrote:
<snip>

You can make a template friend. friends are specified as
declarations, so if you can declare it, you can make a friend out of
it (in theory).

class cls;

template< class Op>
cls func(const cls& a, const cls& b);

class cls
{
template< class Op>
friend cls func(const cls& a, const cls& b);
};

I had tried:
friend template<...> cls func ... ;

Then TC++PL confused me with the syntax as shown in the topic post, but with
a nudge from Victor Bazarov I came the correct syntax. :)

... now, that's not a very interesting template, maybe the OP wanted
somthing like this:

class cls;

template<class TA, class TB>
cls func(const TA& a, const TB& b);

class cls
{
template<class TA, class TB>
friend cls func(const TA& a, const TB& b);
};


I actually had: template<templa te<class> class Op>
To pass a template sort of like this:
cls = func<std::plus> (cls(123), cls(321))

- Pete
Jul 22 '05 #6

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

Similar topics

3
6578
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? Here's what I'm generally trying to achieve: I'm building (trying to anyway) a serialization library. Here's my design:
1
1872
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class my_template
2
1918
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
4
1869
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
6
1316
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated columns work find as long as I pass in a dataset but if I pass in a collection of objects then they won't work unless I can cast the correct object and then fill in the column with the object. My current code looks like this: public void...
2
1690
by: mattjgalloway | last post by:
I'm having some problems with a templated member function of a templated class. Unfortunately I can't replicate it with a simple example so I know something odd must be going on!!! Basically it's like this... I have a few classes all templated by one type. Inside one of the classes is a templated member function. When I try to use this templated function in another class, the compiler complains with: error: expected primary-expression...
7
1731
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
2
2939
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
2
2291
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include <stack> template <class T> class A { public:
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.