Topic: Using delete to prevent class template specializations?


Author: McNepp <mcnepp02@googlemail.com>
Date: Thu, 4 Feb 2010 11:57:14 CST
Raw View
Hi,

I just got an obscure compiler error when I inadvertendly instantiated
a class template for the type "void".

This was OK because it made no sense at all to instantiate my template
"jni_field<T>" for T = void.
It then occurred to me that I'd rather have the possibility to write:

template<> class jni_field<void> = delete;

Unfortunately, the upcoming C++ standard uses the "delete" syntax
soley to prevent method instantiation, not class template
instantiation.

Any thoughts why that latter will not be possible?


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





Author: "David Sachs" <sachs1926@comcast.net>
Date: Thu, 4 Feb 2010 18:42:39 CST
Raw View
One possible reason is that concepts were removed from the proposed standard
at a very late point. With concepts it would be very easy to prevent using
void as a template argument in your case.

"McNepp" <mcnepp02@googlemail.com> wrote in message
news:5802843f-581e-4972-95e9-afaf6b1775ce@n7g2000yqb.googlegroups.com...

> Hi,
>
> I just got an obscure compiler error when I inadvertendly instantiated
> a class template for the type "void".
>
> This was OK because it made no sense at all to instantiate my template
> "jni_field<T>" for T = void.
> It then occurred to me that I'd rather have the possibility to write:
>
> template<> class jni_field<void> = delete;
>
> Unfortunately, the upcoming C++ standard uses the "delete" syntax
> soley to prevent method instantiation, not class template
> instantiation.
>
> Any thoughts why that latter will not be possible?
>
>
> --
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
> ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]
>
>

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





Author: Pavel Minaev <int19h@gmail.com>
Date: Fri, 5 Feb 2010 13:19:11 CST
Raw View
On Feb 4, 9:57 am, McNepp <mcnep...@googlemail.com> wrote:
> I just got an obscure compiler error when I inadvertendly instantiated
> a class template for the type "void".
>
> This was OK because it made no sense at all to instantiate my template
> "jni_field<T>" for T = void.
> It then occurred to me that I'd rather have the possibility to write:
>
> template<> class jni_field<void> = delete;
>
> Unfortunately, the upcoming C++ standard uses the "delete" syntax
> soley to prevent method instantiation, not class template
> instantiation.
>
> Any thoughts why that latter will not be possible?

It is very likely that your template won't work for many types other
than void. For example:

jni_field<int&>     // reference
jni_field<int[10]>  // array
jni_field<int()>    // function type

Would it work with a type with no public default constructor? No
public copy constructor? No public operator= ? No public operators ==
and != ?

This issue is much more broad than a single type, and the correct
solution to this is getting working concepts in the language.


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





Author: =?ISO-8859-1?Q?Falk_Tannh=E4user?= <tannhauser86549spam@free.fr>
Date: Sat, 6 Feb 2010 09:44:03 CST
Raw View
Am 05.02.2010 01:42, schrieb David Sachs:
> One possible reason is that concepts were removed from the proposed standard
> at a very late point. With concepts it would be very easy to prevent using
> void as a template argument in your case.
>
> "McNepp" <mcnepp02@googlemail.com> wrote in message
> news:5802843f-581e-4972-95e9-afaf6b1775ce@n7g2000yqb.googlegroups.com...
>> I just got an obscure compiler error when I inadvertendly instantiated
>> a class template for the type "void".
>>
>> This was OK because it made no sense at all to instantiate my template
>> "jni_field<T>" for T = void.
>> It then occurred to me that I'd rather have the possibility to write:
>>
>> template<> class jni_field<void> = delete;
>>
>> Unfortunately, the upcoming C++ standard uses the "delete" syntax
>> soley to prevent method instantiation, not class template
>> instantiation.

The following should work in C++0x:

    #include <type_traits>

    template<typename T> class jni_field
    {
      static_assert(!std::is_same<T, void>::value, "Invalid template
instantiation jni_field<void>");
      // the rest of the class definition...
    };


Otherwise, you can specialise the template so that you get an incomplete type:
    template<> class jni_field<void>; // Declared but not defined
This will limit most abuses, even if it doesn't stop you from defining
pointers or references to this class.

HTH
Falk

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





Author: Edward Diener <eldiener_no_spam_here@tropicsoft.com>
Date: Sun, 7 Feb 2010 18:44:28 CST
Raw View
McNepp wrote:
>
> Hi,
>
> I just got an obscure compiler error when I inadvertendly instantiated
> a class template for the type "void".
>
> This was OK because it made no sense at all to instantiate my template
> "jni_field<T>" for T = void.
> It then occurred to me that I'd rather have the possibility to write:
>
> template<> class jni_field<void> = delete;

Look at Boost's enable_if implementation for a way to allow or
disallow types for template instantiation. It shows why adding some
new construct to the C++ standard, as you suggest above, is
unnecessary.

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