Template typedefs - What's your work around?

The canonical way is to use a metafunction like thus: template struct my_string_map { typedef std::map type; }; // Invoke: my_string_map::type my_str_int_map This is also used in the STL ( allocator::rebind).

The canonical way is to use a metafunction like thus: template struct my_string_map { typedef std::map type; }; // Invoke: my_string_map::type my_str_int_map; This is also used in the STL (allocator::rebind) and in many libraries including Boost. We use it extensively in a bioinformatical library. It's bloated, but it's the best alternative 99% of the time.

Using macros here is not worth the many downsides. (EDIT: I've amended the code to reflect Boost/STL conventions as pointed out by Daniel in his comment. ).

1 If you use 'type' instead of 'Type' (or in addition to) this will work better with Boost.MPL. Which can be useful, so I think it's a good convention to encourage. – Daniel James Sep 16 '08 at 11:44 is this really called a metafunction?

I doubt it. – Matthieu N. Jan 24 at 1:58 @Zenikoder: It’s perfectly reasonable to call it a metafunction, and most C++ metaprogramming experts do so (e.g. The Boost people).

There doesn’t exist one rigorous definition but personally I call everything a metafunction that, at compile-time, yields a type or compile-time constant given some input. That is, any mapping from an input to an output at compile time. I don’t know that any more reasonable or more widely accepted definition exists.

Finally, can I ask why you object to this usage of the term? – Konrad Rudolph Jan 24 at 12:05.

GOTW dealt with this topic a while ago: gotw.ca/gotw/079.htm.

Template struct my_string_map : public std::map { }; You shouldn't inherit from classes that do not have a virtual destructor. It's related to destructors in derived classes not being called when they should be and you could end up with unallocated memory. That being said you could **probably** get away with it in the instance above because you're not adding any more data to your derived type.

Note that this is not an endorsement. I still advice you don't do it. The fact that you can do it doesn't mean you should.

EDIT: Yes, this is a reply to ShaChris23's post. I probably missed something because it showed up above his/her message instead of below.

– Evan Teran Jan 18 '10 at 23:06 These aren't forums, post do not appear in chronological order. Normally, this would be a comment. You cannot, so perhaps a community wiki answer would have done.

Either way, the question is quite old. – GMan Jan 18 '10 at 23:25 2 I noticed that it's quite old, but I thought it was a good thing to make sure it was not encouraging incorrect practices. Ttyl – xghost Jan 18 '10 at 23:36.

Inheritance is only then a problem if a pointer of the base class points to an object of the derived class, such as Base *p = new Derived(); However, the trick with the inheritance here is that we don't want to type too much. We either want Derived *p = new Derived(); or Derived d; In these cases the constructors are called in the right order. Here is a full code.

#include using namespace std; class Base { public: Base(){ cout Derived*\n"; Base *BasePtr = new Derived(); delete BasePtr; cout Derived*\n"; Derived *DerPtr = new Derived(); delete DerPtr; cout.

1 -1: the question is not about inheritance and your answer do not even have template nor typedef. Did you answer the wrong question? – n1ckp Sep 10 at 12:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions