namespace and friend templates

Started by
6 comments, last by Enigma 18 years, 9 months ago
ok this is annoying, why wont this work?

namespace TestNS
{
	class Test
	{
	private:
		Test();
		template <typename T> friend struct Loki::CreateUsingNew;
	};
};

thx!
Advertisement
Quote:Original post by edwinnie
ok this is annoying, why wont this work?

*** Source Snippet Removed ***

thx!


make sure Loki::CreateUsingNew is declared prior to you trying to befriend it.
hey thx fer replying!

but what happened was that the compiler gave a "Loki::CreateUsingNew cannot be defined in namespace TestNS"...
Quote:
template <typename T> friend struct Loki::CreateUsingNew;


Why do you template the friend declaration, and then not use T ?

Also did you try to put :: in front of the Loki name ?

template <typename T> friend struct ::Loki::CreateUsingNew<T>;
Quote:Original post by edwinnie
hey thx fer replying!

but what happened was that the compiler gave a "Loki::CreateUsingNew cannot be defined in namespace TestNS"...


Compilers can be extremely picky about accepting fully qualified forward references. Some compilers will accept 'friend struct Test;' but not 'friend struct Loki::Test;'. Since you didn't provide much information about what Loki or CreateUsingNew are or how they are declared it's a little difficult to give an accurate solution. The use of template<typename T> in the friend declaration is also suspect. With no template argument passed the compiler will reject it regardless.
Your syntax is correct according to what I could find. Looks like you're using VC++ 2003 .NET, which seems to require the (possibly non-standard) syntax:
friend struct namespace::ClassName;

without the template declaration.

I'll have a look at the final draft standard and see what I can find.

EDIT: The standard states:
Quote:C++ final draft standard, paragraph 14.5.3.2:
A friend template may be declared within a nontemplate class. A friend function template may be defined within a nontemplate class. In these cases, all specializations of the class or function template are friends of the class granting friendship. [Example:
class A {	template<class T> friend class B; // ok	template<class T> friend void f(T){ /* ... */ } // ok};
—end example]


So it looks like VC++ is just non-standard in this respect.

Enigma
Hmm... still cannot get it to work though.
we need that template declaration in front coz Loki's singleton has a template parameter T as well.

//so far i tried to typedef it at global scope:typedef struct Loki::CreateUsingNew LOKI_CREATEUSINGNEW;//then in the namespace:namespace TestNS{	class Test	{	private:		Test(){}		template < typename T > friend struct LOKI_CREATEUSINGNEW;        };        typedef Loki::SingletonHolder < Test > STest;};

what happened now is that it cannot access the private constructor...
(off course we also do not want in to be in public scope)

[Edited by - edwinnie on June 28, 2005 10:16:32 PM]
What I meant was that VC++ seems not to require (or even allow) the template declaration in the template friend declaration, but still declares all associated templates as friends. The following compiles and executes correctly on VC++ 2003 .NET:
#include <iostream>namespace other{	class Test2;}namespace test{	template < typename TYPE >	class Test1	{		public:			void testFunc(other::Test2 const & t)			{				// access private x_ member variable of other::Test2				TYPE ttr = TYPE(t.x_);				std::cout << ttr / 2;			}	};}namespace other{	class Test2	{		public:			friend class ::test::Test1;			Test2(int x)				:				x_(x)			{			}			private:			int x_;	};}int main(){	other::Test2 t2(37);	test::Test1< float > t1f;	test::Test1< unsigned short > t1us;	t1f.testFunc(t2);	t1us.testFunc(t2);}


Enigma

This topic is closed to new replies.

Advertisement