template type problem in msvc. and more

Discussion in 'C++' started by icedac, Feb 2, 2005.

  1. icedac

    icedac Guest

    I have some questions about template and c++ itself.

    /////////////////////////////////////////////////////////////////////////
    q1) see follow c++ code, and compile. it's works only IntelC++8.1 but
    VC71.
    Do you know why? Which compiler's activity is C++STANDARD? And I wanna
    be
    feed back some explain. :)



    //-----------------------------------------------------------------------
    //
    // COMPILER_TEST!,
    //
    // a) VC71 fail to compile
    // b) IC81 OK
    //
    //-----------------------------------------------------------------------
    template < typename MY_TYPE_ >
    class TestClass1
    {
    public:
    typedef int TEST_TYPE;
    };

    template < typename MY_TYPE_ >
    class TestClass2
    {
    public:
    typedef MY_TYPE_ MY_TYPE;
    typedef TestClass1<MY_TYPE> TEST_CLASS1;
    typedef TestClass2<MY_TYPE> TEST_CLASS2;
    typedef typename TEST_CLASS1::TEST_TYPE TEST_TYPE1;


    //typename TestClass2<MY_TYPE_>:: // this make msvc happy, but I dont
    know why!
    TEST_TYPE1 TestMethod1();

    //
    // consequently,
    // a) typename TestClass2<MY_TYPE_>::TEST_TYPE1
    // and
    // b) TEST_TYPE1
    //
    // is different type! why? its the same type!
    //
    // i think its result due to the "typename"
    //

    void TestMethod2( TEST_TYPE1 a );
    };

    /* this will emit error in MSVC */
    template < typename MY_TYPE_ >
    typename TestClass2<MY_TYPE_>::TEST_TYPE1 /* return type; return type
    can not use types in TestClass2
    it have to specify TestClass2 scope to compile*/
    TestClass2<MY_TYPE_>::TestMethod1()
    {
    printf( "typename TestClass2<MY_TYPE_>::TEST_TYPE1
    TestClass2<MY_TYPE_>::TestMethod1() - called.\r\n" );
    return 0;
    }

    /* this is ok */
    template < typename MY_TYPE_ >
    void
    TestClass2<MY_TYPE_>::TestMethod2(
    //typename TestClass2<MY_TYPE_>:: //this var is on scope of
    TestClass2, it can use types in TestClass2
    TEST_TYPE1 a
    )
    {
    return 0;
    }


    void test_2()
    {
    TestClass2<int> a;
    a.TestMethod1();
    }
    //---------END_OF_CODE

    /////////////////////////////////////////////////////////////////////////
    q2)
    code:
    //-- start of code
    class TestCalss3
    {
    public:
    typedef int TEST_TYPE3;
    TEST_TYPE3 TestMethod3( TEST_TYPE3 a );
    };

    inline
    TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
    {
    return 1;
    }
    //-- end of code

    Why c++ standard make difference between 'RETURN type' and 'ARGUMENT
    type'?
    I think 'new code1' or 'new code2' is looks better. Or I omit some
    important point(s) about c++ design or compiler design?

    //-- new code1
    inline
    TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
    {
    return 1;
    }
    //-- new code2
    inline
    TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TestCalss3::TEST_TYPE3
    a )
    {
    return 1;
    }
    //--



    /////////////////////////////////////////////////////////////////////////
    I'll expect good reply. Thank you in advance.
    ps excuse my BAD english :) I'm not native writer :0
    icedac, Feb 2, 2005
    #1
    1. Advertising

  2. icedac wrote:
    > I have some questions about template and c++ itself.
    >
    >

    /////////////////////////////////////////////////////////////////////////
    > q1) see follow c++ code, and compile. it's works only IntelC++8.1 but
    > VC71.
    > Do you know why? Which compiler's activity is C++STANDARD? And I

    wanna
    > be
    > feed back some explain. :)
    >
    >
    >
    >

    //-----------------------------------------------------------------------
    > //
    > // COMPILER_TEST!,
    > //
    > // a) VC71 fail to compile
    > // b) IC81 OK
    > //
    >

    //-----------------------------------------------------------------------
    > template < typename MY_TYPE_ >
    > class TestClass1
    > {
    > public:
    > typedef int TEST_TYPE;
    > };
    >
    > template < typename MY_TYPE_ >
    > class TestClass2
    > {
    > public:
    > typedef MY_TYPE_ MY_TYPE;
    > typedef TestClass1<MY_TYPE> TEST_CLASS1;
    > typedef TestClass2<MY_TYPE> TEST_CLASS2;
    > typedef typename TEST_CLASS1::TEST_TYPE TEST_TYPE1;
    >
    >
    > //typename TestClass2<MY_TYPE_>:: // this make msvc happy, but I

    dont
    > know why!
    > TEST_TYPE1 TestMethod1();

    I think it is a problem in VC. IMHO, those definitions are equivalent.

    >
    > //
    > // consequently,
    > // a) typename TestClass2<MY_TYPE_>::TEST_TYPE1
    > // and
    > // b) TEST_TYPE1
    > //
    > // is different type! why? its the same type!
    > //
    > // i think its result due to the "typename"
    > //

    Don't know. Maybe you should post those questions also in a VC related
    newsgroup.

    >
    > void TestMethod2( TEST_TYPE1 a );
    > };
    >
    > /* this will emit error in MSVC */
    > template < typename MY_TYPE_ >
    > typename TestClass2<MY_TYPE_>::TEST_TYPE1 /* return type; return type
    > can not use types in TestClass2
    > it have to specify TestClass2 scope to compile*/
    > TestClass2<MY_TYPE_>::TestMethod1()
    > {
    > printf( "typename TestClass2<MY_TYPE_>::TEST_TYPE1
    > TestClass2<MY_TYPE_>::TestMethod1() - called.\r\n" );
    > return 0;
    > }
    >
    > /* this is ok */
    > template < typename MY_TYPE_ >
    > void
    > TestClass2<MY_TYPE_>::TestMethod2(
    > //typename TestClass2<MY_TYPE_>:: //this var is on scope of
    > TestClass2, it can use types in TestClass2
    > TEST_TYPE1 a
    > )
    > {
    > return 0;
    > }
    >

    Hm, it doesn't look ok. The method has void type as return type, but
    you return 0. I know that VC is silent about that, but it should say
    something, at least a warning.

    >
    > void test_2()
    > {
    > TestClass2<int> a;
    > a.TestMethod1();
    > }
    > //---------END_OF_CODE
    >
    >

    /////////////////////////////////////////////////////////////////////////
    > q2)
    > code:
    > //-- start of code
    > class TestCalss3
    > {
    > public:
    > typedef int TEST_TYPE3;
    > TEST_TYPE3 TestMethod3( TEST_TYPE3 a );
    > };
    >
    > inline
    > TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
    > {
    > return 1;
    > }

    Here I don't know how real 'a' type is deduced (there is no such type
    in global namespace). Well, I'll search a little :). Comeau online also
    compile fine this method implementation.

    > //-- end of code
    >
    > Why c++ standard make difference between 'RETURN type' and 'ARGUMENT
    > type'?
    > I think 'new code1' or 'new code2' is looks better. Or I omit some
    > important point(s) about c++ design or compiler design?
    >
    > //-- new code1
    > inline
    > TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
    > {
    > return 1;
    > }

    Not correct, as I said there isn't such a type in the global namespace
    (equivalent with TestCalss3::TEST_TYPE3, eg. typedef
    TestCalss3::TEST_TYPE3 TEST_TYPE3).

    > //-- new code2
    > inline
    > TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3(

    TestCalss3::TEST_TYPE3
    > a )
    > {
    > return 1;
    > }
    > //--
    >

    This is ok :).
    >

    ..
    ..
    Best regards,
    Bogdan Sintoma
    Bogdan Sintoma, Feb 2, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. h79
    Replies:
    2
    Views:
    412
  2. Michael
    Replies:
    4
    Views:
    399
    Matt Hammond
    Jun 26, 2006
  3. Niels Dekker - no reply address
    Replies:
    11
    Views:
    1,892
    Niels Dekker - no reply address
    Jan 28, 2006
  4. Alf P. Steinbach

    MSVC sillywarnings -- more?

    Alf P. Steinbach, Feb 23, 2010, in forum: C++
    Replies:
    8
    Views:
    1,645
    Balog Pal
    Feb 26, 2010
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    206
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page