title: Impose sorted order on C++ template parameters
body: <p>Recently, I've been attempting to solve a problem using template parameters. The problem can be simplified as follows:</p>
  
  <p>Say I have three descriptive types: Name, Age, and Occupation. Each instance of these types is stored in a static vector with all other elements of its type. I also have a list of items, identified by ID, each of which is composed of some combination of these types.</p>
  
  <p>For example, say I have three items:</p>
  
  <pre><code>- GoldenGateBridge: {NAME: "Golden Gate", AGE: none, OCCUPATION: none}
  - Baby: {NAME: none, AGE: 1, OCCUPATION: none}
  - UnknownTrucker: {NAME: none, AGE: none, OCCUPATION: "Trucker"}
  </code></pre>
  
  <p>(I know that these types are perhaps not the best example, but for the sake of argument we'll assume that not all of them are mandatory). </p>
  
  <p>These items would be stored within the vectors containing the components that make them up. More precisely,</p>
  
  <pre><code>- Names: [GoldenGateBridge],
  - Ages: [Baby],
  - Occupations: [UnknownTrucker]
  </code></pre>
  
  <p>The reason that the items are stored this way is that there are chunks of logic which operate on composing elements of certain types. For example, there might be a function SayEveryonesName which acts on all elements containing a name. For items composed of a single type, this works like a charm.</p>
  
  <p>However, say that in the example above, I decide to give the name "Tom" to Baby. Since the object now has a Name <em>and</em> an Age, I can no longer store it in the vector of Name elements. Instead, I would like to create a new element in a vector containing elements with a Name <em>and</em> Age. The desired representation here would be:</p>
  
  <pre><code>- Names: [GoldenGateBridge],
  - Ages: [],
  - Occupations: [UnknownTrucker],
  - NamesAndAges: [Baby]
  </code></pre>
  
  <p>By itself, this still isn't too problematic. A templated class with a static vector of tuple types could be created, with its template parameters representing the composing types it contains (e.g. Holder, Holder, Holder).</p>
  
  <p>This approach introduces a few problems, however. Namely, how to represent templated functions to add or remove composing types to given items. For example, if I add an <strong>&lt; A ></strong> to an item composed of a <strong>&lt; B ></strong>, the resulting item should be stored in <strong>&lt; A, B ></strong>'s vector. However, if I do the opposite and add an item composed of a <strong>&lt; B ></strong> to an item composed of an <strong>&lt; A ></strong>, the resulting item should <em>still</em> be stored in <strong>&lt; A, B ></strong>'s vector as an <strong>&lt; A, B ></strong>. In order words, the order of the elements shouldn't matter in the underlying representation.</p>
  
  <p>This leads me to my title question: is there any mechanism with which one can impose ordering on template parameters? <em>e.g. template&lt; Y,Z,X > would map to template&lt; X,Y,Z > under the hood (assuming for this example that our ordering is alphabetical on template parameter type).</em> </p>
  
  <p>If not, does anyone have any suggestions for how to manage a situation like this? One working solution is to simply keep a reference to a given item in <strong>each</strong> of its composing types' vectors, but that sacrifices performance when a given piece of logic depends on items with multiple types. It seems like code generation would also solve this problem, in that one could specifically enumerate every single possible combination of types and define the desired output type for adding each possible type to it, but obviously that introduces quite a bit of code bloat from unused types, isn't as flexible,  and seems to be a bit hacky.</p>
  
  <p>As an aside, another problem which this approach introduces is that retrieving composing types becomes a multi-vector problem. For example, if I have a piece of logic which acts on all items with a type <strong>&lt; A ></strong> in them, we would want to work on all <strong>&lt; A >**s, all **&lt; A, B>**s, and all types of form **&lt; A, B, ... ></strong>. I'm also unclear of the best approach for this, but it seems like this may be wrapped up with the best way to approach the former problem.</p>
  
  <p>Any help which you guys could offer would be very helpful and greatly appreciated. Thanks!</p>