A problem about template function overload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • miaohua1982@gmail.com

    #1

    A problem about template function overload

    the code as follows:
    #include<iostre am>
    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!

  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: A problem about template function overload

    On 2007-03-18 14:02, miaohua1982@gma il.com wrote:
    the code as follows:
    #include<iostre am>
    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

    Comment

    • miaohua1982@gmail.com

      #3
      Re: A problem about template function overload

      On 3月18æ—¥, 下午10æ—¶28åˆ †, Erik Wikström <Erik-wikst...@telia. comwrote:
      On 2007-03-18 14:02, miaohua1...@gma il.com wrote:
      >
      >
      >
      >
      >
      the code as follows:
      #include<iostre am>
      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<iostre am>
      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?

      Comment

      Working...