473,840 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error while testing the "non-template friend function of a templateclass"

Hi,

after reading the article " The Standard Librarian : Defining Iterators
and Const Iterators" from Matt Austern:
http://www.ddj.com/showArticle.jhtml...leID=184401331

I wanted to test using the non-template function friends of template
classes. So I devised this code:

===8<======8<== ====8<======8<= =====8<======8< ======8<===

#include <iostream>

using namespace std;

template <class T>
struct S
{

friend S foo( const S& s1, const S& s2 )
{
return S( s1.s + s2.s );
}

friend T operator+( const T& a, const S& s )
{
return a+s.s;
}

friend ostream& operator<<( ostream& s, const S& o )
{
s << "S(" << o.s << ")";
return s;
}

S( T a ) : s( a ) {}

T s;
};

int main()
{
S<ints1( 1 );
S<S<int s2( 10 );
cout << foo( s1, s2 );
cout << endl;
return 0;
}

===8<======8<== ====8<======8<= =====8<======8< ======8<===

It does compile but segfaults at runtime O_o
Could someone explain to me what I did wrong ?

thanks,

Pierre
Aug 4 '06 #1
3 1490

Pierre Barbier de Reuille wrote:
Hi,

after reading the article " The Standard Librarian : Defining Iterators
and Const Iterators" from Matt Austern:
http://www.ddj.com/showArticle.jhtml...leID=184401331

I wanted to test using the non-template function friends of template
classes. So I devised this code:

===8<======8<== ====8<======8<= =====8<======8< ======8<===

#include <iostream>

using namespace std;

template <class T>
struct S
{

friend S foo( const S& s1, const S& s2 )
{
return S( s1.s + s2.s );
}

friend T operator+( const T& a, const S& s )
{
Try adding the following two lines here:

cout << "[In operator+]";
cout.flush();
return a+s.s;
}

friend ostream& operator<<( ostream& s, const S& o )
{
s << "S(" << o.s << ")";
return s;
}

S( T a ) : s( a ) {}

T s;
};

int main()
{
S<ints1( 1 );
S<S<int s2( 10 );
cout << foo( s1, s2 );
cout << endl;
return 0;
}

===8<======8<== ====8<======8<= =====8<======8< ======8<===

It does compile but segfaults at runtime O_o
Could someone explain to me what I did wrong ?
Add the two lines I indicated above, then re-compile and run your
program. I think you'll see the problem pretty quickly.

By the way, your program won't compile on Comeau, so I suspect your use
of templates is not correct. (But that is NOT the cause of your
described problem.)

Best regards,

Tom

Aug 4 '06 #2
Thomas Tutone wrote:
Pierre Barbier de Reuille wrote:
>Hi,

after reading the article " The Standard Librarian : Defining Iterators
and Const Iterators" from Matt Austern:
http://www.ddj.com/showArticle.jhtml...leID=184401331

I wanted to test using the non-template function friends of template
classes. So I devised this code:
[...]
>>
It does compile but segfaults at runtime O_o
Could someone explain to me what I did wrong ?

Add the two lines I indicated above, then re-compile and run your
program. I think you'll see the problem pretty quickly.

By the way, your program won't compile on Comeau, so I suspect your use
of templates is not correct. (But that is NOT the cause of your
described problem.)

Best regards,

Tom


Thank you very much, I corrected the mistake ... Here is the code which
is working (I put only the class definition):
template <class T>
struct S
{

friend S foo( const S& s1, const S& s2 )
{
return S( s1.s + s2.s );
}

friend T operator+( const S& a, const T& s )
{
return a+s.s;
}

friend T operator+( const S& s1, const S& s2 )
{
return s1.s + s2.s;
}

friend ostream& operator<<( ostream& s, const S& o )
{
s << "S(" << o.s << ")";
return s;
}

S( T a ) : s( a ) {}

T s;
};
As for the correctness, I think it *is* correct. At least, the use of
the non-template friend functions is even explicitly allowed by the norm
(cf. the article which even gives the precise § in the norm).
However, if you could give me the error, I would be very interested.

Thanks,

Pierre
Aug 4 '06 #3
Pierre Barbier de Reuille wrote:
Thank you very much, I corrected the mistake ... Here is the code which
is working (I put only the class definition):
template <class T>
struct S
{

friend S foo( const S& s1, const S& s2 )
{
return S( s1.s + s2.s );
}

friend T operator+( const S& a, const T& s )
{
return a+s.s;
}

friend T operator+( const S& s1, const S& s2 )
{
return s1.s + s2.s;
}

friend ostream& operator<<( ostream& s, const S& o )
{
s << "S(" << o.s << ")";
return s;
}

S( T a ) : s( a ) {}

T s;
};
As for the correctness, I think it *is* correct. At least, the use of
the non-template friend functions is even explicitly allowed by the norm
(cf. the article which even gives the precise § in the norm).
However, if you could give me the error, I would be very interested.
Your code, as revised, compiles on Comeau without error. However, your
original code did not - see for yourself:

http://www.comeaucomputing.com/tryitout/

"ComeauTest .c", line 14: error: no operator "+" matches these
operands
operand types are: const S<int+ const S<int>
return S( s1.s + s2.s );
^
detected during instantiation of class "S<T[with
T=S<int>]" at
line 45

1 error detected in the compilation of "ComeauTest .c".

As I said, you've now corrected the problem.

Best regards,

Tom

Aug 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2716
by: mirnazim | last post by:
Hi, There are great Python Web Application Framework. But most of them are meant for content oriented web apps. Is there something that can ease the development of application that are not content oriented(I call them "NON CONTENT-ORIENTED WEB APPLICATIONS" because I don't know what else to call them). I mean the applications like, accounting, high volume data entry apps, where normally GUI clients have ruled. I know very high...
1
2134
by: JKop | last post by:
Would you classify the following code as "Undefined Behaviour" or as "Non- portable"? signed main() { signed char chedder = 130; } Relevant information:
15
2861
by: Sander Tekelenburg | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The HTML specs speak of "replaced" and "non-replaced" elements, yet for the life of me I can't find an explanation of what "replaced" is supposed to mean in this context. Can someone explain? TIA
0
1486
by: Robert Oschler | last post by:
I have a database table with a field that is indexed as a "full-text" search, since I want the capabiity. However, I also want the ability to quickly retrieve records from that table that are ins "sorted-order" by this very same field. Do I still need to create a "regular" non-full-text search index to have this capability? Or will the "full-text" index be sufficient? Thanks.
0
1527
by: Brent | last post by:
If my field has something like this: 1. dez 2. eft 3. 4. hgt 5. Is there any way to put the non-blank fields on top, sorted alphabetically A-Z, and the blank fields on bottom in whatever order?
4
2334
by: L Mehl | last post by:
Hello -- I am using Enterprise Architect, a UML modeling tool, which is built on a pre-Access 2000 mdb. The database describing the model has the extension ".eap". I want to build some queries, reports, etc., on this model and want to store them in a separate mdb. Is there a way to link to objects in an Access database that has a non-"mdb"
8
3526
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
2
1999
by: thorax | last post by:
I'm having problems running a release build of an application. The application is a native C++ .NET 2003 MFC application which links to a number of other DLLs, one of which is mixed (SLGSE.dll) (VB.NET/C++). Most of the DLLS are statically linked to the main exe (directly or indirectly) but a few are dynamically linked at runtime. When I try and run the app, it gets "An unhandled non- continuable exception was thrown during process...
3
2226
by: domcatanzarite | last post by:
How would one create a button that on click advances the form to the next "non recurring record" as opposed to the next record. The field the button needs to que from has groups of duplicate values. I need it to advance the form to the next group of duplicate values not just the next record.
7
10306
by: Benton | last post by:
Hi there, I have a text box which will receive its value from a pop-up date picker. The user should not be able to edit this field with the keyboard or mouse. I am using ASP.NET. If I set the readonly property of the textbox to true, it won't let the user change it and it can receive its value from the pop-op calendar, just what I need. Problem is, after a postback, value is lost. So I think Javascript may come to the rescue here. Is...
0
9698
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,...
0
10598
jinu1996
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...
1
10657
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,...
0
10299
tracyyun
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...
1
7836
isladogs
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...
0
5684
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...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3136
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.