template<typename VertexType>
class CQuad
{
public:
VertexType tl, bl, tr, br;
void SetColor(const CColor& color)
{
//if VertexType has a member "color", then
tl.color = color;
bl.color = color;
tr.color = color;
br.color = color;
}
};
I wrote a template class called Quad with 4 vertex.
If the vertex type has a variable “color”, then I hope to instantiate a SetColor function to fill up all vertices’ color.
If no color variable member for VertexType, just don’t instantiate SetColor function.
I believe it’s possible to do that with SFINAE, but I failed after many trys.
Thanks in advance for your help!
I hope to get the correct code of SFINAE.
/////////////////I tried it out/////////////////////////////
template<typename VertexType, typename CColorType = void>
class CQuad
{
public:
VertexType tl, bl, tr, br;
};
template<typename VertexType>
class CQuad<VertexType, typename std::enable_if_t<std::is_same<CColor, decltype(VertexType::color)>::value>>
{
public:
VertexType tl, bl, tr, br;
void SetColor(const CColor& color)
{
tl.color = color;
bl.color = color;
tr.color = color;
br.color = color;
}
};
But I still feel confused of what I have done.
why cannot
class CQuad<VertexType, typename std::enable_if_t<decltype(VertexType::color)>::value>>?
why enable_if is so important? why can’t remove the keyword typename?
It seems my code is the only answer to run, but I have a strong feeling that we can simplify the code.
Could someone help explaining what is going on?
1