I've been working on a project, but I got a weird error. I can't pass 2 Shader instances to a variadic constructor, even though it says it supports 2 Shader parameters.
Exact error message: E0289 no instance of constructor "Program::Program" matches the argument list argument types are: (Shader, Shader)
Here is code:
#pragma once
#include "Renderer.h"
#include "Shader.h"
class Program
{
public:
template<Shader&... shaders>
Program()
{
GLCall(m_RendererID = glCreateProgram());
AssignShaders(shaders...);
GLCall(glLinkProgram(m_RendererID));
GLCall(glValidateProgram(m_RendererID));
GLCall(glUseProgram(m_RendererID));
}
Program(const std::string& vertexShaderFilepath, const std::string& fragmentShaderFilepath)
{
Shader vertexShader(vertexShaderFilepath, GL_VERTEX_SHADER);
Shader fragmentShader(fragmentShaderFilepath, GL_FRAGMENT_SHADER);
Program(vertexShader, fragmentShader); // error is here
}
private:
unsigned int m_RendererID;
template<typename First, typename ...Shaders>
void AssignShaders(First& first, const Shaders&... other)
{
Shader& shader = (Shader&)first;
GLCall(glAttachShader(m_RendererID, shader.GetRendererID()));
AssignShaders(other...);
}
void AssignShaders() {};
};
Here is the line which calls the Program constructor:
Program program("resources/shaders/basic_vert.glsl", "resources/shaders/basic_frag.glsl");
Your constructor is missing an argument list. It got a variadic template parameter, but an explicitly empty argument list.
Apart from that, as @richardcritten mentioned, you didn't delegate the constructor as you intended, but would have created a temporary instance of Program
instead. Delegate (by using initializer list), or call AssignShaders
directly.
Also don't forget to call glDeleteProgram
in the destructor of this class, or you will leak OpenGL resources!