Applying the Barton-Nackman idiom

It is amazing how much code you can cram into a single line, and how much magic happens behind the scenes when you do.

I’m working on a pet project in one of the fields I like most: embeddable domain-specific languages. One of the things I want to be able to do with this particular domain-specific language is output any type of literal for debugging purposes while compiling (or interpreting) the script. In order to do that, I want my code to be expressive such that typing

C c(...);
cout << c << endl;

will “just work”. To accomplish that, I’ve written a little class that’s called “Serializable” that looks like this:

template < typename T >
struct Serializable 
    template < typename Y > friend Y& operator<<(Y &out, const T & t) { t.serialize(out); return out; }

To make a type serializable with this, all you need to dos derive from Serializable and implement serialize, like so:

struct Complex : Serializable< Complex >
    double real_;
    double imag_;
    template < typename S >
    void serialize(S &s)
        s << '(' << real_ << ',' << imag_ << ')';

This trick is called the Barton-Nackman idiom, and generates an operator that would look like this:

template < typename S >
S& operator<<(S &s, const Complex &c)
    return s;

Note that in this case only S is still a template parameter: T has become the type Complex, which is what we wanted. Also note that the new operator is not a member: it’s a stand-alone operator that is an overload of the usual operator<<. That's what the friend declaration is for: omitting friend in this case would have made the operator a member -- and would not have worked as expected.

Share in top social networks!

About rlc

Software Analyst in embedded systems and C++ programmer. I specialize in systems design, concurrency and language design, and am interested in generic meta-programming and functional programming and their practical applications. I take a pragmatic approach to project management, focusing on the management of risk. I have over a decade of experience as a software professional and a background in science.
This entry was posted in C & C++. Bookmark the permalink.

One Response to Applying the Barton-Nackman idiom

  1. rlc says:

    I should note, by the way, that because my operator< < is templated, it is not automatically a friend :)

Comments are closed.