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;
}
// -------------------------------------------- 5 2254
"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
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
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
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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Henrik S. Hansen |
last post by:
I'm new to C++, and cannot figure out why this won't compile:
std::map<std::string, int> tst;
tst = 1;
int main() { /*...*/ }
It gives me:
error: ISO C++ forbids declaration of `tst' with...
|
by: William Payne |
last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set
(with set I mean they equal one). I need to determine those bits and their
position and create new numbers from them. For...
|
by: Generic Usenet Account |
last post by:
This is a two-part question.
(1) I have implemented a "Datastructure Registry" template class. I am
getting no compiler warnings with older compilers, but newer compilers
are generating the...
|
by: Ann Huxtable |
last post by:
I have the following code segment - which compiles fine. I'm just
worried I may get run time probs - because it looks like the functions
are being overloaded by the return types?. Is this Ok: ?
...
|
by: Helge Jensen |
last post by:
I've got some data that has Set structure, that is membership, insert
and delete is fast (O(1), hashing). I can't find a System.Collections
interface that matches the operations naturally offered...
| |
by: Georg J. Stach |
last post by:
Hello,
taken I've declared an enumeration like this:
enum Color{ red,green,blue };
The internal representation of Color is of type integer and it's no problem
to print out their values.
...
|
by: jccorreu |
last post by:
I've got to read info from multiple files that will be given to me. I
know the format and what the data is. The thing is each time we run
the program we may be using a differnt number of files,...
|
by: Roland Schwarz |
last post by:
Most probably what I am asking for already has been
answered somewhere, still I was not able to find:-(
I want to encapsulate a class ( a primitive type for
the beginning ) to behave like a...
|
by: kevineller794 |
last post by:
I want to make a split string function, but it's getting complicated.
What I want to do is make a function with a String, BeginStr and an
EndStr variable, and I want it to return it in a char...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |