Forgot your password?
typodupeerror

Comment Re:C++ template concepts vs. C# generics constrain (Score 3, Insightful) 741

You bring up an excellent point about the rigidity of C# generics constraints. One of the crucial features of the proposals for concepts in C++ is retroactive modeling, which allows you to adapt to the specific syntax of a concept *without* changing your data type. So the problem you mention for C# generics is not actually a problem with C++
concepts.

Here's an example. I'm writing a concept for a Stack, which might look like this:

template
concept Stack
{
    typename value_type = S::value_type; // the type of values on the stack
    void push_to_top(S& s, const value_type& value);
    void pop_from_top(S& s);
    value_type& get_top(S& s);
    bool is_empty(const S& s);
};

I picked some silly names on purpose. Now, std::stack doesn't match the syntax of this concept. So what if we try to pass a std::stack to a function like the following, which expects something that is (we use the term "models") a Stack?

template
void clear_stack(S& s)
{
    while (!is_empty(s)) {
        pop_from_top(s);
    }
}

It's going to fail to compile, because std::stack does not match the syntax of the Stack concept. If C++ concepts had the same restrictions as C# generics in this regard, we would be stuck writing an adaptor class. Yuck.

Retroactive modeling saves the day. We can fix the problem by writing a model definition like this:

template
model Stack >
{
    typedef T value_type;
    void push_to_top(std::stack& s, const T& value) { s.push(value); }
    void pop_from_top(std::stack& s) { s.pop(); }
    value_type& get_top(std::stack& s) { return s.top(); }
    bool is_empty(const std::stack& s) { return s.empty(); }
};

In this model definition, we're meeting all of the requirements of the concept by providing function definitions that transform the syntax of the Stack concept (pop_from_top, is_empty, etc.) into calls to the std::stack itself (see the function bodies). Now, when we call clear_stack() with a std::stack, it "just works": the calls to is_empty() and pop_from_top() in clear_stack() go through the model definition. Of course, if we picked more standard names and member functions in our Stack concept, the model definition could be empty or (for implicit/structural concepts) omitted entirely.

Retroactive modeling is *really* important for making it easier to reuse template code. You won't need to be paranoid about matching syntax *exactly* with every concept you need to model, because the compiler will detect any mismatches and you can fix them through a model definition---without having to change the data types, templates,
or concepts. Of course, people will still try to agree on names and concepts when possible, because it saves typing. You can check out the actual proposals before the C++ committee (references follow) for more information. There are two active proposals, but the groups are working together, so expect a final "combined" proposal in the future.

There are other differences between C# generics and C++ concepts. Before starting to design concepts for C++, most of the authors of one of the concepts proposals (N1849; see below) did an extensive study of the generics facilities of several languages (e.g., C# generics, Java generics, Haskell, ML functors, C++ templates). They ran into trouble with every language they tried, and we designed our C++ concepts to avoid those problems. Here's the original paper; there's an extended version (with more languages and more detail) under review:

    Ronald Garcia, Jaakko Jarvi, Andrew Lumsdaine, Jeremy G. Siek, and Jeremiah Willcock. A Comparative Study of Language Support for Generic Programming. In Proceedings of the 2003 ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA'03), October 2003.

Latest revisions of the concept proposals for C++:
    http://www.open-std.org/jtc1/sc22/wg21/docs/papers /2005/n1849.pdf
    http://www.open-std.org/jtc1/sc22/wg21/docs/papers /2005/n1782.pdf

A paper on ConceptGCC, which implements one of the concept proposals (N1849):
    http://www.open-std.org/jtc1/sc22/wg21/docs/papers /2005/n1848.pdf

Slashdot Top Deals

The moon is made of green cheese. -- John Heywood

Working...