Is default ctor reqd for "value" type of "std::map" ?

Discussion in 'C++' started by Diwa, Feb 7, 2007.

  1. Diwa

    Diwa Guest

    Does the "value" type (value as in key-value pair )of "std::map"
    require a default ctor even if it is not used ?

    If I comment out Line 1 in the code attached later,
    i.e remove the default ctor of "value" type of map,
    I get the following error:

    // --------------------------------------------
    /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for
    call to `FieldType::FieldType()'
    temp.cpp:7: candidates are: FieldType::FieldType(const FieldType&)
    temp.cpp:10: FieldType::FieldType(char, int, bool)
    // --------------------------------------------

    // --------------------------------------------
    #include <map>
    #include <string>
    // --------------------------------------------

    class FieldType
    {
    public:

    FieldType( char oType, int oLen, bool oReqd ): type(oType),
    len(oLen),
    reqd(oReqd)
    {
    }

    FieldType(){} // <-------- Line 1

    private:
    char type;
    int len;
    bool reqd;
    };

    // --------------------------------------------

    int main()
    {
    std::map<std::string, FieldType> newOrderFields; // fieldname &
    its type

    newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
    Line 2 uses non default ctor

    return 0;
    }

    // --------------------------------------------
     
    Diwa, Feb 7, 2007
    #1
    1. Advertising

  2. "Diwa" <> wrote in message
    news:...
    > Does the "value" type (value as in key-value pair )of "std::map"
    > require a default ctor even if it is not used ?


    It is used, you just don't see it being used by the mere use of a std::map
    :). As it is used, it is also needed.


    > int main()
    > {
    > std::map<std::string, FieldType> newOrderFields; // fieldname &
    > its type
    >
    > newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
    > Line 2 uses non default ctor


    Actually, think about what std::map::eek:perator[] does. It needs to return a
    reference to the value. But the value doesn't yet exist, so it has to create
    one before it is able to return a reference to it. What your line of code
    does is create a default-constructed FieldType (inside map::eek:perator[]),
    which is then overwritten with a temporary constructed FieldType (your
    FieldType('S', 4, true) expression) by the use of the (auto-generated)
    assignment operator.

    - Sylvester
     
    Sylvester Hesp, Feb 7, 2007
    #2
    1. Advertising

  3. Diwa wrote:
    > Does the "value" type (value as in key-value pair )of "std::map"
    > require a default ctor even if it is not used ?


    Yes, if you use the indexing operator, like you did below. You,
    however, don't have to use it, if you restrict yourself to using
    'insert' and 'find' instead.

    >
    > If I comment out Line 1 in the code attached later,
    > i.e remove the default ctor of "value" type of map,
    > I get the following error:
    >
    > // --------------------------------------------
    > /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for
    > call to `FieldType::FieldType()'
    > temp.cpp:7: candidates are: FieldType::FieldType(const FieldType&)
    > temp.cpp:10: FieldType::FieldType(char, int, bool)
    > // --------------------------------------------
    >
    > // --------------------------------------------
    > #include <map>
    > #include <string>
    > // --------------------------------------------
    >
    > class FieldType
    > {
    > public:
    >
    > FieldType( char oType, int oLen, bool oReqd ): type(oType),
    > len(oLen),
    > reqd(oReqd)
    > {
    > }
    >
    > FieldType(){} // <-------- Line 1
    >
    > private:
    > char type;
    > int len;
    > bool reqd;
    > };
    >
    > // --------------------------------------------
    >
    > int main()
    > {
    > std::map<std::string, FieldType> newOrderFields; // fieldname &
    > its type
    >
    > newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
    > Line 2 uses non default ctor


    No, it uses the default c-tor and the copy-assignment operator.
    Step through the [library] code to see what's going on there.

    >
    > return 0;
    > }
    >
    > // --------------------------------------------


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 7, 2007
    #3
  4. In article <>,
    "Diwa" <> wrote:

    > Does the "value" type (value as in key-value pair )of "std::map"
    > require a default ctor even if it is not used ?


    If it is known at compile time that the default constructor is not used,
    then it is not required.

    > If I comment out Line 1 in the code attached later,
    > i.e remove the default ctor of "value" type of map,
    > I get the following error:
    >
    > // --------------------------------------------
    > /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for
    > call to `FieldType::FieldType()'
    > temp.cpp:7: candidates are: FieldType::FieldType(const FieldType&)
    > temp.cpp:10: FieldType::FieldType(char, int, bool)
    > // --------------------------------------------
    >
    > // --------------------------------------------
    > #include <map>
    > #include <string>
    > // --------------------------------------------
    >
    > class FieldType
    > {
    > public:
    >
    > FieldType( char oType, int oLen, bool oReqd ): type(oType),
    > len(oLen),
    > reqd(oReqd)
    > {
    > }
    >
    > FieldType(){} // <-------- Line 1
    >
    > private:
    > char type;
    > int len;
    > bool reqd;
    > };
    >
    > // --------------------------------------------
    >
    > int main()
    > {
    > std::map<std::string, FieldType> newOrderFields; // fieldname &
    > its type
    >
    > newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
    > Line 2 uses non default ctor
    >
    > return 0;
    > }
    >
    > // --------------------------------------------


    In this example FieldType() is used (and required). The operator[] of
    map will default construct FieldType() (if at run time "aotag" is found
    not to be in the map). This default constructed object is then assigned
    into.

    -Howard
     
    Howard Hinnant, Feb 7, 2007
    #4
  5. Diwa

    Diwa Guest

    On Feb 7, 10:31 am, Howard Hinnant <> wrote:
    > In article <>,
    >
    > "Diwa" <> wrote:
    > > Does the "value" type (value as in key-value pair )of "std::map"
    > > require a default ctor even if it is not used ?

    >
    > If it is known at compile time that the default constructor is not used,
    > then it is not required.
    >
    >
    >
    >
    >
    > > If I comment out Line 1 in the code attached later,
    > > i.e remove the default ctor of "value" type of map,
    > > I get the following error:

    >
    > > // --------------------------------------------
    > > /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for
    > > call to `FieldType::FieldType()'
    > > temp.cpp:7: candidates are: FieldType::FieldType(const FieldType&)
    > > temp.cpp:10: FieldType::FieldType(char, int, bool)
    > > // --------------------------------------------

    >
    > > // --------------------------------------------
    > > #include <map>
    > > #include <string>
    > > // --------------------------------------------

    >
    > > class FieldType
    > > {
    > > public:

    >
    > > FieldType( char oType, int oLen, bool oReqd ): type(oType),
    > > len(oLen),
    > > reqd(oReqd)
    > > {
    > > }

    >
    > > FieldType(){} // <-------- Line 1

    >
    > > private:
    > > char type;
    > > int len;
    > > bool reqd;
    > > };

    >
    > > // --------------------------------------------

    >
    > > int main()
    > > {
    > > std::map<std::string, FieldType> newOrderFields; // fieldname &
    > > its type

    >
    > > newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
    > > Line 2 uses non default ctor

    >
    > > return 0;
    > > }

    >
    > > // --------------------------------------------

    >
    > In this example FieldType() is used (and required). The operator[] of
    > map will default construct FieldType() (if at run time "aotag" is found
    > not to be in the map). This default constructed object is then assigned
    > into.
    >


    Thanks Sylvester, Victor and Howard for the nice explanations. It
    confirmed what I thought might be the issue. I think I am better
    off using the "insert( )" then.

    Thanks,
    Diwakar
     
    Diwa, Feb 7, 2007
    #5
  6. Diwa

    Diwa Guest

    On Feb 7, 11:03 am, "Diwa" <> wrote:
    > On Feb 7, 10:31 am, Howard Hinnant <> wrote:
    >
    >
    >
    >
    >
    > > In article <>,

    >
    > > "Diwa" <> wrote:
    > > > Does the "value" type (value as in key-value pair )of "std::map"
    > > > require a default ctor even if it is not used ?

    >
    > > If it is known at compile time that the default constructor is not used,
    > > then it is not required.

    >
    > > > If I comment out Line 1 in the code attached later,
    > > > i.e remove the default ctor of "value" type of map,
    > > > I get the following error:

    >
    > > > // --------------------------------------------
    > > > /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for
    > > > call to `FieldType::FieldType()'
    > > > temp.cpp:7: candidates are: FieldType::FieldType(const FieldType&)
    > > > temp.cpp:10: FieldType::FieldType(char, int, bool)
    > > > // --------------------------------------------

    >
    > > > // --------------------------------------------
    > > > #include <map>
    > > > #include <string>
    > > > // --------------------------------------------

    >
    > > > class FieldType
    > > > {
    > > > public:

    >
    > > > FieldType( char oType, int oLen, bool oReqd ): type(oType),
    > > > len(oLen),
    > > > reqd(oReqd)
    > > > {
    > > > }

    >
    > > > FieldType(){} // <-------- Line 1

    >
    > > > private:
    > > > char type;
    > > > int len;
    > > > bool reqd;
    > > > };

    >
    > > > // --------------------------------------------

    >
    > > > int main()
    > > > {
    > > > std::map<std::string, FieldType> newOrderFields; // fieldname &
    > > > its type

    >
    > > > newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
    > > > Line 2 uses non default ctor

    >
    > > > return 0;
    > > > }

    >
    > > > // --------------------------------------------

    >
    > > In this example FieldType() is used (and required). The operator[] of
    > > map will default construct FieldType() (if at run time "aotag" is found
    > > not to be in the map). This default constructed object is then assigned
    > > into.

    >
    > Thanks Sylvester, Victor and Howard for the nice explanations. It
    > confirmed what I thought might be the issue. I think I am better
    > off using the "insert( )" then.
    >


    I guess even with "map.insert()" I still have have to write the
    default
    ctor. This is because other places using "[]" to view data (not for
    inserting) wont work (due to the run time - compile time thing
    explained by others earlier)
     
    Diwa, Feb 7, 2007
    #6
    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. Apricot
    Replies:
    4
    Views:
    560
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    520
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    514
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    432
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. puzzlecracker
    Replies:
    8
    Views:
    454
    James Kanze
    Apr 15, 2008
Loading...

Share This Page