Reputation: 646
I want to define member functions using templates, in a class which is not a template:
class Foo {
public:
template <typename T>
int doThing(T thing, int num);
};
This works. But what I want to do is this:
class Foo {
public:
template <typename T>
int doThing<T>(int num);
};
With this, I get error: expected initializer before ‘<’ token
. I don't understand why one works and the other doesn't? Changing typename
to class
doesn't make any difference.
Is there some way to achieve this?
Edit: More detail as requested: what I want to achieve is to collapse this repetitive code:
class Blob {
public:
int read_int8(lua_State *Lua, int offset);
int read_uint8(lua_State *Lua, int offset);
int read_int16(lua_State *Lua, int offset);
int read_uint16(lua_State *Lua, int offset);
//...
};
int Blob::read_int8(lua_State *Lua, int offset) {
int8_t *ptr = (int8_t*)this->data;
lua_pushinteger(Lua, ptr[offset]);
return 1;
}
int Blob::read_uint8(lua_State *Lua, int offset) {
uint8_t *ptr = (uint8_t*)this->data;
lua_pushinteger(Lua, ptr[offset]);
return 1;
}
int Blob::read_int16(lua_State *Lua, int offset) {
int16_t *ptr = (int16_t*)this->data;
lua_pushinteger(Lua, ptr[offset]);
return 1;
}
int Blob::read_uint16(lua_State *Lua, int offset) {
uint16_t *ptr = (uint16_t*)this->data;
lua_pushinteger(Lua, ptr[offset]);
return 1;
}
Upvotes: 0
Views: 116
Reputation: 837
No need for <T>
.
class Foo {
public:
template <typename T>
int doThing(int num);
};
Upvotes: 4