I'm trying to do some Resolution struct which simply holds my width_ and height_ of screen. I will use it a lot in certain ways, and some methods will require a vector like data and some will require and internal structure-like data.
Example:
template<typename InternalType>
struct Resolution
{
InternalType width_;
InternalType height_;
std::vector<InternalType> vectorRepr()
{
return std::vector<InternalType>{width_, height_};
};
Vector2 vectorRepr()
{
return Vector2{width_, height_};
};
// maybe some other overloadings of vectorRepr()
}
Above example is NOT working, as vectorRepr is overloaded badly.
What I want to achive is to have in Resolution struct encapsulated methods to return me internal state in different data types. Template specialization might come in handy for this, but I'am having a hard time joining the idea of both Resolution templating and vectorRepr templating.
For me it looks similar as partial template specialization.
Close up to std::vector return type:
template<> // <- this is a specialization for vectorRepr
std::vector<InternalType> vectorRepr<std::vector>()
{
// but here, vector should know the InternalType, to embed it.
// so the InternalType is not specialized.
return std::vector<InternalType>{width_, height_};
}
Here I've found interesting example (but it is lacking a member attributes to return): How to specialize template function with template types
I am aware it can be done easy in different way, but at this point I'm just to curious.
Is it achivable ?
Playground:
https://godbolt.org/z/bE49vv3Ms
CodePudding user response:
In the example shown, I don't see a need for any specialization, partial or otherwise. This is sufficient:
template <typename Ret>
Ret vectorRepr() {
return {width_, height_};
}