I don't quite understand what the following does:
std::vector<const char*> getRequiredExtensions()
{
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
if (enableValidationLayers) {
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}
return extensions;
}
I don't understand this line:
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
What exactly does it do? And what does glfwExtensions resolve to, I only get that it's a double pointer (Which is also a new concept for me). And what does the glfwExtensions + glfwExtensionCount mean?
thanks for any help!
Let's concentrate on the following line of code:
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
This declares extensions
as a variable of the templated std::vector<typename T>
type, where the T
type resolves to const char*
(that is, it declares a vector whose elements are each pointers to constant character data). This vector object is initialized using the constructor that takes two iterator arguments – the form (5) on this cppreference page.
A point of confusion here may be how the arguments are iterators: well, pointers are also iterators (in many ways). In this case, the const char** glfwExtensions;
line declares a variable that points to a const char*
(which is the type of the vector's elements), and the call to glfwGetRequiredInstanceExtensions
makes this point to the beginning of an array of such (const char*
) pointers and sets the glfwExtensionCount
variable to the number of elements in that array. Further, adding the size of an array to a pointer to its first elements yields a pointer to "one past the end" of the array – which is equivalent to an STL "end" iterator.
Thus, the two arguments to the constructor are, effectively, acting as std::begin(glfwExtensions)
and std::end(glfwExtensions)
– but those functions can't be used directly, because glfwExtensions
is not an array of known size in this context.