I'm creating a library to allow OCaml/Haskell-like algebraic data types and pattern matching. The algebraic data types are implemented using a class similar to Boost.Variant. I would like to be able to define new types (the constructor) in the template arguments, but I get an error. I'm using my own type with variadic templates, but I'll use Boost's variant
here for simplicity. Why isn't something like this:
typedef variant <
class Foo { ... },
class Bar { ... }
> Baz;
allowed? I know I can define the types separately, but that means I can't use some nice macros. In most cases in C++ you are allowed to define a new type where you are using it, for example:
struct Foo { ... } bar;
Here I am defining a new type Foo
, and a variable bar
of type Foo
. If things like this are allowed, why doesn't it work with templates?