Topic: template and const version.


Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Mon, 16 Dec 2002 23:35:55 +0000 (UTC)
Raw View
""Edward Diener"" <eldiener@earthlink.net> wrote
> The return value does not come into play with overload resolution but I
> would expect the difference in the first parameter to find_all, either a
> non-const reference or a const reference to correctly determine which of
the
> two function templates will be chosen during template instantiation. So I
> think BCB5 correct in this case and VC6 incorrect.

I don't think this is right.
Unlike class templates there is no partial ordering of function templates.

Take an even simpler case:-

  template <typename T>
  void p( T & t ) {}

  int main()
  {
      const int ci = 0;
      p( ci );
  }

This is obviously valid with T = 'const int'

Adding a new template:-

template <typename U>
void p( const U & t ) {}

produces an ambiguity with U = 'int'.
I don't find anything in the standard (looking in sections 14.8.3 and then
13.3)
which resolves this ambiguity.

On a second point the original posting needs 'typename' to the return type
since
Container::iterator is a dependent name (14.6.2):

template <typename Container, typename T>
  typename  Container::iterator find_all(Container& C, const T& Value)


Roger Orr
--
MVP in C++ at www.brainbench.com



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: spambo_steffan_lankinensucks@hotmail.com ("Bo-Staffan Lankinen")
Date: Tue, 17 Dec 2002 02:20:51 +0000 (UTC)
Raw View
> I don't think this is right.
> Unlike class templates there is no partial ordering of function templates.

Yes, there is. See 14.5.5.2.

> Take an even simpler case:-
>
>   template <typename T>
>   void p( T & t ) {}
>
>   int main()
>   {
>       const int ci = 0;
>       p( ci );
>   }
>
> This is obviously valid with T = 'const int'
>
> Adding a new template:-
>
> template <typename U>
> void p( const U & t ) {}
>
> produces an ambiguity with U = 'int'.
> I don't find anything in the standard (looking in sections 14.8.3 and then
> 13.3)
> which resolves this ambiguity.

Then your compiler is broken. According to the rules for selecting the best
viable function (13.3.3), the latter template should be choosen since it's
more specialized.

Bo-Staffan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 17 Dec 2002 03:42:06 +0000 (UTC)
Raw View
Roger Orr wrote:
> Adding a new template:-

....and violating the ODR.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 17 Dec 2002 03:49:28 +0000 (UTC)
Raw View
Hyman Rosen wrote:
> ....and violating the ODR.

Oops, sorry. I was reading with only half an eye.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Tue, 17 Dec 2002 20:08:20 +0000 (UTC)
Raw View
""Bo-Staffan Lankinen"" <spambo_steffan_lankinensucks@hotmail.com> wrote
> > Unlike class templates there is no partial ordering of function
templates.
>
> Yes, there is. See 14.5.5.2.

Yes, you're right.  There's even a link in 13.3.3p1 which I missed :-(

Roger Orr
--
MVP in C++ at www.brainbench.com


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: crapaddress@rubbish.com (Jon)
Date: Fri, 6 Dec 2002 01:49:58 +0000 (UTC)
Raw View
Is it possible to have two templates like this.

template <typename Container, typename T>
  Container::iterator find_all(Container& C, const T& Value)
{
  return std::find(C.begin(), C.end(), Value);
}

// const version.

template <typename Container, typename T>
  Container::const_iterator find_all(const Container& C, const T& Value)
{
  return std::find(C.begin(), C.end(), Value);
}

CBuilder5 seems to accept these and use the right one in each case. VC++
6.0 says that the overloads are ambiguous.

Thanks,
Jonathan.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: eldiener@earthlink.net ("Edward Diener")
Date: Mon, 9 Dec 2002 23:44:37 +0000 (UTC)
Raw View
"Jon" <crapaddress@rubbish.com> wrote in message
news:MPG.185ac71d26eea637989683@news.trimble.co.nz...
> Is it possible to have two templates like this.
>
> template <typename Container, typename T>
>   Container::iterator find_all(Container& C, const T& Value)
> {
>   return std::find(C.begin(), C.end(), Value);
> }
>
> // const version.
>
> template <typename Container, typename T>
>   Container::const_iterator find_all(const Container& C, const T& Value)
> {
>   return std::find(C.begin(), C.end(), Value);
> }
>
> CBuilder5 seems to accept these and use the right one in each case. VC++
> 6.0 says that the overloads are ambiguous.

The return value does not come into play with overload resolution but I
would expect the difference in the first parameter to find_all, either a
non-const reference or a const reference to correctly determine which of the
two function templates will be chosen during template instantiation. So I
think BCB5 correct in this case and VC6 incorrect.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]