These are phantom type parameters, that is, parameters of a parameterised type that are used not for their representation, but to separate different “spaces” of types with the same representation.
And speaking of spaces, that’s a useful application of phantom types:
template<typename Space>
struct Point { double x, y; };
struct WorldSpace;
struct ScreenSpace;
// Conversions between coordinate spaces are explicit.
Point<ScreenSpace> project(Point<WorldSpace> p, const Camera& c) { … }
As you’ve seen, though, there are some difficulties with unit types. One thing you can do is decompose units into a vector of integer exponents on the fundamental components:
template<typename T, int Meters, int Seconds>
struct Unit {
Unit(const T& value) : value(value) {}
T value;
};
template<typename T, int MA, int MB, int SA, int SB>
Unit<T, MA - MB, SA - SB>
operator/(const Unit<T, MA, SA>& a, const Unit<T, MB, SB>& b) {
return a.value / b.value;
}
Unit<double, 0, 0> one(1);
Unit<double, 1, 0> one_meter(1);
Unit<double, 0, 1> one_second(1);
// Unit<double, 1, -1>
auto one_meter_per_second = one_meter / one_second;
Here we’re using phantom values to tag runtime values with compile-time information about the exponents on the units involved. This scales better than making separate structures for velocities, distances, and so on, and might be enough to cover your use case.
The approach I would recommend is to focus on the interface of your key-value store, so as to make it as clean as possible and as nonrestrictive as possible, meaning that it should allow maximum freedom to the callers, but also maximum freedom for choosing how to implement it.
Then, I would recommend that you provide an as bare as possible, and as clean as possible implementation, without any performance concerns whatsoever. To me it seems like unordered_map
should be your first choice, or perhaps map
if some kind of ordering of keys must be exposed by the interface.
So, first get it to work cleanly and minimally; then, put it to use in a real application; in doing so, you will find what issues you need to address on the interface; then, go ahead and address them. Most chances are that as a result of changing the interface, you will need to rewrite big parts of the implementation, so any time you have already invested on the first iteration of the implementation beyond the bare minimum amount of time necessary to get it to just barely work is time wasted.
Then, profile it, and see what needs to be improved in the implementation, without altering the interface. Or you may have your own ideas about how to improve the implementation, before you even profile. That's fine, but it is still no reason to work on these ideas at any earlier point in time.
You say you hope to do better than map
; there are two things that can be said about that:
a) you probably won't;
b) avoid premature optimization at all costs.
With respect to the implementation, your main issue appears to be memory allocation, since you seem to be concerned with how to structure your design in order to work around problems that you foresee that you are going to have with respect to memory allocation. The best way to address memory allocation concerns in C++ is by implementing a suitable memory allocation management, not by twisting and bending the design around them. You should consider yourself lucky that you are using C++, which allows you to do your own memory allocation management, as opposed to languages like Java and C#, where you are pretty much stuck with what the language runtime has to offer.
There are various ways of going about memory management in C++, and the ability to overload the new
operator may come in handy. A simplistic memory allocator for your project would preallocate a huge array of bytes and use it as a heap. (byte* heap
.) You would have a firstFreeByte
index, initialized to zero, which indicates the first free byte in the heap. When a request for N
bytes comes in, you return the address heap + firstFreeByte
and you add N
to firstFreeByte
. So, memory allocation becomes so fast and efficient that it becomes virtually no issue.
Of course, preallocating all of your memory may not be a good idea, so you may have to break your heap into banks which are allocated on demand, and keep serving allocation requests from the at-any-given-moment-newest bank.
Since your data are immutable, this is a good solution. It allows you to abandon the idea of variable length objects, and to have each Pair
contain a pointer to its data as it should, since the extra memory allocation for the data costs virtually nothing.
If you want to be able to discard objects from the heap, so as to be able to reclaim their memory, then things become more complicated: you will need to be using not pointers, but pointers to pointers, so that you can always move objects around in the heaps so as to reclaim the space of deleted objects. Everything becomes a bit slower due to the extra indirection, but everything is still lightning fast compared to using standard runtime library memory allocation routines.
But all this is of course really useless to be concerned with if you don't first build a straightforward, bare-minimal, working version of your database, and put it to use in a real application.
Best Answer
Sadly, no, because there's too many cases. In your sample, you use
std::string@
to represent the perfectly forwarded type of an object that should be perfectly forwarded to astd::string
constructor, and say "A similar code could be written for setters.". But you're wrong. You'd need another seperate syntax for assignment. For instance, I can construct astd::vector<anything>
from anint
, but I can't assign anint
to astd::vector<anything>
. So I'd need likestd::vector<anything>#
for assignments. And what about the+
operator? If I want to perfect forward a RHS to a member'soperator+
, then I'd need a notation for that too. And it can't be an existing symbol like+
or that would make C++ much harder to parse than it already is! So you can see that this doens't apply universally how you appear to think it does.Secondly, I disagree that the existing boilerplate doesn't scale well. It scales linearly, which is pretty well I think. (Note that the members and the mem-init-list boilerplate is required in any case and is thus not part of the scaling. Even if it were, that's still linear)
Third: This is only needed when you need to pass an unknown type perfectly to the member, which is very rare. Normally, you'd just take all the members as
std::string
by value, and move them into the members, which is amazingly close to optimal considering how amazingly easy it is.