Open mmore500 opened 5 years ago
issue seems to be related to emp::vector<std::string>
vs. std::vector<std::string>
When we are not in debug mode, the emp::vector becomes an std::vector, thus allowing them to be used interchangable. When we are in debug mode, emp::vector becomes a class derived from std::vector, and thus can be passed in anywhere the an std::vector is required. The reverse, however, is not true and not particularly trivial since emp::vector has extra internal state.
I think from my own perspective, I was assuming that if people are using an emp function, they'll be using emp::vector, but that's not very friendly to those who want to port their code over.
I'm not actually sure of the best way to solve this problem; anyone else have ideas?
(This is also relevant since I'm currently building a similar emp drop-in replacement for map that will find other types of common bugs.)
Maybe we could provide a user-defined conversion from emp::vector
to std::vector
... something a la
operator std::vector<T>() const { TODO }
As mentioned, we'd lose internal state information so perhaps we could print a run time warning when the conversion is used?
Unfortunately the conversion won't help in most cases. Vectors tend to be passed around as references for speed purposes (either because you don't want to take the time to copy the whole thing or because the function you're calling with it might change it).
I am toying with some other ideas though. We might be able to use a move constructor to build an emp::vector on the fly from the std::vector and then pass the contents back again.
Would that help in this case? My understanding is that the snipped above doesn't compile because we need to get a std::vector
(e.g., std::vector<std::string> args
) from an emp::vector
(e.g., the return type of emp::cl::args_to_strings()
) and not the other way around
That direction works; the first line of main() compiles fine. Since emp::vector is derived from std::vector the std::vector constructor will automatically convert it back (and an operator std::vector isn't needed).
The SECOND line, however, is where the problem is. In the failing example, we're trying to pass an std::vector into a function that is expecting a reference to an emp::vector and we can't do that kind of conversion.
Ah, I did misunderstand. That makes a lot more sense now...
The specific conversion point may be off, but you're exactly right that we need to think more through the nature of these conversions.
compiles:
won't compile:
giving the compilation error message