-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot implicitly convert between a subclass of std::vector<T*> and tcb::span<const T* const> #56
Comments
Subclassing https://gcc.godbolt.org/z/Y68boPvYo Are you able to give any more information about the error message that might help me recreate the problem, or perhaps try with another compiler? |
I was trying to reproduce it w/ something closer to my code, but it was still working, but when I switched to MSVC even the std::vector case broke! (maybe I got confused earlier about it working) See https://gcc.godbolt.org/z/e3q5e4PW5 It looks like it doesn't like it when it needs to add const? |
I think the problem boils down to this: #include <type_traits>
static_assert(std::is_convertible<int* (*)[], const int* const (*)[]>::value, "");
int main() {} On compiler explorer, this compiles fine with:
This does not compile with x64 MSVC 19.14 with If we remove the first |
Thank you very much for the detective work @kimci86! After some more investigation, this seems to be caused by CWG issue 330, which was reported in 2002 and finally resolved via N4261 in 2014. So that's the cause, but unfortunately there are no simple solutions that I can think of. The condition that's being checked -- can we convert a pointer-to-array-of-U into a pointer-to-array-of-T -- is directly lifted from the Unless anyone has any clever ideas, I'm afraid that this may just be something that we have to say is unsupported in older compilers. (Note that |
Wouldn't it be possible to 'wrap' |
The goal is to provide |
As an update, I tried to implement the algorithm from the linked paper myself, and it seems to work? I included the following before
Just putting it out there (and would love to hear if this approach is flawed or I missed something). |
I have an
AutoVector<T>
template class in our codebase which inherits publicly fromstd::vector<T*>
and handles ownership (created before the days of C++11).I've noticed that I get compiler errors when I try to have an instance of this type implicitly convert to the associated span type, for example, something like
similar code works fine when using
std::vector<T*>
directly. Looking at the C++20 std::span documentation, this should work as long as theAutoVector<T>
type models theRange
concept, and I think if std::vector does, so should a subclass (that doesn't explicitly delete any member functions)?I'm having the problem in Visual Studio 2019, haven't tried any other compilers/platforms yet.
The text was updated successfully, but these errors were encountered: