Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can we explicit instantiate a class template inside of a class or a function?

59 views
Skip to first unread message

Barry

unread,
Nov 20, 2007, 9:41:21 AM11/20/07
to
Can we explicit instantiate a class template inside of a class or a
function?

according to my understanding, The answer is NO, explicit template
instantiation should take place in a namespace scope, not a class scope,
not a function scope.

And the I think where supports my answer is here:

14.7.2/5
An explicit instantiation of a class or function template specialization
is placed in the namespace in which the template is defined.

Do I interpret the para right?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Daniel Krügler

unread,
Nov 21, 2007, 6:10:36 PM11/21/07
to
On 20 Nov., 15:41, dhb2...@gmail.com (Barry) wrote:
> Can we explicit instantiate a class template inside of a class or a
> function?

No.

> according to my understanding, The answer is NO, explicit template
> instantiation should take place in a namespace scope, not a class scope,
> not a function scope.
>
> And the I think where supports my answer is here:
>
> 14.7.2/5
> An explicit instantiation of a class or function template specialization
> is placed in the namespace in which the template is defined.
>
> Do I interpret the para right?

I agree with your interpretation. Note that I think that this
view is also somewhat supported by a comment given in

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#44

which says:

"All uses of the phrase "in the namespace" in the IS mean "directly
in the namespace," not in a scope nested within the namespace."

(NB: Those comments have no binding effect, but I think
quoting such a remark at this stage of analysis is a valid
one)

Greetings from Bremen,

Daniel Krügler

Jerry Coffin

unread,
Nov 26, 2007, 12:51:17 PM11/26/07
to
On Nov 20, 7:41 am, dhb2...@gmail.com (Barry) wrote:
> Can we explicit instantiate a class template inside of a class or
> a function?
>
> according to my understanding, The answer is NO,explicit template

> instantiation should take place in a namespace scope, not
> a class scope, not a function scope.
>
> And the I think where supports my answer is here:
>
> 14.7.2/5
> An explicit instantiation of a class or function template
> specialization is placed in the namespace in which the template is
> defined.
>
> Do I interpret the para right?

I don't think so. To mean that, the standard would (or at least
should) use the word "shall" -- i.e. the wording would be:

An explicit instantiation of a class or function template

specialization shall be


placed in the namespace in which the template is defined.

As it stands right now, it's written not as a requirement, but as
information about what WILL happen. IOW, you can put the code for the
explicit instantiation essentially anywhere you want to, but
regardless of where you put that code, the instantiation will take
place in the scope of the namespace in which the template is defined.

Daniel Krügler

unread,
Nov 26, 2007, 2:38:26 PM11/26/07
to
On 26 Nov., 18:51, Jerry Coffin <jerry.cof...@gmail.com> wrote:
> I don't think so. To mean that, the standard would (or at least
> should) use the word "shall" -- i.e. the wording would be:
>
> An explicit instantiation of a class or function template
> specialization shall be
> placed in the namespace in which the template is defined.
>
> As it stands right now, it's written not as a requirement, but as
> information about what WILL happen. IOW, you can put the code for the
> explicit instantiation essentially anywhere you want to, but
> regardless of where you put that code, the instantiation will take
> place in the scope of the namespace in which the template is defined.

I agree that wording could (and should) be more stricter,
but I don't think that due to this omission the standard
allows arbitrary lexical places of an explicit instantiation.
[temp.point]/5 says:

"An explicit instantiation directive is an instantiation point
for the specialization or specializations specified by the
explicit instantiation directive."

It says *is* an instantiation point - and a class or function
template cannot have an POI inside function scope, for
example.

Greetings from Bremen,

Daniel Krügler

---

Barry

unread,
Nov 28, 2007, 10:31:47 AM11/28/07
to
Jerry Coffin wrote:
> On Nov 20, 7:41 am, dhb2...@gmail.com (Barry) wrote:
>> Can we explicit instantiate a class template inside of a class or
>> a function?
>>
>> according to my understanding, The answer is NO,explicit template
>> instantiation should take place in a namespace scope, not
>> a class scope, not a function scope.
>>
>> And the I think where supports my answer is here:
>>
>> 14.7.2/5
>> An explicit instantiation of a class or function template
>> specialization is placed in the namespace in which the template is
>> defined.
>>
>> Do I interpret the para right?
>
> I don't think so. To mean that, the standard would (or at least
> should) use the word "shall" -- i.e. the wording would be:
>
> An explicit instantiation of a class or function template
> specialization shall be
> placed in the namespace in which the template is defined.
>
> As it stands right now, it's written not as a requirement, but as
> information about what WILL happen. IOW, you can put the code for the
> explicit instantiation essentially anywhere you want to, but
> regardless of where you put that code, the instantiation will take
> place in the scope of the namespace in which the template is defined.
>

Catching up the URL given by Diaiel (thanks :-) )

Proposed resolution (suggested 04/01, proposed 10/01):

(The first change below will need to be revised in accordance with the
resolution of issue 284 to add a cross-reference to the text dealing
with class names.)

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#275

*Proposed resolution (suggested 04/01, proposed 10/01):*

Add in 14.7.2 [temp.explicit] paragraph 2 before the example:

An explicit instantiation shall appear in an enclosing namespace of its
template. If the name declared in the explicit instantiation is an
unqualified name, the explicit instantiation shall appear in the
namespace where its template is declared. [Note: Regarding qualified
names in declarators, see 8.3 [dcl.meaning].]


//////////
My understanding about explicit instantiation/specialization is that
they should be both placed in a namespace, (except declaring a
specialization as friend, which only does name injection).

Think about it, if we allow explicit instantiation/specialization in a
function/class, then how do we explain the visibility of such
specialization/instantiation? I think this will bring more complexity.

0 new messages