Boost logo

Boost :

Subject: [boost] [mpl] holder class for types with external linkage (similar to integral contants)
From: klaus triendl (klaus_at_[hidden])
Date: 2009-02-04 08:57:24


Recently, I've been in need of a holder class for strings with external
linkage but I didn't find an mpl datatype that could wrap an externally
defined entity.

Instead of creating a quick solution working for my needs I thought the
mpl library could benefit from a generic holder class similar to the
integral constant holders.

<code>
template<typename T, T N>
struct external_c
{
    // make it a nullary metafunction
    typedef external_c type;
    typedef T value_type;
    // correct? requirements?
    typedef integral_c_tag tag;

    // can't make a static compile time constant but only a static
    // function
    static value_type value() { return N; }

    // implicitly convertible to its runtime type value
    operator value_type() const { return this->value(); }
};

// e.g.
extern const char string1[];
const char string1[] = "string1";
extern const std::string string2;
const std::string string2("string2");

int main()
{
    external_c<const char* const, string1> x1;
    external_c<const std::string&, string2> x2;
    std::cout << x1 << "\n";
    std::cout << x2.value() << "\n";
    return 0;
}
</code>

The name 'external_c' is just a quick idea and would have to be changed
anyway with c++09's possibility to use types with internal linkage in
template programming.

Would it be reasonable to integrate this into the mpl? Any comments?

Klaus Triendl


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk