A problem of T * const &

Discussion in 'C++' started by miaohua1982@gmail.com, Nov 30, 2006.

  1. Guest

    the program is as follows:
    #include <vector>
    using namespace std;

    class A{};
    int main()
    {
    A* const &p = NULL;
    vector<A*> B(3,NULL); //there is a compile error
    B.push_back(NULL);
    return 0;
    }

    int VC7£¬the error is
    d:\Microsoft Visual Studio .NET 2003\Vc7\include\vector(357): error
    C2664: "std::vector<_Ty>::_Construct_n" : can't convert from "int"
    to "A *const & "
    with
    [
    _Ty=A *
    ]
    which puzzles me is that , why this one"A* const &p = NULL;" is OK,
    the compiler even doesn't give a warning?!
     
    , Nov 30, 2006
    #1
    1. Advertising

  2. Guest

    > A* const &p = NULL;
    > vector<A*> B(3,NULL); //there is a compile error


    Short: Well, "NULL" is not of value_type "A*"!
    Just write: vector<A*> B(3,(A*)NULL);

    Long: I'm not an C++ standard guru but I guess because the vector (a
    template) isn't created yet the compiler tries to match the correct
    ctor but doesn't find it because 'NULL' will be treaded as type int
    (see your warning)! (Is a difference to a class myvector - no template
    - with a ctor myvector(A*) throwing no warning!)
     
    , Nov 30, 2006
    #2
    1. Advertising

  3. <> schrieb im Newsbeitrag
    news:...
    >> A* const &p = NULL;

    what's this? call by reference which is null assigned const on a A* what
    ever...
    >> vector<A*> B(3,NULL); //there is a compile error

    >
    > Short: Well, "NULL" is not of value_type "A*"!
    > Just write: vector<A*> B(3,(A*)NULL);

    well an (A*)NULL looks like a c-style typecast to me, hm?
    maybe the fist line is off use...
     
    Oliver Bleckmann, Nov 30, 2006
    #3
  4. Earl Purple Guest

    wrote:
    > the program is as follows:
    > #include <vector>
    > using namespace std;
    >
    > class A{};
    > int main()
    > {
    > A* const &p = NULL;
    > vector<A*> B(3,NULL); //there is a compile error
    > B.push_back(NULL);
    > return 0;
    > }
    >
    > int VC7£¬the error is
    > d:\Microsoft Visual Studio .NET 2003\Vc7\include\vector(357): error
    > C2664: "std::vector<_Ty>::_Construct_n" : can't convert from "int"
    > to "A *const & "
    > with
    > [
    > _Ty=A *
    > ]
    > which puzzles me is that , why this one"A* const &p = NULL;" is OK,
    > the compiler even doesn't give a warning?!


    The error is not in the line you have indicated, it is in the one
    above. A reference must be bound to a variable, and p is not.

    It's like writing doing

    int const & x = 0;

    which is not valid as 0 is not a variable. Perhaps the fact that it is
    const (and therefore does not need to be an l-value) has confused you.

    (Compilers ranting on about "int" for undefined types by the way is one
    of my biggest annoyances. "Literal" would be better because NULL which
    is 0 is also a pointer).
     
    Earl Purple, Nov 30, 2006
    #4
  5. * Earl Purple:
    > wrote:
    >> the program is as follows:
    >> #include <vector>
    >> using namespace std;
    >>
    >> class A{};
    >> int main()
    >> {
    >> A* const &p = NULL;
    >> vector<A*> B(3,NULL); //there is a compile error
    >> B.push_back(NULL);
    >> return 0;
    >> }
    >>
    >> int VC7,the error is
    >> d:\Microsoft Visual Studio .NET 2003\Vc7\include\vector(357): error
    >> C2664: "std::vector<_Ty>::_Construct_n" : can't convert from "int"
    >> to "A *const & "
    >> with
    >> [
    >> _Ty=A *
    >> ]
    >> which puzzles me is that , why this one"A* const &p = NULL;" is OK,
    >> the compiler even doesn't give a warning?!

    >
    > The error is not in the line you have indicated, it is in the one
    > above. A reference must be bound to a variable, and p is not.
    >
    > It's like writing doing
    >
    > int const & x = 0;
    >
    > which is not valid as 0 is not a variable.


    It's valid. A reference to const can be bound to an rvalue.

    The error is in using plain 0 (which is what NULL is, NULL is not a
    pointer) as a default value.

    Some compilers may accept this (MSVC 7.1 does) but that's just bad luck.
    Because the standard requires in §23.1.1/9 that a call of the
    templated constructor taking two iterators shall have the same effect as
    that call with the arguments casted to the vector's size_type, if the
    iterator type is an integral type, and when NULL is defined simply as 0
    instead of as 0L the above ends up in that case with the iterator type
    as int. To do it properly, cast the default value to A*, like 'B(3,
    (A*)NULL)'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 30, 2006
    #5
  6. Guest

    "Oliver Bleckmann дµÀ£º
    "
    > <> schrieb im Newsbeitrag
    > news:...
    > >> A* const &p = NULL;

    > what's this? call by reference which is null assigned const on a A* what
    > ever...
    > >> vector<A*> B(3,NULL); //there is a compile error

    > >
    > > Short: Well, "NULL" is not of value_type "A*"!
    > > Just write: vector<A*> B(3,(A*)NULL);

    > well an (A*)NULL looks like a c-style typecast to me, hm?
    > maybe the fist line is off use...
     
    , Nov 30, 2006
    #6
  7. Earl Purple:

    > The error is not in the line you have indicated, it is in the one
    > above. A reference must be bound to a variable, and p is not.
    >
    > It's like writing doing
    >
    > int const & x = 0;
    >
    > which is not valid as 0 is not a variable.



    You might want to try compile that -- you'll see that a "reference to const"
    can be bound to an R-value. The language provides special rules for this.

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 30, 2006
    #7
  8. Guest

    > news:...
    > >> A* const &p = NULL;

    > what's this? call by reference which is null assigned const on a A* what
    > ever...

    this is a "A*" const reference, and the "NULL" is defined as 0,so a
    const reference to rvalue is valid

    "Oliver Bleckmann дµÀ£º
    "
    > <> schrieb im Newsbeitrag
    > news:...
    > >> A* const &p = NULL;

    > what's this? call by reference which is null assigned const on a A* what
    > ever...
    > >> vector<A*> B(3,NULL); //there is a compile error

    > >
    > > Short: Well, "NULL" is not of value_type "A*"!
    > > Just write: vector<A*> B(3,(A*)NULL);

    > well an (A*)NULL looks like a c-style typecast to me, hm?
    > maybe the fist line is off use...
     
    , Nov 30, 2006
    #8
  9. :

    > vector<A*> B(3,NULL);



    The Standard necessitates that NULL must be a macro which expands to a
    compile-time integer type whose value is 0. Examples are:

    #define NULL 0

    #define NULL 0L

    #define NULL '\0'

    #define NULL (6 - 4 - 2)

    I don't know the exact templates rules, but it seems the compiler is unhappy
    with having an integer type where it should have an A*.

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 30, 2006
    #9
  10. Guest

    Thank you!Maybe I've understand.
    Form cpp2003 23.1.1./10
    [Note: This follows directly from the requirements in the Iterator
    Requirements Table. Integral types cannot be iterators, so, if n1 and
    n2 are values of an integral type N, the expression X(n1, n2) cannot
    possibly be interpreted as construction from a range of iterators. It
    must be taken to mean the first constructor in the Iterator
    Requirements Table, not the second one. If there is no conversion from
    N to X::value_type, then this is not a valid

    ~~~~~~~~~~~~~the NULL is define ,so the "N" is int ,int can't convert
    to A*. Am I right??
    expression at all.


    "Alf P. Steinbach дµÀ£º
    "
    > * Earl Purple:
    > > wrote:
    > >> the program is as follows:
    > >> #include <vector>
    > >> using namespace std;
    > >>
    > >> class A{};
    > >> int main()
    > >> {
    > >> A* const &p = NULL;
    > >> vector<A*> B(3,NULL); //there is a compile error
    > >> B.push_back(NULL);
    > >> return 0;
    > >> }
    > >>
    > >> int VC7,the error is
    > >> d:\Microsoft Visual Studio .NET 2003\Vc7\include\vector(357): error
    > >> C2664: "std::vector<_Ty>::_Construct_n" : can't convert from "int"
    > >> to "A *const & "
    > >> with
    > >> [
    > >> _Ty=A *
    > >> ]
    > >> which puzzles me is that , why this one"A* const &p = NULL;" is OK,
    > >> the compiler even doesn't give a warning?!

    > >
    > > The error is not in the line you have indicated, it is in the one
    > > above. A reference must be bound to a variable, and p is not.
    > >
    > > It's like writing doing
    > >
    > > int const & x = 0;
    > >
    > > which is not valid as 0 is not a variable.

    >
    > It's valid. A reference to const can be bound to an rvalue.
    >
    > The error is in using plain 0 (which is what NULL is, NULL is not a
    > pointer) as a default value.
    >
    > Some compilers may accept this (MSVC 7.1 does) but that's just bad luck.
    > Because the standard requires in ¡ì23.1.1/9 that a call of the
    > templated constructor taking two iterators shall have the same effect as
    > that call with the arguments casted to the vector's size_type, if the
    > iterator type is an integral type, and when NULL is defined simply as 0
    > instead of as 0L the above ends up in that case with the iterator type
    > as int. To do it properly, cast the default value to A*, like 'B(3,
    > (A*)NULL)'.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    , Nov 30, 2006
    #10
    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. Replies:
    11
    Views:
    1,109
  2. Javier
    Replies:
    2
    Views:
    568
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,124
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    542
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page