In C# we can define a generic type that imposes constraints on the types that can be used as the generic parameter. The following example illustrates the usage of generic constraints:
interface IFoo { } class Foo<T> where T : IFoo { } class Bar : IFoo { } class Simpson { } class Program { static void Main(string[] args) { Foo<Bar> a = new Foo<Bar>(); Foo<Simpson> b = new Foo<Simpson>(); // error CS0309 } }
Is there a way we can impose constraints for template parameters in C++.
C++0x has native support for this but I am talking about current standard C++.
As someone else has mentioned, C++0x is getting this built into the language. Until then, I’d recommend Bjarne Stroustrup‘s suggestions for template constraints.
Edit: Boost also has an alternative of its own.
Edit2: Looks like concepts have been removed from C++0x.