I can not get my head around the following problem. I don't even really know how I could approach it.
Consider this code:
struct fragment_shader {
std::string mPath;
};
struct vertex_shader {
std::string mPath;
};
template <typename T>
T shader(std::string path) {
return T{ path };
}
To create the different structs, I can write the following:
auto fragmentShader = shader<vertex_shader>("some_shader.frag");
auto vertexShader = shader<fragment_shader>("some_shader.vert");
I am wondering, if it is possible to let the compiler figure out the type based on the path
parameter which is passed to the shader
function, so I would only have to write:
auto fragmentShader = shader("some_shader.frag");
auto vertexShader = shader("some_shader.vert");
and because of the file ending ".frag", the type fragment_shader
would be inferred, and for a path ending with ".vert", vertex_shader
would be inferred.
Is that possible?
I was reading up a bit on enable_if
, but actually I have no idea how I could use that to achieve what I am trying to achieve. I would try something like follows:
template<>
typename std::enable_if<path.endsWith(".frag"), fragment_shader>::type shader(std::string path) {
return fragment_shader{ path };
}
template<>
typename std::enable_if<path.endsWith(".vert"), vertex_shader>::type shader(std::string path) {
return vertex_shader{ path };
}
But obviously, this doesn't compile. It's just to make clear what I am trying to do.