By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}

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

Feb 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Diwa" <sh***********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr 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::operator[] 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::operator[]),
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
Feb 7 '07 #2

P: n/a
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
Feb 7 '07 #3

P: n/a
In article <11**********************@p10g2000cwp.googlegroups .com>,
"Diwa" <sh***********@gmail.comwrote:
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
Feb 7 '07 #4

P: n/a
On Feb 7, 10:31 am, Howard Hinnant <howard.hinn...@gmail.comwrote:
In article <1170861497.738077.176...@p10g2000cwp.googlegroups .com>,

"Diwa" <shettydiwa...@gmail.comwrote:
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
Feb 7 '07 #5

P: n/a
On Feb 7, 11:03 am, "Diwa" <shettydiwa...@gmail.comwrote:
On Feb 7, 10:31 am, Howard Hinnant <howard.hinn...@gmail.comwrote:


In article <1170861497.738077.176...@p10g2000cwp.googlegroups .com>,
"Diwa" <shettydiwa...@gmail.comwrote:
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)

Feb 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.