I’m trying to write two template functions. They must simply return reference to local variable. But it doesn’t work:
class Cube_cache_value {
public:
//...
template<>
QVector<unsigned short>& get_buffer<unsigned short>() { return vector; } // error here
template<>
QVector<float>& get_buffer<float>() { return float_vector; }
private:
QVector<unsigned short> vector;
QVector<float> float_vector;
};
error: explicit specialization in non-namespace scope ‘class hsp::Cube_cache_value’
Then I’ve tried to get functions out of class scope, but compiler still don’t like it:
class Cube_cache_value {
public:
//...
inline QVector<unsigned short>& get_buffer() { return vector; }
inline QVector<float>& get_float_buffer() { return float_vector; }
private:
QVector<unsigned short> vector;
QVector<float> float_vector;
};
template<>
QVector<unsigned short>& get_buffer<unsigned short>(Cube_cache_value* v) { //error here
return v->get_buffer();
}
template<>
QVector<float>& get_buffer<float>(Cube_cache_value* v) { return v->get_float_buffer(); }
error: expected initializer before ‘<‘ token
Please correct my code.
It looks like bad design in the first place. Why is your class storing 2 distinct vectors for 2 possible types? It’s a bloat and you’d need to do this for any other type you might want.
Why not do this ?