473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calling container constructor explicitly

I'm trying to write some code that calls the constructors of STL
containers explicitly, and I can't get it to compile. A sample program
is below. One compiler complains about the last two lines (the map
constructor calls), saying that I'm trying to take the address of a
constructor. Another compiler complains about all four of the last
lines, just saying "invalid use of class". What is the correct syntax
for calling a container constructor explicitly?

You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.

Thanks for the help!

- Dave
#include <vector>
#include <map>
#include <cstdlib>

int
main()
{
typedef std::vector<int VecType;
typedef std::map<int, intMapType;

VecType v, *vp;
MapType m, *mp;

vp = (VecType *)malloc(sizeof(VecType));
mp = (MapType *)malloc(sizeof(MapType));

// call constructors
v.VecType::vector();
vp->VecType::vector();
m.MapType::map();
mp->MapType::map();
}

Jul 16 '06 #1
6 2437
* daveb:
I'm trying to write some code that calls the constructors of STL
containers explicitly, and I can't get it to compile.
The following is an implicit constructor call ("implicit" means "implied
or understood though not directly expressed"):

std::vector<intv;

The following is an explicit constructor call ("explicit" means "fully
and clearly expressed; leaving nothing implied"):

std::vector<int>();

The last one creates a temporary; see the FAQ.

A sample program
is below. One compiler complains about the last two lines (the map
constructor calls), saying that I'm trying to take the address of a
constructor. Another compiler complains about all four of the last
lines, just saying "invalid use of class". What is the correct syntax
for calling a container constructor explicitly?
In general, supplying a constructor argument list after a type
specification is the correct syntax for calling a constructor
explicitly. But that's not a formal view: it's just a summary of the
effect of the formal syntax rules. Also, note that the opposite does
not hold: int() constructs the int value 0, but int has no constructor.

You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.
Placement new is exactly for that.

But it's a rather silly thing, nay, STRONGERWORD thing, to do.

A vector object by itself takes up just a few bytes. The contents are
typically allocated separately. To take charge of the allocation for
vector contents you need to specify an allocator type in the type
definition.

#include <vector>
#include <map>
#include <cstdlib>

int
main()
{
typedef std::vector<int VecType;
typedef std::map<int, intMapType;

VecType v, *vp;
At this point v has been constructed and vp is uninitialized pointer.

MapType m, *mp;

vp = (VecType *)malloc(sizeof(VecType));
mp = (MapType *)malloc(sizeof(MapType));

// call constructors
v.VecType::vector();
vp->VecType::vector();
Syntax error.

The way to call constructors on raw storage is to use placement new, but
see above: it's STRONGERWORD for std::vector, check out allocators.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 16 '06 #2
daveb wrote:
What is the correct syntax for calling a container constructor explicitly?
The same as for any other class: none.
You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.
Strange. "placement new" is habitually used for that.

If you want that the contained objects, not the container object itself, use
your allocation function, write an adequate allocator and use it.

--
Salu2
Jul 16 '06 #3
daveb wrote:
I'm trying to write some code that calls the constructors of STL
containers explicitly, and I can't get it to compile. A sample program
is below. One compiler complains about the last two lines (the map
constructor calls), saying that I'm trying to take the address of a
constructor. Another compiler complains about all four of the last
lines, just saying "invalid use of class". What is the correct syntax
for calling a container constructor explicitly?
That depends on what you mean by calling the constructor explictly. The
standard reserves this language for the function notation of a type
conversion like

std::string( "hello world" );

Despite that, placement new is what is semantically closest to calling a
constructor.
You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.
Huh? Isn't this exactly what placement new is doing: construct an object at
a specified location in memory by invoking the constructor appropriate for
the given arguments?
>
Thanks for the help!

- Dave
#include <vector>
#include <map>
#include <cstdlib>

int
main()
{
typedef std::vector<int VecType;
typedef std::map<int, intMapType;

VecType v, *vp;
MapType m, *mp;

vp = (VecType *)malloc(sizeof(VecType));
mp = (MapType *)malloc(sizeof(MapType));

// call constructors
v.VecType::vector();
vp->VecType::vector();
m.MapType::map();
mp->MapType::map();
}

That won't fly as constructors don't have names, are not found by name
lookup and therefore cannot be called like member functions.

What about:

VecType v;
VecType* vp = (VecType *)malloc(sizeof(VecType));
new ( vp ) VecType ();

MapType m;
MapType* mp = (MapType *)malloc(sizeof(MapType));
new ( mp ) MapType ();
Best

Kai-Uwe
Jul 16 '06 #4
daveb wrote:
>
You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.
I'm not sure why you jump to the conclusion placement new will not
work. It appears to be exactly what you want.

You can't call constructors PERIOD. The best you can do is create
objects with a different allocation function (i.e., placement new).

You do know that the memory used by a standard container itself
is tiny compared with the memory for the contained objects which
you probably need to write an allocation function for as well.
Jul 16 '06 #5
My thanks to everyone who responded. I was under the impression that
placement new could be used only to call an object's *default*
constructor. But I infer from the code that Kai-Uwe provided that
parameterized constructors can be called by providing the parameters
within parentheses at the end of the statement. If so, (as most of you
noted) that's exactly what I need.

BTW, this is embedded firmware. I need the proprietary memory
allocator because the container and its contents must reside in a
"persistent" area of memory that is not initialized at boot time. I
already intended to provide a custom allocator to the container, but
this custom allocator will be called only for the *contents* of the
container, correct? I also need the container itself to live in the
persistent region of memory.

- Dave

Jul 17 '06 #6
* Julián Albo:
daveb wrote:
>What is the correct syntax for calling a container constructor explicitly?

The same as for any other class: none.
Do not perpetuate urban myths, please.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christian Dieterich | last post by:
Hi, I need to create many instances of a class D that inherits from a class B. Since the constructor of B is expensive I'd like to execute it only if it's really unavoidable. Below is an example...
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
1
by: JezB | last post by:
In my Page_Load event of all my web app Pages and UserControls I instantiate the same library class "TextResources" (to take care of reading presentation text strings from localizable resource...
2
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
2
by: mikepolitowski | last post by:
Hi folks, I am have been trying to solve this problem for quite some time now and would appreciate any advice. I have been trying to call a code-behind function that is defined in my aspx.cs...
80
by: Boltar | last post by:
Hi I need to store a number of integer values which I will then search on later to see if they exist in my container. Can someone tell me which container would be quickest for finding these...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.