Describing pixel format information in C++ in a way that is usable at both compile-time and runtime
<p>I have a library that does operations on pixels. The pixels can by in many different formats. I am looking for an effective way to describe the formats in the library API (internally and externally).</p>

<p>For some classes the pixel format is a <strong>template argument</strong>, for others it is a <strong>runtime argument</strong>. So the pixel formats need to be usable both runtime (as constructor or function argument) and compile time (as template argument). I want to <strong>describe the pixel formats only once</strong>.</p>

<p>What I have now is something like this:</p>

<pre><code>enum class color_space : uint8_t { rgb, cmyk /* , etc... */ };

struct pixel_layout {
    color_space space;
    uint8_t channels;
    /* etc... */
};

template &lt;color_space ColorSpace, uint8_t Channels /* etc.. */&gt;
struct pixel_type {
    static constexpr color_space space = ColorSpace;
    static constexpr uint8_t channels = Channels;
    /* etc... */

    static constexpr pixel_layout layout() {
        return {space, channels /* , etc... */ };
    }
};

struct rgb  : public pixel_type&lt;color_space::rgb, 3 /* , etc... */ &gt; {};
struct rgba : public pixel_type&lt;color_space::rgb, 4 /* , etc... */ &gt; {};
</code></pre>

<p>This works fairly well. I can use these as runtime and compile time arguments:</p>

<pre><code>template &lt;class PixelType&gt;
class image { };

struct transform {
    transform(const pixel_layout from, const pixel_layout to)
        : from(from), to(to) { /* ... */ }

    pixel_layout from;
    pixel_layout to;
};
</code></pre>

<p>Also convert from compile-time type to runtime type:</p>

<pre><code>transform(rgb::layout(), rgba::layout());
</code></pre>

<p><strong>However</strong>, duplicating and storing the <code>pixel_layout</code> details of the pixel types whenever they are used at runtime seems silly to me. Conceptually, all the program should need is an ID/address/reference to a specific <code>pixel_type</code> and a way to retrieve the associated properties (color space, channels, etc) at both compile time and runtime.</p>

<p>Also, if I want to get a derived property from a pixel type, I need to implement it on <code>pixel_layout</code> if I want to avoid duplicating logic. Then to use it at compile time, I need to go from <code>pixel_type&lt;...&gt;</code> class to <code>pixel_layout</code> instance to derived property. That too, seems a little silly.</p>

<p><strong>Can I avoid passing around the <code>pixel_layout</code> details, and instead use some kind of reference to the <code>pixel_type&lt;...&gt;</code> (sub)classes?</strong> </p>

<p><strong>I tried using <code>enum</code>s</strong>, because enums work as template argument &amp; function argument. But I struggled to get from enum value (e.g. <code>rgba</code>) to pixel type property (e.g. 4 channels) at runtime &amp; compile time in an idiomatic C++ way.</p>

<p>Also, enums as template arguments give far less useful diagnostics during compile error. For example, I get <code>image&lt;(pixel_type)2&gt;</code> rather than <code>image&lt;rgba&gt;</code> in compile error messages with clang. So this does not seem like a useful approach.</p>