Topic: friend declaration using typedefs
Author: "PETER NORDLUND" <peter_nordlund@swipnet.se>
Date: 2000/02/29 Raw View
Hi all!
I have a problem with a friend declaration
When compiling with gcc 2.95.2 I get an Internal compiler error.
When compiling with a recent snapshot I get an error message.
After reading the standard I don't understand what is legal C++.
Can someone explain?
I am trying to use typedefs in a friend declaration.
I suspect that this illegal since this is not an
"elaborated-type-specifier".
But I do not really know what that means ....
I will give 2 code examples together with compiler error messages.
If someone knows what really is correct or not I would be greatful!
If none of the code I have written is correct, is there a short answer to
"How much typedef you can use in a friend declaration" ?
---------------------
Example 1:
This code gives 2 different result with 2 different compiler versions.
With gcc 2.95.2:
bug2.cpp:20: Internal compiler error.
bug2.cpp:20: Please submit a full bug report.
With the mainline compiler (2.96 20000125 (experimental)):
bug2.cpp:20: typedef `Singleton<S, NoCleanupMgr2>::InstantiatorType' in
elaborated type specifier
bug2.cpp:20: ANSI C++ forbids declaration of `type name' with no type
----------
template <typename SINGLETON, typename MANAGER> class SingletonTraits { };
template <typename SINGLETON, typename MANAGER>
class Singleton
public:
typedef typename
SingletonTraits<SINGLETON, MANAGER>::InstantiatorType InstantiatorType;
};
class NoCleanupMgr2 {};
// Traits class
//
template <typename SINGLETON>
struct SingletonTraits<SINGLETON, NoCleanupMgr2> {
typedef NoCleanupMgr2 InstantiatorType;
};
class S: public Singleton<S, NoCleanupMgr2> {
friend class Singleton<S, NoCleanupMgr2>::InstantiatorType; // Line 20
};
int main(){
return 0;
}
---------------------------------------
Example 2:
This code compiles ok with both gcc 2.95.2 and the recent snapshot, but is
it correct?
-----
template <typename SINGLETON, typename MANAGER> class SingletonTraits { };
template <typename SINGLETON, typename MANAGER>
class Singleton
public:
typedef typename
SingletonTraits<SINGLETON, MANAGER>::InstantiatorType InstantiatorType;
};
class NoCleanupMgr2
public:
class H {};
};
// Traits class
//
template <typename SINGLETON>
struct SingletonTraits<SINGLETON, NoCleanupMgr2> {
typedef NoCleanupMgr2::H InstantiatorType;
};
class S: public Singleton<S, NoCleanupMgr2> {
friend class Singleton<S, NoCleanupMgr2>::InstantiatorType;
};
int main(){
S s;
return 0;
}
------------------------------
I would also be grateful for any constructive comments of what to do
instead,
if the code I have written is totally wrong. I am using typedefs because I
don't
whant to change the friend decl if I change the MANAGER template parameter.
So, in a complete example the class NoCleanupMgr2 would be a typedeffed in
the
class S definition. Then it would look like this:
typedef NoCleanupMgr2 Mgr;
class S: public Singleton<S, Mgr> {
friend class Singleton<S, Mgr>::InstantiatorType;
};
---
Best regards, Peter Nordlund
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]