Template Question...

I've just written my first c++ template, but I'm getting an error I don't understand. Here is the original template class I wrote:

template <typename TYPE>
class t_StateStack 
{
private:
	TYPE stack[4];
	int cnt = 0;
	TYPE Def = 0;

public:
	void setDefault(TYPE def) { Def = def; }
	inline void Push(TYPE val) { stack[cnt++] = val; }
	inline TYPE Pop(void) { if (cnt)  return stack[--cnt]; else return Def; }
	inline TYPE Peek(void)  { if (cnt)  return stack[cnt-1]; else return Def; }
	inline void Flush() { cnt = 0; }
};

I tried using this with a typedef enum:

typedef enum
{
    ENUM1,
    ENUM2,
    ...
    ENUM10
} t_MyEnum;

t_StateStack <t_MyEnum> MyStack;

...

MyStack.Push(ENUM1);

This threw an "illegal conversion from int to t_MyEnum, pointing at the "class t_StateStack" line of the template. I could not figure out what to do, so I cheated, and modified the template as below, to change all the member data to simple ints, doing casts to the enum on the return values. This works properly, but I don't understand why the first form does not work.

template <typename TYPE>
class t_StateStack 
{
private:
	uint16_t stack[4];
	int cnt = 0;
	uint16_t Def = 0;

public:
	void setDefault(TYPE def) { Def = (uint16_t)def; }
	inline void Push(TYPE val) { stack[cnt++] = (uint16_t)val; }
	inline TYPE Pop(void) { if (cnt)  return (TYPE)stack[--cnt]; else return (TYPE)Def; }
	inline TYPE Peek(void)  { if (cnt)  return (TYPE)stack[cnt-1]; else return (TYPE)Def; }
	inline void Flush() { cnt = 0; }
};

I'm sure it's just some silly syntax thing. Any ideas why the first approach throws that invalid conversion error?

Regards,
Ray L.

What happens if you define your enum the C++ way?

enum MyEnum { val1, val2, ..., valN };

t_StateStack <MyEnum> MyStack;

I can't test it, since all you posted was incomplete snippets.

RayLivingston:
I've just written my first c++ template,

Isn't there a cure for that condition ? :slight_smile:

...R

PaulS:
What happens if you define your enum the C++ way?

enum MyEnum { val1, val2, ..., valN };

t_StateStack MyStack;




I can't test it, since all you posted was incomplete snippets.

I created a small test program, which seems to not care how I declare things - it just works. I'll need to figure out what it is in the BIG program that is causing the error.

Regards,
Ray L.

And it suddenly all makes sense. It had nothing whatsoever to do with the template, which works just fine in its original form, in my big program. The problem is VisualStudio. I recently switched from AtmelStudio to VisualStudio 2013, so I could get the full feature set of VisualMicro. I started having odd build failures. Some of these were caused by a bug in VisualMicro, which was promptly fixed. But I continued to have odd problems I could not pin down. Well, just a little while ago I discovered the real problem: Microsoft, in their infinite wisdom, in VisualStudio 2013 eliminated an option I have been using, and depending on for years - SaveAll before doing a Build. As a result, I was unwittingly doing builds with out-of-date files. Doing a manual SaveAll before each build seems to have eliminated the problem I saw with the template.

Regards,
Ray L.