Re: Calling container constructor explicitly
* 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<int> v;
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, int> MapType;
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?