Concepts? Template specialization depending on class functions?
Ben Phillips
Ben_member at pathlink.com
Thu Mar 2 12:49:06 PST 2006
In article <du751p$1imp$1 at digitaldaemon.com>, Ben Phillips says...
>
>I have a question and a suggestion:
>Is it possible to determine whether a given class contains a specific member
>function?
>
>It would be useful to be able to create a template for classes that have certain
>functions without
>requiring the user to inherit from an interface. One idea I've heard is to use
>"concepts" (a possible
>extension to C++) where code would go as follows
>
>concept Collection(T)
>{
>bool add(T t);
>bool isEmpty();
>..
>}
>
>template doSomething(CollectionType : Collection)
>{
>void doSomething(CollectionType t) { ... }
>}
>
>This would allow the compiler to give a clearer error message to people who try
>to use the template
>with a class that doesn't fit the Collection concept. Instead of errors about
>undefined functions the user
>would be told something like "Error: T does not fit the Collection concept [and
>maybe list the missing
>functions'".
>
>
I should probably have clarified that a class would not have to inherit from a
concept to qualify for a template. It would only have to have functions (and
possibly aliases) of the same names.
Concepts would also only be used to help specialize templates. It would be
illegal to do something like
concept Queue { ... }
void foo()
{
Queue q = new QueueClassThatFitsConcept(); // error, concepts aren't types and
can't be instantiated. Use interfaces in this case
}
Also, most importantly, since concepts aren't types and classes that "fit" a
concept don't inherit from them there is no virtual function overhead (actually
a class doesn't even have to know the concepts it fits).
More information about the Digitalmars-d
mailing list