P
Pierre Barbier de Reuille
Hi,
after reading the article " The Standard Librarian : Defining Iterators
and Const Iterators" from Matt Austern:
http://www.ddj.com/showArticle.jhtml;jsessionid=41JODBZDWEMBYQSNDLOSKH0CJUNN2JVN?articleID=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<int> s1( 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
Could someone explain to me what I did wrong ?
thanks,
Pierre
after reading the article " The Standard Librarian : Defining Iterators
and Const Iterators" from Matt Austern:
http://www.ddj.com/showArticle.jhtml;jsessionid=41JODBZDWEMBYQSNDLOSKH0CJUNN2JVN?articleID=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<int> s1( 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
Could someone explain to me what I did wrong ?
thanks,
Pierre