Topic: Introduce "typegroups" for template specialization
Author: Peter Kondratyuk <pkondratyuk@gmail.com>
Date: Thu, 27 Mar 2014 10:11:37 -0700 (PDT)
Raw View
------=_Part_4257_14807718.1395940297849
Content-Type: text/plain; charset=UTF-8
Currently, when specializing templates for multiple types, each type has to
have its own specialization. This requires code duplication when several of
these specializations should behave identically.
Obviously, duplicated code is bad, so I propose a new keyword, "typegroup",
that is only used in template specializations. This keyword would tell the
compiler to specialize the template identically for each type in the
typegroup. The example below is a self-explanatory demonstration of the
concept:
Example:
//Proposed introduction of the "typegroup" keyword into the C++ standard
//To avoid code duplication in template specializations
typegroup floatTypes {double, float, complex};
typegroup intTypes {short, int, long long};
//Templated function, but it also could be a method of a templated class
template<typename T> void PrintValue(T value)
{
//Do something for all types, except specialized one
printf("No specialization for this type.\n");
}
//Specialization of the function for all floatTypes
template<> PrintValue<floatTypes>(floatTypes value)
{
printf("%e \n", value);
}
//Specialization of the function for all intTypes
template<> PrintValue<intTypes>(intTypes value)
{
printf("%i \n", value);
}
--
---
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-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_4257_14807718.1395940297849
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Currently, when specializing templates for multiple types,=
each type has to have its own specialization. This requires code duplicati=
on when several of these specializations should behave identically.<br><br>=
Obviously, duplicated code is bad, so I propose a new keyword, "typegroup",=
that is only used in template specializations. This keyword would tell the=
compiler to specialize the template identically for each type in the typeg=
roup. The example below is a self-explanatory demonstration of the concept:=
<br><br>Example:<br><br>//Proposed introduction of the "typegroup" keyword =
into the C++ standard<br>//To avoid code duplication in template specializa=
tions<br><br>typegroup floatTypes {double, float, complex};<br>typegroup in=
tTypes {short, int, long long};<br><br>//Templated function, but it also co=
uld be a method of a templated class<br>template<typename T> void Pri=
ntValue(T value)<br>{<br> //Do something for all types, e=
xcept specialized one<br> printf("No specialization for t=
his type.\n");<br>}<br><br>//Specialization of the function for all floatTy=
pes<br>template<> PrintValue<floatTypes>(floatTypes value)<br>{=
<br> printf("%e \n", value);<br>}<br><br>//Specialization=
of the function for all intTypes<br>template<> PrintValue<intType=
s>(intTypes value)<br>{<br> printf("%i \n", value);<br=
>}<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_4257_14807718.1395940297849--
.