A
anon
Hello,
I am reading the "c++ templates - the complete guide" book, and I found
something very strange.
On the pages 146-147, there is the example about the points of
instantiations. I wasn't lazy, and I made the full example like this:
///// code
#include <iostream>
using namespace std;
class MyInt
{
public:
MyInt(int i):i(i)
{
cout<<"MyInt() i="<<i<<endl;
}
~MyInt()
{
cout<<"~MyInt() i="<<i<<endl;
}
int i;
};
MyInt operator-(const MyInt& a )
{
return MyInt(a.i-1);
}
bool operator >(const MyInt &a,const MyInt &b)
{
return a.i>b.i;
}
typedef MyInt Int;
template < typename T >
void f(T i )
{
if ( i>T(0) )
{
g(-i);
}
}
void g(Int i)
{
f<Int>(i);
}
int main()
{
Int a(5);
g(a);
}
///// code
The book says that if I replace:
typedef MyInt Int;
with
typedef int Int;
the example will no longer compile, which is not true.
Can anyone (who has access to the book) explain why they wrote it should
not compile?
I am using g++ 4.1.3
I am reading the "c++ templates - the complete guide" book, and I found
something very strange.
On the pages 146-147, there is the example about the points of
instantiations. I wasn't lazy, and I made the full example like this:
///// code
#include <iostream>
using namespace std;
class MyInt
{
public:
MyInt(int i):i(i)
{
cout<<"MyInt() i="<<i<<endl;
}
~MyInt()
{
cout<<"~MyInt() i="<<i<<endl;
}
int i;
};
MyInt operator-(const MyInt& a )
{
return MyInt(a.i-1);
}
bool operator >(const MyInt &a,const MyInt &b)
{
return a.i>b.i;
}
typedef MyInt Int;
template < typename T >
void f(T i )
{
if ( i>T(0) )
{
g(-i);
}
}
void g(Int i)
{
f<Int>(i);
}
int main()
{
Int a(5);
g(a);
}
///// code
The book says that if I replace:
typedef MyInt Int;
with
typedef int Int;
the example will no longer compile, which is not true.
Can anyone (who has access to the book) explain why they wrote it should
not compile?
I am using g++ 4.1.3