This function:
template<typename T>
void f(int stack, std::vector<T> *vec, void (*readItemFunc)(int, T*))
{
// omitted irrelevant code
for(size_t i = 0; i < sz; i++)
{
readItemFunc(stack, &vec->at(i));
}
}
fails to compile, because of how std::vector<bool>
is specialized. Clang++ reports this compile error:
no viable conversion from
__bit_iterator<std::__1::vector<bool, std::__1::allocator<bool> >, false>
tobool *
In C++17 I could do:
template<typename T>
void f(int stack, std::vector<T> *vec, void (*readItemFunc)(int, T*))
{
// omitted irrelevant code
for(size_t i = 0; i < sz; i++)
{
if constexpr(std::is_same<T, bool>::value)
{
T v;
readItemFunc(stack, &v);
(*vec)[i] = v;
}
else
{
readItemFunc(stack, &vec->at(i));
}
}
}
however it would be the only line requiring C++17, while the rest of the project supports back to C++11, so I'd try to stick with C++11 until it makes sense.
Considering that the "omitted irrelevant code" is quite long, and depends on T
, specializing f()
for bool
would involve a lot of code duplication.
In what other way I can solve this issue for C++11?
Aucun commentaire:
Enregistrer un commentaire