From: buchtak on
Hi,

can I define type of a variable based on some other type using
templates? For example I have

template <typename T> struct A { type x; };

and I want to define member variable x of A, such that if T is char,
then x will be of type int, if T is float, then x will be of type
double etc. Is this possible via templates, or do I have to declare
something like

struct A_char { int x; };
struct A_float { double x; };

and to this manually?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
buchtak(a)gmail.com wrote:
> Hi,
>
> can I define type of a variable based on some other type using
> templates? For example I have
>
> template <typename T> struct A { type x; };
>
> and I want to define member variable x of A, such that if T is char,
> then x will be of type int, if T is float, then x will be of type
> double etc. Is this possible via templates, or do I have to declare
> something like
>
> struct A_char { int x; };
> struct A_float { double x; };
>
> and to this manually?
Read up about template specialisation.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: buchtak on
On 10 pro, 06:42, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> buch...(a)gmail.com wrote:
> > Hi,
>
> > can I define type of a variable based on some other type using
> > templates? For example I have
>
> > template <typename T> struct A { type x; };
>
> > and I want to define member variable x of A, such that if T is char,
> > then x will be of type int, if T is float, then x will be of type
> > double etc. Is this possible via templates, or do I have to declare
> > something like
>
> > struct A_char { int x; };
> > struct A_float { double x; };
>
> > and to this manually?
>
> Read up about template specialisation.

{ edits: quoted banner removed. please don't quote the banner. -mod }

I know the basics of templates. But I need more advanced technique
than just simple type specialisation, that's why I ask. For example,
if I want to create an array, which will contain squares of the input
array, I need a type with better precision that the input to avoid
overflowing, i.e. for char I need short or int, for float I need
double and so on... Basically, some of members should be directly of
type T, some of other type, which is determined by the input type. I
know there is possibility of declaring type by a static condtition, I
just do not know how to do it in this particular case.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
buchtak(a)gmail.com wrote:
>
> can I define type of a variable based on some other type using
> templates? For example I have
>
> template <typename T> struct A { type x; };
>
> and I want to define member variable x of A, such that if T is char,
> then x will be of type int, if T is float, then x will be of type
> double etc.

Use type traits:

template <typename T> struct A_traits
/* you may provide default here: { typedef void x_type; } */ ;

template <> struct A_traits<char> { typedef int x_type; };
template <> struct A_traits<float> { typedef double x_type; };

template <typename T> struct A { typename A_traits<T>::x_type x; };

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 10 d�c, 10:25, "buch...(a)gmail.com" <buch...(a)gmail.com> wrote:

> I know the basics of templates. But I need more advanced technique
> than just simple type specialisation

This is exactly what you need given the explanation of what you want
to do.
Look at partial template specialization as well.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]