Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-04-05 15:31:12


Angus Leeming <angus.leeming_at_[hidden]> writes:

> What are the advantages of the Boost.MPL
> approach over the functionally identical:
>
> #include <iostream>
>
> class foo {
> public:
> enum state {
> state1,
> state2,
> state3
> };
>
> // Only the specializations (below) of this template will compile.
> template <int N>
> static foo
> set() { return invalid_value; }

Without a definition for invalid_value, this is invalid code, and on a
conforming compiler, compilation fails at the point it is parsed.

> private:
> foo(state)
> {
> std::cout << "foo" << std::endl;
> }
> };
>
> template <>
> foo foo::set<foo::state2>() { return foo::state2; }
>
> template <>
> foo foo::set<foo::state3>() { return foo::state3; }
>
> int main()
> {
> // Compiles, as expected.
> foo f1 = foo::set<foo::state2>();
> foo f2(foo::set<foo::state2>());
> // Fail to compile, as expected.
> // foo f3 = foo::set<foo::state1>();
> // foo f4(foo::set<foo::state1>());
>
> return 0;
> }

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net