tags:

views:

177

answers:

3

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?

+2  A: 

There really isn't a reason why except for that it's not there and I guess it hasn't been proposed to be added, or if it has, didn't have enough support.

If you want to pursue it, I'd suggest starting with the standards committee

http://www.open-std.org/Jtc1/sc22/wg21/

Lou Franco
+1  A: 

I guess it's because template arguments are treated similar to function arguments and you can not declare

void func( class A{} a, class B{} b );

either. I also think it would be impossible to obey the ODR if you need the classes in more than one template (typedef).

lothar
A: 

I think coming C++0x Concepts are pretty close, though not exactly the same to what you are describing.

Nikolai N Fetissov
That has nothing to do with this.
Zifre