Click here to Skip to main content
11,637,415 members (62,532 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ STL Templates
Hello everyone,

I have the following hierarchy:

std::map ->
assoc_map ->
_tActionMap

_tActionMap maps actions (Action) with moods (Mood). The problem is: Mood has an intensity value which is mandatory, so there is no default constructor, and I don't want one.

Now, templates only compile when a specialization is created. In order to get around this, in "assoc_map" I force the passage of a default value for the mapped type so that the default constructor is not mandatory on mapped types (instead, a copy of the default value is created). Then, I overload operator[] to use the default value instead of a new instance (created with the default constructor).

This "trick" works in VS08, but I don't know if this is standard compliant. So... Is it? Will it work in other compilers?

Best regards
Posted 7-Apr-12 9:28am
Comments
Lakamraju Raghuram at 8-Apr-12 3:22am
   
i am lost ... do elaborate
AndreFratelli at 8-Apr-12 10:02am
   
I was kind of fearing that :P

std::map::operator [] does one of two things:

1) Return a reference to an existing value with the given key; or
2) Instantiate a new value using the default constructor and associate it with the key.

So what to do when a default constructor is not available? I inherited from std::map (assoc_map) and allow the passage of a default value at construction time.

Then I overload operator[] and copy the default instance instead of propagating to std::map::operator[], when the value does not exist.

This works because it's a template class and, therefore, its methods only get compiled when a specialization exists for them. What I would like to know is if this is indeed true. If an attempt exists to try to compile std::map::operator[] it will complain that the default constructor is not available, even though that does not happen in VS08.

Thank you

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It sounds like it would work, however without seeing the code I can't tell for sure. As you're a VC++ user perhaps download mingw (gcc for windows) and give it a whirl. If something compiles on VC++2005 or later and gcc 3.3 or later I'm usually happy it's standard code.

Just to satisfy my curiosity...

Is there a reason you don't want a default constructor? I assume it's something like you're using someone else's type and you've got the .h but not the .cpp.

Have you considered not deriving from a map but containing and forwarding instead? That will work on every compiler I know. You won't be able to do this if you've got existing code that wants a map but you avoid the whole "STL collections aren't meant to be derived from" panic that wells up in the souls of most experienced C++ programmers.

Anyway, I'm not sure that the weak advice of "try it on another compiler" is really a solution but it's a good way forward.

Cheers,

Ash

PS: If you can get hold of a compiler that uses the Edison Group's front end (http://www.edg.com/[^]) and it compiles on that you'll know it's standard compliant. Unfortunately all the compilers based on that I know of (Intel and Comeau) you have to pay for.

PPS: VC++2010 Intellisense uses EDG's front end. If you download VC++2010 express, stick your code in and see if you get any red wiggly lines under any part of the code. If you don't you're probably going to be okay. It's a good check anyway.
  Permalink  
Comments
AndreFratelli at 14-Apr-12 9:53am
   
My later experiments seem to suggest that the only problem will be with DLLs. It seems the compiler actually tries to compile the templates when they are exported in a DLL. Unfortunately, this really is to export in a DLL.

Anyway, the conditions have changed and I can't even use STL at all! It seems some target platforms do not support it, don't really know which. That said, I wrote my own implementation of red-black trees and I'm using them in the slots of an hash table (it's even more efficient). But I'll take a look at those compilers ;)

Actually the class being mapped was written by me. I usually insist in designing my apps as well as I can and I believe that a default constructor should not be there when a class shouldn't support default construction. This is the case. It has a mandatory argument. Of course I could work around this and allow default construction and then allow setting the value with a setter. But then again, it would allow default construction =) it's just a design issue.

Thank you for your reply!
Regards
Aescleal at 14-Apr-12 14:04pm
   
I completely agree that classes shouldn't have default anything unless there's a default state they can be in that doesn't break their invariants.

Anyway I'm glad you've got a workable solution to your problem, if you're a good enough programmer to hack out a red/black tree implementation you're better than most!

Cheers,

Ash
AndreFratelli at 14-Apr-12 14:46pm
   
Appreciated ;)

Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 304
1 OriginalGriff 295
2 stibee 288
3 jyo.net 270
4 himanshu agarwal 184
0 OriginalGriff 9,031
1 Sergey Alexandrovich Kryukov 8,643
2 Mika Wendelius 6,959
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web02 | 2.8.150728.1 | Last Updated 14 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100