A problem about template function overload

Discussion in 'C++' started by miaohua1982@gmail.com, Mar 18, 2007.

  1. Guest

    the code as follows:
    #include<iostream>
    using namespace std;

    template <int N>
    void foo( const char (&str)[N])
    {
    cout<<"array"<<endl;
    }
    template <typename T>
    void foo(const T& str);
    template <>
    void foo(char *const &str)
    {
    cout<<"char *"<<endl;
    }

    int main()
    {
    char arr[10];
    foo(arr);
    }

    why the called function is the void foo(const char(&str)[N])? I test
    the code by VC7, the output is
    *array*, but according to my knowledge, the following code is also
    OK:

    char p[1];
    char * const & str = p;

    so why the output is not *char*? I mean the why the *exact match*
    funcion is not the secnod "foo" with params (char *const &str)?

    It has confused me so much. Is there anyone call tell me?
    Thank you very much!
    , Mar 18, 2007
    #1
    1. Advertising

  2. On 2007-03-18 14:02, wrote:
    > the code as follows:
    > #include<iostream>
    > using namespace std;
    >
    > template <int N>
    > void foo( const char (&str)[N])
    > {
    > cout<<"array"<<endl;
    > }
    > template <typename T>
    > void foo(const T& str);
    > template <>
    > void foo(char *const &str)
    > {
    > cout<<"char *"<<endl;
    > }
    >
    > int main()
    > {
    > char arr[10];
    > foo(arr);
    > }
    >
    > why the called function is the void foo(const char(&str)[N])? I test
    > the code by VC7, the output is
    > *array*, but according to my knowledge, the following code is also
    > OK:
    >
    > char p[1];
    > char * const & str = p;
    >
    > so why the output is not *char*? I mean the why the *exact match*
    > funcion is not the secnod "foo" with params (char *const &str)?


    Because an array is not a pointer, it can however decay (is that the
    correct word?) to a pointer, so the function taking an array is a better
    match since no conversion is needed.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Mar 18, 2007
    #2
    1. Advertising

  3. Guest

    On 3月18æ—¥, 下åˆ10æ—¶28分, Erik Wikström <> wrote:
    > On 2007-03-18 14:02, wrote:
    >
    >
    >
    >
    >
    > > the code as follows:
    > > #include<iostream>
    > > using namespace std;

    >
    > > template <int N>
    > > void foo( const char (&str)[N])
    > > {
    > >       cout<<"array"<<endl;
    > > }
    > > template <typename T>
    > > void foo(const T& str);
    > > template <>
    > > void foo(char *const &str)
    > > {
    > >    cout<<"char *"<<endl;
    > > }

    >
    > > int main()
    > > {
    > >     char arr[10];
    > >    foo(arr);
    > > }

    >
    > > why  the called function is the  void foo(const char(&str)[N])? I test
    > > the code by VC7, the output is
    > > *array*,  but according to my knowledge, the following code is also
    > > OK:

    >
    > > char p[1];
    > > char * const & str = p;

    >
    > > so why the output is not *char*? I mean the why the *exact match*
    > > funcion is not the secnod "foo" with params (char *const &str)?

    >
    > Because an array is not a pointer, it can however decay (is that the
    > correct word?) to a pointer, so the function taking an array is a better
    > match since no conversion is needed.
    >
    > --
    > Erik Wikström- éšè—被引用文字 -
    >
    > - 显示引用的文字 -


    well, I don't think so. Just have a look at the following code:
    #include<iostream>
    using namespace std;

    template <int N>
    void foo( const char (&str)[N])
    {
    cout<<"array"<<endl;
    }

    void foo(char * const &str)
    {
    cout<<"char *"<<endl;
    }

    int main()
    {
    char arr[10];
    foo(arr);
    }

    the output in VC7 is "char*", so can you explain it?
    , Mar 19, 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. Victor Bazarov

    Re: Template member function overload

    Victor Bazarov, Jul 31, 2003, in forum: C++
    Replies:
    0
    Views:
    520
    Victor Bazarov
    Jul 31, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    368
    Nick Hounsome
    Jan 25, 2004
  3. Fei Liu
    Replies:
    5
    Views:
    344
    Maxim Yegorushkin
    Feb 25, 2006
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
  5. 邹俊洋
    Replies:
    0
    Views:
    191
    邹俊洋
    Aug 12, 2013
Loading...

Share This Page