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

D

Diwa

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;
}

// --------------------------------------------
 
S

Sylvester Hesp

Diwa said:
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
 
V

Victor Bazarov

Diwa said:
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
 
H

Howard Hinnant

"Diwa said:
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
 
D

Diwa

Diwa said:
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
 
D

Diwa

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top