By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Templated friend function can't access private members?

P: n/a
Hi folks... I've been sifting through archived postings on this, but
haven't quite found an answer yet. I've got a templated stand-alone
function:

template<typename Tvoid access(const T & t) { ; }

And a class 'Foo':

struct Foo {
friend void access(const Foo & f);
private:
int x;
};

As you can see, Foo is friended to the function 'access'. One more
thing to do: specialize access() for Foo:

template<void access(const Foo & f) { f.x = 9; }

So access() should be able to work, right? Because access() has been
friended properly to Foo?

The following main() code, however, won't compile. The compiler
complains that 'x' is private:

int main() {
Foo f;
access(f);
}

Did I declare the friend function incorrectly? Or is this simply not
possible?

Thanks in advance,
--Steve (mr************@hotmail.com)

Nov 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
mrstephengross wrote:
Hi folks... I've been sifting through archived postings on this, but
haven't quite found an answer yet. I've got a templated stand-alone
function:

template<typename Tvoid access(const T & t) { ; }

And a class 'Foo':

struct Foo {
friend void access(const Foo & f);
private:
int x;
};

As you can see, Foo is friended to the function 'access'.
Yes, to a non-templated overload of it.
One more thing to do: specialize access() for Foo:

template<void access(const Foo & f) { f.x = 9; }

So access() should be able to work, right? Because access() has been
friended properly to Foo?
Not if you mean the template specialization.
The following main() code, however, won't compile. The compiler
complains that 'x' is private:

int main() {
Foo f;
access(f);
}

Did I declare the friend function incorrectly? Or is this simply not
possible?
Try:

friend void access<>(const Foo & f);

Nov 6 '06 #2

P: n/a
Ok, the addition of the "<>" got it working. My next problem is
namespaces, though. I stick Foo in a namespace ("NS"), and it compiles;
the linker, however, complains that access<is multiply defined:

================================================
template<typename Tvoid access(const T & t) { ; }
namespace NS {
struct Foo {
friend void access<>(const NS::Foo & f);
private:
int x;
};
} // End namespace

template<void access(const NS::Foo & f) { f.x = 9; }

int main() { NS::Foo f; access(f); }
================================================

Am I maybe forgetting 'inline' tags? Is there some fundamental issue
with templates & namespaces?

Thanks again,
--Steve

Nov 6 '06 #3

P: n/a
mrstephengross schrieb:
Ok, the addition of the "<>" got it working. My next problem is
namespaces, though. I stick Foo in a namespace ("NS"), and it compiles;
the linker, however, complains that access<is multiply defined:

================================================
template<typename Tvoid access(const T & t) { ; }
namespace NS {
struct Foo {
friend void access<>(const NS::Foo & f);
private:
int x;
};
} // End namespace

template<void access(const NS::Foo & f) { f.x = 9; }

int main() { NS::Foo f; access(f); }
================================================

Am I maybe forgetting 'inline' tags? Is there some fundamental issue
with templates & namespaces?
I am surprised that this code can be compiled, as you pass a reference
to a const object in access(). Hence, the compiler should complain that
you try to access 'x' which is read-only... Or am I missing something?

Furthermore, note that the code above with the const qualifier removed
compiles fine with gcc 4.1.0

Cheers,
Loic.

Nov 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.