points of instantiation

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
 
F

Fraser Ross

A footnote mentions that the code is using a part of the language that
is under review. Does your copy not have that? The program doesn't
compile with int because ADL isn't done for the built in types.

Fraser.
 
A

anon

Fraser said:
A footnote mentions that the code is using a part of the language that
is under review. Does your copy not have that? The program doesn't
compile with int because ADL isn't done for the built in types.

My book has the footnote, but the problem is my example is compiling
when I replace MyInt with int.

Can you suggest a modification to my example that demonstrates what they
wanted to show?
 
M

Michael DOUBEZ

anon a écrit :
My book has the footnote, but the problem is my example is compiling
when I replace MyInt with int.

Can you suggest a modification to my example that demonstrates what they
wanted to show?

Change your compiler :) I suppose you are using gcc.

With Comeau the original cases succeeds and with int:
Comeau C/C++ 4.3.10.1 (May 29 2008 09:37:15) for ONLINE_EVALUATION_BETA1
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 33: error: identifier "g" is undefined
g(-i);
^
detected during instantiation of "void f(T) [with T=Int]" at
line 39

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

In strict mode, with -tused, Compile failed

For another relvant example about ADL niceties with int, see:
http://www.cs.rpi.edu/~musser/stl-book/index_9.html
 
D

diamondback

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

This sounds like an error in the text to me. It is wrong for a
programming text to state, unequivocally that something will/will not
compile. The intent is obviously *should* not compile. But, there is
always an exception based on standards, compiler, hardware, version,
etc. In this case, it seems that your g++ is fine with the construct
for some reason. Different compilers may have various different
responses.
 
T

Triple-DES

[example from C++ Templates p146-147]
This sounds like an error in the text to me. It is wrong for a
programming text to state, unequivocally that something will/will not
compile. The intent is obviously *should* not compile.

Actually, the exact wording is: "(...) the previous example _should_
no longer compile".
But, there is
always an exception based on standards, compiler, hardware, version,
etc. In this case, it seems that your g++ is fine with the construct
for some reason. Different compilers may have various different
responses.

True, but successfully compiling the example program (an ill-formed
program) without issuing a diagnostic is prohibited by the C++
standard.

DP
 
A

anon

Triple-DES said:
On Sep 22, 3:26 am, anon <[email protected]> wrote:

[example from C++ Templates p146-147]
This sounds like an error in the text to me. It is wrong for a
programming text to state, unequivocally that something will/will not
compile. The intent is obviously *should* not compile.

Actually, the exact wording is: "(...) the previous example _should_
no longer compile".
But, there is
always an exception based on standards, compiler, hardware, version,
etc. In this case, it seems that your g++ is fine with the construct
for some reason. Different compilers may have various different
responses.

True, but successfully compiling the example program (an ill-formed
program) without issuing a diagnostic is prohibited by the C++
standard.

I tried (as Michael Doubez suggested) to use comeau online to compile
the example, and it failed.

I was surprised when the g++ accepted that without even the warning
(with -pedantic -ansi -Wall)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top