Why I (heart) c++.


R. Clayton (rclayton@monmouth.edu)
10 Dec 2003 18:53:28 -0500


[ You should study this until you understand the true terribleness of it.
  Source: comp.lang.c++.moderated, 18 Nov 2003; the mailing list reference for
  this source is at the end of this message. The reference below to 23.1.1/9
  is to http://www.zib.de/benger/C++/clause23.html Scott Meyers is well known
  for his Effective C++/STL books. "TCPPPL" refers to Stroustrup's The C++
  Programming Language (a.k.a. The Thousand Pages of Bjarne); I haven't found
  the specific citation mentioned. ]

SCOTT MEYERS: "Scott Meyers" <Usenet@aristeia.com>

Color me dumb as a doorknob, but I can't figure out what's going on here.
Consider:

  void f(size_t, int); // non-template func
  template<typename T> void f(T, T); // func template

  f(10, 100); // call with two ints

All my compilers instantiate the template and call the resulting exact-match
function instead of doing the int->size_t conversion that calling the
non-template function would require. This makes sense to me.

If I do the same thing with member functions, I get the same results, which
also makes sense to me:

  template<typename T>
  class PseudoVector {
  public:
    PseudoVector(size_t, int); // non member-template func
    template<typename It> PseudoVector(It, It); // member func template
  };

  PseudoVector pv(10, 100); // call with two ints;
                                                 // instantiates the member
                                                 // template

What confuses me is that this does NOT seem to happen with std::vector. Here's
what I consider to be the relevant portion of vector's definition:

  explicit vector(size_type n, const T& value = T(), // non member-
                  const Allocator& = Allocator()); // template func

  template <class InputIterator> // member func
  vector(InputIterator first, InputIterator last, // template
         const Allocator& = Allocator());

  vector<int> v(10, 100); // call with two ints; does NOT
                             // instantiate the member template!

Can somebody please explain to why this call to a vector constructor doesn't
instantiate and call the member function template?

{ See 23.1.1/9. -mod/jep }

ALBO: Julián Albo <JULIANALBO@terra.es>

>> According TCPPPL, the standard mandates that vector does that. The way to do
>> it is left to the implementation.
 
CALCOTE: jcalcote@novell.com (John Calcote)

> I find it difficult to believe that the standard would effectively force
> compiler vendors to write code like this:
 
> if symbol is some deriviative of 'vector'
> then operate differently.

> where I come from we call this a 'dirty hook' - it's the security equivalent
> of 'if username == "fred" then grant special rights'.

KANZE: James Kanze <kanze@gabi-soft.fr>

That's not what is required. The standard does require the implementation to
recognize cases where there is an overload between:

    template< typename SomeIterator >
    xxx( SomeIterator, SomeIterator )

and

    xxx( size_t, someOtherIntegralType )

in a collection, and function as if the second is called when SomeIterator is
an integral type in the first.

Of course, that doesn't necessarily mean that the "dirty hook" name is
unjustified.

_______________________________________________
cpptips mailing list
http://cpptips.hyperformix.com



This archive was generated by hypermail 2.0b3 on Thu Dec 18 2003 - 16:15:05 EST