Re: [std-proposals] Explicitly defining template class members

50 views
Skip to first unread message
Message has been deleted

David Rodríguez Ibeas

unread,
Feb 26, 2015, 3:18:07 PM2/26/15
to std-pr...@isocpp.org
But currently, template class a<int>; does not instantiate the template class's member function definitions (only the declarations).

14.7.2/8
An explicit instantiation that names a class template specialization is also an explicit instantiation of the
same kind (declaration or definition) of each of its members (not including members inherited from base
classes and members that are templates) that has not been previously explicitly specialized in the translation
unit containing the explicit instantiation, except as described below.

Am I misunderstanding the issue? The explicit instantiation of the class *does* explicitly instantiate the members.

    David

On Thu, Feb 26, 2015 at 8:45 AM tmlen <timle...@gmail.com> wrote:
Given a class template like:

template<typename T>
class a {
public:
    void f();
};

template<typename T>
void a<T>::f() {
    ...
}

C++11 has explicit class template instantiation declarations:

extern template class a<int>;

To define that a translation unit encountering a<int> should not try to instantiate it, but instead look for an existing (explicit) instantiation to link to. In a shared library, this corresponding template instantiation definition would be put in a module of the library:

template class a<int>;

This is useful for example when the shared library is compiled with more compiler optimization, with OpenMP, ... than the program using it.

But currently, template class a<int>; does not instantiate the template class's member function definitions (only the declarations). So to also include an instantiation of a<int>::f this would be needed: In the header:

extern template class a<int>;
extern void a<int>::f();

and in the source

template class a<int>;
void a<int>::f();

It may be useful to be able to state in the template class definition that when the class template gets explicitly instantiated, some of its member functions, or other related classes/functions should get too. Something like

template<typename T>
class a {
public:
    using helper = helper<T>;
    void f();
    template<typename U> void g();

    extern helper;
    extern f();
    extern g<int>();
    extern g<long>();
};

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
Reply all
Reply to author
Forward
0 new messages