-
Notifications
You must be signed in to change notification settings - Fork 38
Description
Description
In the implementation of glsl::vector
, the following methods are used to return a pointer to the element after the last element:
Lines 31 to 33 in 8fd08ed
T* end() { return &m_data[size()]; } | |
const T* begin() const { return &m_data[0]; } | |
const T* end() const { return &m_data[size()]; } |
However, in Visual Studio (MSVC), runtime assertions prevent accessing any index out of range, including the "one-past-the-end" pointer returned by
end()
. This leads to a runtime exception when trying to access this position.
Problem
The MSVC runtime checks throw an exception due to the pointer being out of bounds, even though the pointer is intentionally pointing to the "past-the-end" element (the valid position after the last element in the container). The runtime enforcement causes issues with valid code and is not necessary for such access.
Workaround
There is a workaround to disable the runtime assertions by using the following preprocessor directive:
#define _CONTAINER_DEBUG_LEVEL 0 // Disable the debug level for the container
However, this is not a desirable solution, as it bypasses helpful runtime checks and reduces the safety of the code.
Suggestion
It would be better to adjust the implementation to avoid triggering MSVC's runtime assertions. One possible solution is to use std::vector::data() and compute the "one-past-the-end" pointer without triggering any bounds checking:
T* end() { return m_data.data() + size(); }
This approach should be safe and avoid the MSVC runtime assertions while maintaining the correctness of the code.