points of instantiation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anon

    points of instantiation

    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
  • Fraser Ross

    #2
    Re: points of instantiation

    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.


    Comment

    • anon

      #3
      Re: points of instantiation

      Fraser Ross wrote:
      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?

      Comment

      • diamondback

        #4
        Re: points of instantiation

        On Sep 22, 3:26 am, anon <a...@no.invali dwrote:
        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.

        Comment

        • Triple-DES

          #5
          Re: points of instantiation

          On 22 Sep, 20:31, diamondback <christopher... .@gmail.comwrot e:
          On Sep 22, 3:26 am, anon <a...@no.invali dwrote:
          [example from C++ Templates p146-147]
          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.
          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

          Comment

          • anon

            #6
            Re: points of instantiation

            Triple-DES wrote:
            On 22 Sep, 20:31, diamondback <christopher... .@gmail.comwrot e:
            >On Sep 22, 3:26 am, anon <a...@no.invali dwrote:
            >
            [example from C++ Templates p146-147]
            >
            >>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.
            >
            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)

            Comment

            Working...