vector ctor passed with two arguments of same type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    vector ctor passed with two arguments of same type

    I am copying the following text as it is from Stroustrup's TC++PL 3rd
    edition, page 450.

    It says:

    "Note that a constructor given two arguments of the same type can be a
    match for more than one constructor. For example,

    vector<intv(10, 50); // (size, value) or (iterator1, iterator2)?

    The problem is that the declaration

    template <class Invector<In first, In last, const A& = A());

    doesn't actually say that 'In' must be an input iterator. The
    declaration specifies only that the constructor's two first arguments
    must be of the same type. The unfortunate consequence is that v's
    declaration causes compile-time or link-time errors."

    My Doubt:
    ---------------
    I ran the following program with v(10, 50). It didn't give any
    compilation or linker error. Instead it printed ten 50s. I am using g+
    + 3.4.3

    Is it wrong with the compiler or my understanding that 'In' should be
    an input iterator is wrong. ? Kindly clarify.

    #include <cstdlib>
    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<intv(10, 50);

    for (vector<int>::c onst_iterator cit = v.begin(); cit !=
    v.end(); ++cit)
    cout << *cit << endl;

    return EXIT_SUCCESS;
    }

    Thanks
    V.Subramanian
  • Kai-Uwe Bux

    #2
    Re: vector ctor passed with two arguments of same type

    subramanian100i n@yahoo.com, India wrote:
    I am copying the following text as it is from Stroustrup's TC++PL 3rd
    edition, page 450.
    >
    It says:
    >
    "Note that a constructor given two arguments of the same type can be a
    match for more than one constructor. For example,
    >
    vector<intv(10, 50); // (size, value) or (iterator1, iterator2)?
    >
    The problem is that the declaration
    >
    template <class Invector<In first, In last, const A& = A());
    >
    doesn't actually say that 'In' must be an input iterator. The
    declaration specifies only that the constructor's two first arguments
    must be of the same type. The unfortunate consequence is that v's
    declaration causes compile-time or link-time errors."
    >
    My Doubt:
    ---------------
    I ran the following program with v(10, 50). It didn't give any
    compilation or linker error. Instead it printed ten 50s. I am using g+
    + 3.4.3
    >
    Is it wrong with the compiler or my understanding that 'In' should be
    an input iterator is wrong. ? Kindly clarify.
    >
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    >
    using namespace std;
    >
    int main()
    {
    vector<intv(10, 50);
    >
    for (vector<int>::c onst_iterator cit = v.begin(); cit !=
    v.end(); ++cit)
    cout << *cit << endl;
    >
    return EXIT_SUCCESS;
    }
    This is covered by the standard [23.1.1/9]:

    For every sequence defined in this clause and in clause 21:
    ? the constructor

    template <class InputIterator>
    X(InputIterator f, InputIterator l, const Allocator& a = Allocator())

    shall have the same effect as:

    X(static_cast<t ypename X::size_type>(f ),
    static_cast<typ ename X::value_type>( l),
    a)

    if InputIterator is an integral type.

    ...


    Best

    Kai-Uwe Bux

    Comment

    • Barry

      #3
      Re: vector ctor passed with two arguments of same type

      subramanian100i n@yahoo.com, India wrote:
      I am copying the following text as it is from Stroustrup's TC++PL 3rd
      edition, page 450.
      >
      It says:
      >
      "Note that a constructor given two arguments of the same type can be a
      match for more than one constructor. For example,
      >
      vector<intv(10, 50); // (size, value) or (iterator1, iterator2)?
      >
      The problem is that the declaration
      >
      template <class Invector<In first, In last, const A& = A());
      >
      doesn't actually say that 'In' must be an input iterator. The
      declaration specifies only that the constructor's two first arguments
      must be of the same type. The unfortunate consequence is that v's
      declaration causes compile-time or link-time errors."
      >
      I checked my TC++PL 3rd Special Edition,
      I seems quite different from your quote.
      My Doubt:
      ---------------
      I ran the following program with v(10, 50). It didn't give any
      compilation or linker error. Instead it printed ten 50s. I am using g+
      + 3.4.3
      >
      Is it wrong with the compiler or my understanding that 'In' should be
      an input iterator is wrong. ? Kindly clarify.
      >
      #include <cstdlib>
      #include <iostream>
      #include <vector>
      >
      using namespace std;
      >
      int main()
      {
      vector<intv(10, 50);
      >
      for (vector<int>::c onst_iterator cit = v.begin(); cit !=
      v.end(); ++cit)
      cout << *cit << endl;
      >
      return EXIT_SUCCESS;
      }
      >
      I think the rule here is that
      non-template function is more viable than template function.
      see 10.3

      Comment

      • James Kanze

        #4
        Re: vector ctor passed with two arguments of same type

        On Apr 29, 9:47 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
        subramanian10.. .@yahoo.com, India wrote:
        I am copying the following text as it is from Stroustrup's TC++PL 3rd
        edition, page 450.
        It says:
        "Note that a constructor given two arguments of the same
        type can be a match for more than one constructor. For
        example,
        vector<intv(10, 50); // (size, value) or (iterator1, iterator2)?
        The problem is that the declaration
        template <class Invector<In first, In last, const A& = A());
        doesn't actually say that 'In' must be an input iterator.
        The declaration specifies only that the constructor's two
        first arguments must be of the same type. The unfortunate
        consequence is that v's declaration causes compile-time or
        link-time errors."
        My Doubt:
        ---------------
        I ran the following program with v(10, 50). It didn't give
        any compilation or linker error. Instead it printed ten 50s.
        I am using g+ + 3.4.3
        Is it wrong with the compiler or my understanding that 'In'
        should be an input iterator is wrong. ? Kindly clarify.
        #include <cstdlib>
        #include <iostream>
        #include <vector>
        using namespace std;
        int main()
        {
        vector<intv(10, 50);
        for (vector<int>::c onst_iterator cit = v.begin(); cit !=
        v.end(); ++cit)
        cout << *cit << endl;
        return EXIT_SUCCESS;
        }
        This is covered by the standard [23.1.1/9]:
        For every sequence defined in this clause and in clause 21:
        ? the constructor
        template <class InputIterator>
        X(InputIterator f, InputIterator l, const Allocator& a = Allocator())
        shall have the same effect as:
        X(static_cast<t ypename X::size_type>(f ),
        static_cast<typ ename X::value_type>( l),
        a)
        if InputIterator is an integral type.
        Two additional remarks:

        -- This is a "correction " of the C++ committee to the original
        STL. I think it was added toward the end of the
        standardization process, and Stroustrup's text may be
        talking about an earlier version, where you did have to cast
        the first argument to a size_t for the correct constructor
        to be called.

        -- The "correction " also allows things like:
        std::vector< std::vector< int v( 10, 50 ) ;
        because the "effect" is described using an explicit
        conversion (static_cast) rather than an implicit one. The
        consensus of the committe is that this was more than was
        intended, and the next version of the standard has been
        reworded so that the "effect" depends on an implicit
        conversion (and the preceding line is illegal). (On the
        other hand, the new wording will allow something like:
        std::vector< double v( 3.5, 5.0 ) ;
        which is currently still illegal.)

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • subramanian100in@yahoo.com, India

          #5
          Re: vector ctor passed with two arguments of same type

          * Barry <dhb2...@gmail. comwrote:
          subramanian10.. .@yahoo.com, India wrote:
          I am copying the following text as it is from Stroustrup's TC++PL 3rd
          edition, page 450.
          >
          It says:
          >
          "Note that a constructor given two arguments of the same type can be a
          match for more than one constructor. For example,
          >
          vector<intv(10, 50); // (size, value) or (iterator1, iterator2)?
          >
          The problem is that the declaration
          >
          template <class Invector<In first, In last, const A& = A());
          >
          doesn't actually say that 'In' must be an input iterator. The
          declaration specifies only that the constructor's two first arguments
          must be of the same type. The unfortunate consequence is that v's
          declaration causes compile-time or link-time errors."
          >
          I checked my TC++PL 3rd Special Edition,
          I seems quite different from your quote.
          I am using TC++PL 3rd Edition. You are using TC++PL Special 3rd
          edition. I do not have TC++PL SPECIAL 3rd edition. There may be some
          differences between these two editions.

          Thanks
          V.Subramanian

          Comment

          • Bo Persson

            #6
            Re: vector ctor passed with two arguments of same type

            subramanian100i n@yahoo.com wrote:
            * Barry <dhb2...@gmail. comwrote:
            >subramanian10. ..@yahoo.com, India wrote:
            >>I am copying the following text as it is from Stroustrup's TC++PL
            >>3rd edition, page 450.
            >>
            >>It says:
            >>
            >>"Note that a constructor given two arguments of the same type can
            >>be a match for more than one constructor. For example,
            >>
            >>vector<intv(1 0, 50); // (size, value) or (iterator1,
            >>iterator2)?
            >>
            >>The problem is that the declaration
            >>
            >>template <class Invector<In first, In last, const A& = A());
            >>
            >>doesn't actually say that 'In' must be an input iterator. The
            >>declaration specifies only that the constructor's two first
            >>arguments must be of the same type. The unfortunate consequence
            >>is that v's declaration causes compile-time or link-time errors."
            >>
            >I checked my TC++PL 3rd Special Edition,
            >I seems quite different from your quote.
            >
            I am using TC++PL 3rd Edition. You are using TC++PL Special 3rd
            edition. I do not have TC++PL SPECIAL 3rd edition. There may be some
            differences between these two editions.
            >
            It is actually 3rd Edition and Special Edition (with extra chapters),
            not a combination. However, there are 15 to 20 printings of each, with
            different "correction s" to the text and the language.



            I have the 5th printing of the 3rd edition :-), where it says that the
            library should just make it work.


            Bo Persson




            Comment

            Working...