Type conversion operators

E

Eric

Say you have the following:

template <typename _T>
struct A {
typedef _T T;
A (T t);
operator T () const;
};

How do you define the T conversion operator?

TIA.
 
R

Rob Williscroft

Eric wrote in
Say you have the following:

template <typename _T>

Note that identifiers that begin with an underscore followed by one
of A through Z are reserved for the implementation, for eg your
std library implementation my define _T as a macro, don't use such
identifiers,
struct A {
typedef _T T;
A (T t);
operator T () const;
};

How do you define the T conversion operator?

template <typename Type >
struct A
{
typedef Type T;
A (T t) {}
operator T () const;
};

template < typename Type >
A< Type >::eek:perator typename A< Type >::T () const
{
return T();
}

You could also in this case of writen this,

template < typename Type >
A< Type >::eek:perator Type () const
{
return T(); // or Type();
}

Or (if inline is Ok) inside the class/struct,

template <typename Type >
struct A
{
typedef Type T;
A (T t) {}
operator T () const
{
return T();
}
};

HTH.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top