web developer & system programmer

coder . cl

ramblings and thoughts on programming...


c++0x, deprecation of exported templates

published: 24-10-2009 / updated: 24-10-2009
posted in: c++, programming, tips
by Daniel Molina Wegener

C++0x, the incoming C++ standard, is subject of continuous changes. Few months ago C++0x Concepts were removed from the working draft. Now, Template Exports will be deprecated from the future standard. What does it means?. "A template defines a family of classes, functions, or concept maps, or an alias for a family of types". Who has worked with C++ templates, knows that they are the most powerful approach to abstraction. Also they are pretty similar to the Java Generics, but different enough to hold more complex classes and methods, such as working with operators.

A template in C++ works like a Java Generic, but it uses different semantics. For example we can use a template to overload a previously declared operator — in Java we can not overload operators — and change its behavior. We can also define templates for functions, classes and methods. How do we play with them, it’s just a design issue. They have a similar behavior to abstract methods, but with more powerful semantics, allowing us to work directly with named types.

/* example template function */
#include <iostream>
#include <vector>

template <class T>
const T& smaller_v(const T& a, const T& b)
{
    if (a.size() > b.size()) {
        return b;
    } else {
        return a;
    }
}

int main(void)
{
    // we play around with three vectors
    std::vector<int> a = std::vector<int>();
    std::vector<int> b = std::vector<int>();
    std::vector<int> c;
    // we put two elements in a vector
    a.push_back(1);
    a.push_back(2);
    // we put three elements in b vector
    b.push_back(3);
    b.push_back(4);
    b.push_back(5);
    // so we compare each vector size
    c = smaller_v<std::vector<int> >(a, b);
    // after we get the smaller vector
    // through our template, we output its size
    std::cout << c.size() << std::endl;
    return 0;
}

We have above an example of template function. It plays around the same namespace and linkage of main(), so we don’t need to export it. But what happens to foreign templates, where we need to import them into the working namespace, at the time they are declared in external linkage. We use the export keyword. How were affected the template declarations before they were removed from the C++0x standard?

A template-declaration may be preceded by the export keyword. Such a template is said to be exported. Declaring exported a class template is equivalent to declaring exported all of its non-inline member functions, static data members, member classes, member class templates, and non-inline member function templates.

Then, templates were exported if we use the export keyword on them, extending the export to its members and the template kind.

If a template is exported in one translation unit, it shall be exported in all translation units in which it appears; no diagnostic is required. A declaration of an exported template shall appear with the export keyword before any point of instantiation (14.7.4.1) of that template in that translation unit. In addition, the first declaration of an exported template containing the export keyword shall not follow the definition of that template. The export keyword shall not be used in a friend declaration.

The template shall be exported in all translation units of its linkage, regarding its declared namespace, preventing the use of any instantiation related usage of the export keyword including the implicit restriction on exporting friends — but explicitly exposed in the working draft. Also the standard refers to templates contained in unnamed namespaces to not be exported outside of its linkage. You may ask what is a translation unit in C++?

The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.6.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit. [ Note: a C++ program need not all be translated at the same time. — end note ]

conclusion

The explicit export of a template, by using the exported templates feature, is restricted on the implementation definition, so the deprecation of the explicit export and any implementation must regard at least the deprecated standard recommendation. So, it lefts to implementation specific or undefined behavior for exported templates in future standards, since they are thinking to remove it completely. In some way I personally feel that they are decapitating the original design of C++0x, which was very powerful at his original specification. Such as the removed specification for Concepts for C++ and nice formal designs.


No coments yet.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>