explicit instantiation of member class

H

highegg

hello,

I'm wondering whether it is legal to explicitly instantiate a member
(nested) class prior to explicit instantiation of the enclosing class.
Example:

template <class T>
class A
{
public:
class X
{
public:
T x() { return T(0); }
};

T a () { return T(1); }
};

template class A<int>::X;
template class A<int>;

I'm fairly sure that if I reverse the two explicit instantiations, it
will become illegal, but I have doubts about this form. It is accepted
by GCC and MSVC (at least it was contributed by a MSVC user), but
rejected by Intel C++.

The C++ standard apparently says that a specialization would be OK,
but doesn't seem to say anything explicit about this case. Does an
explicit instantiation count as specialization?

thanks a lot

Jaroslav Hajek, GNU Octave developer
 
N

Neelesh

hello,

I'm wondering whether it is legal to explicitly instantiate a member
(nested) class prior to explicit instantiation of the enclosing class.
Example:

template <class T>
class A
{
public:
  class X
    {
    public:
      T x() { return T(0); }
    };

  T a () { return T(1); }

};

template class A<int>::X;
template class A<int>;

I'm fairly sure that if I reverse the two explicit instantiations, it
will become illegal, but I have doubts about this form. It is accepted
by GCC and MSVC (at least it was contributed by a MSVC user), but
rejected by Intel C++.

Interesting. The above form is also rejected by comeau online.

while I am not completely sure, I gather that template class
A<int>::X is an explicit instantaion of A<int>::X. Further when we say
template class A<int>, according to 14.7.2/7:

"The explicit instantiation of a class template specialization implies
the instantiation of all of its members not previously explicitly
specialized in the translation unit containing the explicit
instantiation."

Which means A<int>::X is instantiated again. Finally according to
14.7/5 "No program shall explicitly instantiate any template more than
once", and hence this is an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top