Is this somehow illegal?
template<typena me T>
class A {
public:
private:
friend class T;
int intVal;
};
class B {
public:
int init (A<B>& a)
{
a.intVal = 6;
}
};
....
error C2248: 'A<T>::intVal' : cannot access private member declared in
class 'A<T>' 8 1917
LuB wrote: Is this somehow illegal?
template<typena me T> class A { private: friend class T; };
My compiler (gcc 4.0.2) complains at this point because T is not
necessarily a class. Only classes can be friends. Since according to
this, your program is illegal from this point on, there's not much
point in speculating about what's going on later. Either your compiler
is wrong and it's 'nuff said, or mine is wrong and we've learned
something and can proceed. Given that mine is a recent version of gcc,
I'd lean towards the former likelihood.
Luke
Not sure what you mean. Both our compilers failed. Why is mine wrong
and yours right or vice versa? Did you notice the error at the end of
the post?
At any rate, after a bit more trial and error, substitute
friend typename T;
for
friend class T;
That successfully compiles in MSVC 2005.
Thanks,
-Luther
....Sorry if this is a TOP post. Google only has so much flexibility.
LuB wrote: Not sure what you mean. Both our compilers failed. Why is mine wrong and yours right or vice versa? Did you notice the error at the end of the post?
Please quote what you're replying to, as a matter of standard Usenet
courtesy.
Both compilers did not "fail," in the sense of doing something wrong.
They each produced errors. However, my compiler produced an error at
an earlier point -- if it was correct to do so, then your compiler may
have been incorrect to *not* produce an error at the same point. Based
on the reasoning I provided (which you ignored?), and the fact that gcc
is has a reputation for being far more standards-compliant than MSVC,
it seems far more likely that your compiler is doing the wrong thing
here, in the sense of providing nonstandard behavior. If that's the
case, it's useless and/or off-topic to speculate further about the
consequences of said nonstandard behavior.
At any rate, after a bit more trial and error, substitute
friend typename T;
for
friend class T;
That successfully compiles in MSVC 2005.
I don't have my copy of the standard with me (keep it at work), but I
suspect that while this works on your compiler, it's not correct
according to the standard. My reasoning is based on the fact that only
certain types (classes and functions) can be friends. Since your T can
be anything, including types which are not eligible for friendship, the
compiler objects. This is kind of a nitpick, but it's the sort of
nitpick C++ compilers tend to make. It's also the sort of thing MSVC
implementors are apt to ignore.
Anyway, it's nice that you got it working on your compiler. If you
don't need your code to be portable between platforms, compilers, or
compiler versions, or be standards-compliant, then you're all set.
Does the standard even allow "friend typename" used in this way? I'm a
little suspicious of that, too.
...Sorry if this is a TOP post. Google only has so much flexibility.
You didn't top-post, you failed to quote entirely. If you click
"options" at the top of the message, and click "reply" from there,
you'll get a proper quote to work with. I'm posting this from Google
as well, and as you can see I quote just fine.
Luke
Luke Meyers wrote: LuB wrote: Is this somehow illegal?
template<typena me T> class A { private: friend class T; };
My compiler (gcc 4.0.2) complains at this point because T is not necessarily a class. Only classes can be friends. [snip]
I think that cannot be the reason your compiler complains: such a check
would be postponed until the template is instantiated. Then, if the actual
type passed is not a class, you should see an error.
However, the code is illegal because the standard explicitly says so in
[7.1.5.3/2]:
3.4.4 describes how name lookup proceeds for the identifier in an
elaborated-type-specifier. If the identifier resolves to a class-name or
enum-name, the elaborated-type-specifier introduces it into the declaration
the same way a simple-type-specifier introduces its type-name. If the
identifier resolves to a typedefname or a template type-parameter, the
elaborated-type-specifier is ill-formed. [Note: this implies that, within a
class template with a template type-parameter T, the declaration
friend class T;
is ill-formed. ] If name lookup does not find a declaration for the name,
the elaborated-type-specifier is ill-formed unless it is of the simple form
class-key identifier in which case the identifier is declared as described
in 3.3.1.
Support for this provision in g++ is a little flaky and you can sometimes
fool the compiler. For instance, g++ (4.1.0) still accepts the following
*illegal* code:
template < typename T >
class identity {
public:
typedef T me;
};
template < typename T >
class my_friend {
private:
friend class identity< T >::me;
char x;
};
class The_T {
public:
static
char & peek_friend ( my_friend< The_T > & f ) {
return( f.x );
}
};
int main (void) {
my_friend< The_T > x;
}
Best
Kai-Uwe Bux
Kai-Uwe Bux wrote: Luke Meyers wrote: LuB wrote: Is this somehow illegal?
template<typena me T> class A { private: friend class T; }; My compiler (gcc 4.0.2) complains at this point because T is not necessarily a class. Only classes can be friends. [snip]
I think that cannot be the reason your compiler complains: such a check would be postponed until the template is instantiated. Then, if the actual type passed is not a class, you should see an error.
I get the same error with the following complete program:
template<class T>
class A
{
friend class T;
};
int main () { return 0; }
I guess gcc just doesn't trust me not to instantiate it.
However, the code is illegal because the standard explicitly says so in [7.1.5.3/2]:
Nice, thanks very much for the cite. I don't suppose I could convince
you to take a look at the unique_copy constness issue I encountered in
another thread? No bites so far (though it's early yet)... :)
Cheers,
Luke
Luke Meyers wrote: LuB wrote: Not sure what you mean. Both our compilers failed. Why is mine wrong and yours right or vice versa? Did you notice the error at the end of the post?
Please quote what you're replying to, as a matter of standard Usenet courtesy.
Both compilers did not "fail," in the sense of doing something wrong. They each produced errors. However, my compiler produced an error at an earlier point -- if it was correct to do so, then your compiler may have been incorrect to *not* produce an error at the same point. Based on the reasoning I provided (which you ignored?), and the fact that gcc is has a reputation for being far more standards-compliant than MSVC, it seems far more likely that your compiler is doing the wrong thing here, in the sense of providing nonstandard behavior. If that's the case, it's useless and/or off-topic to speculate further about the consequences of said nonstandard behavior.
At any rate, after a bit more trial and error, substitute
friend typename T;
for
friend class T;
That successfully compiles in MSVC 2005.
I don't have my copy of the standard with me (keep it at work), but I suspect that while this works on your compiler, it's not correct according to the standard. My reasoning is based on the fact that only certain types (classes and functions) can be friends. Since your T can be anything, including types which are not eligible for friendship, the compiler objects. This is kind of a nitpick, but it's the sort of nitpick C++ compilers tend to make. It's also the sort of thing MSVC implementors are apt to ignore.
Anyway, it's nice that you got it working on your compiler. If you don't need your code to be portable between platforms, compilers, or compiler versions, or be standards-compliant, then you're all set.
Does the standard even allow "friend typename" used in this way? I'm a little suspicious of that, too.
...Sorry if this is a TOP post. Google only has so much flexibility.
You didn't top-post, you failed to quote entirely. If you click "options" at the top of the message, and click "reply" from there, you'll get a proper quote to work with. I'm posting this from Google as well, and as you can see I quote just fine.
Luke
It was a simple question ... why all the ridicule?
Kai-Uwe Bux wrote: Luke Meyers wrote:
LuB wrote: Is this somehow illegal?
template<typena me T> class A { private: friend class T; }; My compiler (gcc 4.0.2) complains at this point because T is not necessarily a class. Only classes can be friends. [snip]
I think that cannot be the reason your compiler complains: such a check would be postponed until the template is instantiated. Then, if the actual type passed is not a class, you should see an error. However, the code is illegal because the standard explicitly says so in [7.1.5.3/2]:
3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier. If the identifier resolves to a class-name or enum-name, the elaborated-type-specifier introduces it into the declaration the same way a simple-type-specifier introduces its type-name. If the identifier resolves to a typedefname or a template type-parameter, the elaborated-type-specifier is ill-formed. [Note: this implies that, within a class template with a template type-parameter T, the declaration
friend class T;
is ill-formed. ] If name lookup does not find a declaration for the name, the elaborated-type-specifier is ill-formed unless it is of the simple form class-key identifier in which case the identifier is declared as described in 3.3.1.
.... Best
Kai-Uwe Bux
Thank you for the elaboration Kai. Very much appreciated. I believe
I'll need a different approach.
-Luther
LuB wrote: It was a simple question ... why all the ridicule?
I find it unfortunate that you interpreted any of what I said as such.
It was certainly not my intent, and I wonder if you could find your way
to a more charitable interpretation. It might help to consider a few
points of local culture:
1. Many who frequent this group, myself included, are more-than-usually
precise with our language. This pays off in various ways and is
generally a Good Thing, but can occasionally come off as abrasive to
the uninitiated. This is particularly prone to occur when
disambiguating language one is responding to (e.g. your term "fail").
2. It's of pragmatic importance to assertively reinforce norms
regarding what is and is not on-topic. In particular, discussion
begins and ends (or is meant to) where the C++ standard does -- hence,
it's not a good idea to let a conversation drift into discussion of,
for example, questions deriving from the consequences of nonstandard
extensions a particular compiler happens to provide. There are fora
for such things; this is not one of them.
3. Factual statements containing no value judgements generally best not
taken as personal attacks, even if you don't like what they imply about
your favorite compiler, newsreader, etc.
Nothing I said was intended to impugn or ridicule anyone. My
statements of fact regarding norms of Usenet etiquette and the
mechanics of Google Groups, the current and historical level of
standards-compliance in the MSVC compiler, various terminology, the
contents of the C++ standard, and your options regarding portability
and compliance were all intended as just that -- factual statements
with no value judgements implied or desired.
I hope this explanation satisfies you, and that you will in future be
less prone to take offense from those who behave civilly and make no
attempt to give it. Especially when they're trying to provide help you
requested, hmm?
Luke This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Stippler |
last post by:
Hi
what's wrong about the following code? It does not compile.
template <typename T>
class A {
public:
class B;
};
|
by: Sebastian Faust |
last post by:
Hi,
is a construction like the following possible:
template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
|
by: Dmitry D |
last post by:
Hi all,
I'm having problems with declaring a template friend function. It seems like
I've done everything as explained
in C++ FAQ, but still, I'm getting the linker error (unresolved external
'aFunc(A<char> const&)' ) in
the following sample program. Tried it on GCC and Borland's compiler.
Can somebody please show me the correct way to fix this?
Thanks,
Dmitry
|
by: Ben Ingram |
last post by:
Hi all,
I am writing a template matrix class in which the template parameters are
the number of rows and number of columns. There are a number of reasons
why this is an appropriate tradeoff for my particular application. One of the
advantages is that the _compiler_ can force inner matrix dimensions used in
multiplication to agree. A _complie-time_ error will be triggered if you
write A * B and the number of coluns in A does not equal the...
|
by: James Aguilar |
last post by:
Hello, all.
I saw this in the provided code of a lab that I have due in a couple of
weeks in my algorithms class. It compiled fine in g++ but did not compile
with the VC++ compiler. It does not recognize the angle brackets that
follow "<<" in the operator friend declaration.
--- CODE ---
template <class T> class PriorityQueue {
| |
by: Grumble |
last post by:
Hello everybody,
(Disclaimer: I do not understand C++ templates.)
I've tried to make a testcase from the original code.
GCC 3.3.5 compiles with no warning, while GCC 3.4.3 rejects it. Could
somebody tell me if it because the code is illegal, and GCC 3.4.3's
stricter parser now rejects it, or is it some kind of regression?
|
by: BigMan |
last post by:
I have a class template like this:
template< typename t >
class c;
I'd also like to have an operator == for objects of types, which are
different specializations of c:
template< typename t1, typename t2 >
bool operator ==
|
by: Zeng |
last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was
not supported in C#. It's almost about as bad as it doesn't support the
inheritance hierarchy and method reference (calling tree) browsing that is
supported in C++. I don't know how some could write a large scale
object-oriented application w/o those. If you have overcome these
limitations in C#, please share your thoughts and ideas. Thanks!
|
by: H9XLrv5oXVNvHiUI |
last post by:
Hi, I have a question about injecting friend functions within template
classes. My question is specific to gcc (version 3.4.5) used in
combination with mingw because this code (or at least code that gets
the same result) works as expected in visualc++. I know that this is
probably not the right behavior for a compiler but it's the kind of
behavior I'm searching for so I was hoping there was a way to do the
same thing in gcc.
As you know...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |