Topic: Can we explicit instantiate a class template inside of a class or


Author: dhb2000@gmail.com (Barry)
Date: Tue, 20 Nov 2007 14:41:21 GMT
Raw View
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-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "=?ISO-8859-1?Q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Wed, 21 Nov 2007 17:10:36 CST
Raw View
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

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





Author: Jerry Coffin <jerry.coffin@gmail.com>
Date: Mon, 26 Nov 2007 11:51:17 CST
Raw View
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.

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





Author: "=?ISO-8859-1?Q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Mon, 26 Nov 2007 13:38:26 CST
Raw View
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

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