I am using GCC 7.4.0 and at some point it started giving me the following warning:
offset outside bounds of constant string
The reason is that the code is now templated and inlined and the compiler can now make some additional compile time checks. The code looks more or less like this:
template <typename StringType = std::string>
class StringWrapper {
public:
explicit StringWrapper(const typename StringType::value_type* string_buffer);
private:
static bool check(const typename StringType::value_type* string_buffer);
private:
StringType m_text;
};
template <typename StringType>
StringWrapper<StringType>::StringWrapper(
const typename StringType::value_type* string_buffer)
: m_text(check(string_buffer) ? string_buffer + 3U
: StringType{}) // the warning is at this line
{
}
template <typename StringType>
bool StringWrapper<StringType>::check(const typename StringType::value_type* string_buffer)
{
if (string_buffer == nullptr) {
return false;
}
return std::none_of(string_buffer, string_buffer + 3U,
[](const typename StringType::value_type& string_element) {
return string_element == '\0';
});
}
int main()
{
StringWrapper<> a("na");
return 0;
}
The compiler seems not to be able to understand at compile time that the function check()
as already validated the offset to be applicable without any risk. The reason why the compiler basically ignores the call to check()
is that the function unfortunately cannot be made constexpr
, and therefore it cannot be evaluated at compile time.
How can I disable only this specific warning and only for this specific portion of code?
I've found How to disable GCC warnings for a few lines of code but I do not know what -Wname_of_warning
should be used.