mixing bound and unbound friends

C

Christof Warlich

Hi,

I need to make a templated class A a friend of class B.
My problem: I think I know how to do this for both

1) _one_ specific template instantiation of class A, e.g.:

template<typename Type, unsigned int x> class A {
public:
void useSomeFunctionalityOfB(void);
};
class B {
friend class A<float, 7>;
};

2) _any_ instantiation of class A, e.g.:

template<typename Type, unsigned int x> class A {
public:
void useSomeFunctionalityOfB(void);
};
class B {
template<typename Type, unsigned int x> friend class A;
};

But I do _not_ know how to mix the two extremes, e.g.
how to make class A a friend of class B only for Type float,
but for any unsigned int x.

Is it possible to express this at all, and if so, how?

Thanks for any help,

Christof
 
O

Ondra Holub

Hi,

I need to make a templated class A a friend of class B.
My problem: I think I know how to do this for both

1) _one_ specific template instantiation of class A, e.g.:

template<typename Type, unsigned int x> class A {
public:
void useSomeFunctionalityOfB(void);};

class B {
friend class A<float, 7>;

};

2) _any_ instantiation of class A, e.g.:

template<typename Type, unsigned int x> class A {
public:
void useSomeFunctionalityOfB(void);};

class B {
template<typename Type, unsigned int x> friend class A;

};

But I do _not_ know how to mix the two extremes, e.g.
how to make class A a friend of class B only for Type float,
but for any unsigned int x.

Is it possible to express this at all, and if so, how?

Thanks for any help,

Christof

Hi Christof.

You can define intermediate access class which is templated only with
one parameter:

#include <iostream>

template<typename T> struct AccessB;

class B {
friend struct AccessB<float>; // Make only AccessB<float> friend

private:
static void MethodOfB() { std::cout << "B::MethodOfB()\n"; }
};

template<typename T> struct AccessB {
static void useSomeFunctionalityOfB()
{
B::MethodOfB();
}
};

template<typename Type, unsigned int x> class A {
public:
A() { }

void useSomeFunctionalityOfB()
{
// This will compile only when Type is float
AccessB<Type>::useSomeFunctionalityOfB();
}
};

int main()
{
// This is OK
A<float, 10> af10;
af10.useSomeFunctionalityOfB();

// The following will not compile
// A<double, 10> ad10;
// ad10.useSomeFunctionalityOfB();
}

If you uncomment last 2 lines in main(), you will get compile error,
because you will be trying to access private member of B. It is a bit
obscure, but maybe it could help you.
 
O

Ondra Holub

Hi Christof.

You can define intermediate access class which is templated only with
one parameter

However this way you will provide a "backdoor" to class B - anyone may
use AccessB to access private method of B.
 
C

Christof Warlich

Ondra said:
However this way you will provide a "backdoor" to class B - anyone may
use AccessB to access private method of B.
Hi Ondra,

thanks, good idea. However, quite some effort to just restrict access
to its minimum. Thus, if there is no direct way to express this, I'll
probably just go the unbound friend approach, accepting that access is
not as much restricted as it could.

Cheers,

Christof
 
O

Ondra Holub

I have another idea:

You can define templated method in B, which takes as parameter Type:

class B
{
template<typename T>
static void Method()
{
// Here should be some static assertion
// ...
}
};

Specialize Method() only for template parameter float:

template<>
void AAA::Method<float>()
{
// Do what you need
// ...
}

For all types but float it should stop compilation on static assertion
(which has to be searched on web, I do not remember, how is it made).
But for float there is another specialization, so it will not stop on
static assert.
 

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,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top