473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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::Fie ldType()'
temp.cpp:7: candidates are: FieldType::Fiel dType(const FieldType&)
temp.cpp:10: FieldType::Fiel dType(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::s tring, FieldType newOrderFields; // fieldname &
its type

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

return 0;
}

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

Feb 7 '07 #1
5 2261
"Diwa" <sh***********@ gmail.comwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.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::s tring, FieldType newOrderFields; // fieldname &
its type

newOrderFields["aotag"] = FieldType('S', 4, true); // <---------
Line 2 uses non default ctor
Actually, think about what std::map::opera tor[] 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
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::Fie ldType()'
temp.cpp:7: candidates are: FieldType::Fiel dType(const FieldType&)
temp.cpp:10: FieldType::Fiel dType(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::s tring, 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
In article <11************ **********@p10g 2000cwp.googleg roups.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::Fie ldType()'
temp.cpp:7: candidates are: FieldType::Fiel dType(const FieldType&)
temp.cpp:10: FieldType::Fiel dType(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::s tring, 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
On Feb 7, 10:31 am, Howard Hinnant <howard.hinn... @gmail.comwrote :
In article <1170861497.738 077.176...@p10g 2000cwp.googleg roups.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::Fie ldType()'
temp.cpp:7: candidates are: FieldType::Fiel dType(const FieldType&)
temp.cpp:10: FieldType::Fiel dType(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::s tring, 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
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.738 077.176...@p10g 2000cwp.googleg roups.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::Fie ldType()'
temp.cpp:7: candidates are: FieldType::Fiel dType(const FieldType&)
temp.cpp:10: FieldType::Fiel dType(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::s tring, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2597
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 no type
7
3593
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
3
2952
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 following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
9
2725
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: ? template <class T1, class T2> int getValue( T1 col, T2 row ) ; template <class T1, class T2> double getValue( T1 col, T2 row ) ;
21
13810
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 Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
6
1779
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. But is it possible to get their string representing value _without_ having a
7
5542
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, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
0
993
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 lvalue, but instead of living in process memory being forwarded to some remote store. I believe an lvalue needs to be able to be convertible to its wrapped type on reading, and providing an
12
1911
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 array. For example: char teststr; strcpy(teststr, "test:blah;");
0
7883
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6677
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5842
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3876
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1492
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.