Reg. Function Templates

Discussion in 'C++' started by VSP, Aug 4, 2007.

  1. VSP

    VSP Guest

    Hi,

    In the following code, we have created a template function,
    We expected the output to display "A::A()" and "B::B()" but we are getting
    both as "B::B()".

    class A
    {
    public:
    A()
    {
    cout<<endl<<"A::A()";
    }
    };

    class B
    {
    public:
    B()
    {
    cout<<endl<<"B::B()";
    }
    };

    template<class T>
    void func()
    {
    T t;
    }

    int main(int argc, char* argv[])
    {
    func<A>();
    func<B>();
    return 0;
    }

    Please clarify.
    Compiler Used: VC++ 6.0

    Regards,
    VSP
     
    VSP, Aug 4, 2007
    #1
    1. Advertising

  2. VSP

    Jim Langston Guest

    "VSP" <> wrote in message
    news:f91fap$4a8$...
    > Hi,
    >
    > In the following code, we have created a template function,
    > We expected the output to display "A::A()" and "B::B()" but we are getting
    > both as "B::B()".
    >
    > class A
    > {
    > public:
    > A()
    > {
    > cout<<endl<<"A::A()";
    > }
    > };
    >
    > class B
    > {
    > public:
    > B()
    > {
    > cout<<endl<<"B::B()";
    > }
    > };
    >
    > template<class T>
    > void func()
    > {
    > T t;
    > }
    >
    > int main(int argc, char* argv[])
    > {
    > func<A>();
    > func<B>();
    > return 0;
    > }
    >
    > Please clarify.
    > Compiler Used: VC++ 6.0


    In VC++ .net 2003 the output is the expected

    A::A()
    B::B()

    Of course after I included <iostream> and changed cout and endl to std::cout
    and std::endl.

    Try a fresh build. If that doesn't work, it's a bug in VC++ 6.0. 6.0 is
    fairly old, prestandard and has a few bugs. You definately need to upgrade.
     
    Jim Langston, Aug 4, 2007
    #2
    1. Advertising

  3. VSP

    Bo Persson Guest

    Jim Langston wrote:
    :: "VSP" <> wrote in message
    :: news:f91fap$4a8$...
    ::: Hi,
    :::
    ::: In the following code, we have created a template function,
    ::: We expected the output to display "A::A()" and "B::B()" but we
    ::: are getting both as "B::B()".
    :::
    ::: class A
    ::: {
    ::: public:
    ::: A()
    ::: {
    ::: cout<<endl<<"A::A()";
    ::: }
    ::: };
    :::
    ::: class B
    ::: {
    ::: public:
    ::: B()
    ::: {
    ::: cout<<endl<<"B::B()";
    ::: }
    ::: };
    :::
    ::: template<class T>
    ::: void func()
    ::: {
    ::: T t;
    ::: }
    :::
    ::: int main(int argc, char* argv[])
    ::: {
    ::: func<A>();
    ::: func<B>();
    ::: return 0;
    ::: }
    :::
    ::: Please clarify.
    ::: Compiler Used: VC++ 6.0
    ::
    :: In VC++ .net 2003 the output is the expected
    ::
    :: A::A()
    :: B::B()
    ::
    :: Of course after I included <iostream> and changed cout and endl to
    :: std::cout and std::endl.
    ::
    :: Try a fresh build. If that doesn't work, it's a bug in VC++ 6.0.
    :: 6.0 is fairly old, prestandard and has a few bugs. You definately
    :: need to upgrade.

    Yes, this is a known bug in VC6. The compiler uses function
    parameters, but not template parameters, in the function signature.
    This fools the linker into believing that there is only one func().


    If you change the function to

    template<class T>
    void func(const T* = 0)
    {
    T t;
    }

    it will work as it should.

    If you try to learn C++, the free Visual C++ Express (VC8) is a much
    better choice.


    Bo Persson
     
    Bo Persson, Aug 4, 2007
    #3
    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. Fred
    Replies:
    1
    Views:
    627
    Neredbojias
    Sep 26, 2005
  2. JKop
    Replies:
    3
    Views:
    505
  3. csudha

    Reg virtual function and friend function

    csudha, Feb 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    317
    infobahn
    Feb 21, 2005
  4. BigMan
    Replies:
    1
    Views:
    446
  5. recover
    Replies:
    2
    Views:
    844
    recover
    Jul 25, 2006
Loading...

Share This Page