Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2008-04-12 03:28:51


I recently had a C++ problem and found an answer that tickled my brain.
In the spirit of Car Talk on NPR, I thought I'd share it in the form of
a puzzler.

Say that you are given a set of types, and you must find the "common"
type, where common is defined as follows:

- If all the types are the same, say X, then the common type is X
- If some types are X and some are Default (where Default is a known
special type), the common type is X
- If some types are X and some are Y (and neither X nor Y is Default),
then there is no common type. It's an error and you should issue a
diagnostic.

So for example:

[Default, Default, Default] --> Default
[Default, A, Default] --> A
[B, B, B] --> B
[Default, C, D] --> Error, issue diagnostic.

And here's the kicker ... do it in O(1). No O(N) recursive template
instantiations allowed. Assume that the maximum size of the set is known
ahead of time.

Answer next week, unless someone beats me to it.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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