operator<< overload for inner enum of a template

D

Doctor Smith

Hello!

I would like to have an output streaming operator for an enum that is
inside a templated class, but I just can't seem to make it work! I
suspect that I have a template ambiguity issue, but I haven't pinned
down what change will make the code work.

The following example illustrates the problem. I would like the
program to print 'C', but it prints '2'.

#include <iostream>

template <typename AX_FLOAT>
class M
{
public:
enum T
{
A,
B,
C
};
};

template <typename AX_FLOAT>
std::eek:stream& operator<<(std::eek:stream& os, typename M<AX_FLOAT>::T t)
{
switch (t)
{
case M<AX_FLOAT>::A: os << 'A'; break;
case M<AX_FLOAT>::B: os << 'B'; break;
case M<AX_FLOAT>::C: os << 'C'; break;
}

return os;
}

int main()
{
M<float>::T t = M<float>::C;

std::cout << "t = " << t << std::endl;
}

Any insights on how to make this work, or why it will never work, is
muchly appreciated.
 
I

Ian Collins

Doctor said:
Hello!

I would like to have an output streaming operator for an enum that is
inside a templated class, but I just can't seem to make it work! I
suspect that I have a template ambiguity issue, but I haven't pinned
down what change will make the code work.

The following example illustrates the problem. I would like the
program to print 'C', but it prints '2'.
Try:

template <typename AX_FLOAT>
class M
{
public:
enum T
{
A,
B,
C
};

friend std::eek:stream& operator<<(std::eek:stream& os, T t)
{
switch (t)
{
case M<AX_FLOAT>::A: os << 'A'; break;
case M<AX_FLOAT>::B: os << 'B'; break;
case M<AX_FLOAT>::C: os << 'C'; break;
}

return os;
}
};
 
D

Doctor Smith

Thanks! That little change fixed the problem neatly in my test
program. Presumably it will work equally well in our project code.

Though, it would be interesting to get the technical explanation as to
why the first works and the second doesn't.
 
J

Jerry Coffin

Hello!

I would like to have an output streaming operator for an enum that is
inside a templated class, but I just can't seem to make it work! I
suspect that I have a template ambiguity issue, but I haven't pinned
down what change will make the code work.

The following example illustrates the problem. I would like the
program to print 'C', but it prints '2'.

One possibility would be like this:

#include <iostream>

template <typename F>
struct M {
enum Ts {A, B, C};

class T {
typename M::Ts t;
public:
T(typename M::Ts val) : t(val) {}

friend std::eek:stream &operator<<(
std::eek:stream &os,
typename M<F>::T const &m)
{
static char const names[] = "ABC";
return os << names[m.t];
}
};
};

int main()
{
M<float>::T t = M<float>::C;

std::cout << "t = " << t << std::endl;
}
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top