Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is default ctor reqd for "value" type of "std::map" ?

Reply
Thread Tools

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

 
 
Diwa
Guest
Posts: n/a
 
      02-07-2007
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;
}

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

 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      02-07-2007
"Diwa" <> wrote in message
news: oups.com...
> 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: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: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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-07-2007
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


 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      02-07-2007
In article < .com>,
"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
 
Reply With Quote
 
Diwa
Guest
Posts: n/a
 
      02-07-2007
On Feb 7, 10:31 am, Howard Hinnant <howard.hinn...@gmail.com> wrote:
> In article <1170861497.738077.176...@p10g2000cwp.googlegroups .com>,
>
> "Diwa" <shettydiwa...@gmail.com> 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


 
Reply With Quote
 
Diwa
Guest
Posts: n/a
 
      02-07-2007
On Feb 7, 11:03 am, "Diwa" <shettydiwa...@gmail.com> wrote:
> On Feb 7, 10:31 am, Howard Hinnant <howard.hinn...@gmail.com> wrote:
>
>
>
>
>
> > In article <1170861497.738077.176...@p10g2000cwp.googlegroups .com>,

>
> > "Diwa" <shettydiwa...@gmail.com> 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57