Search code examples
c++stringstdvectorassert

comparing std::vector to a raw value of the same type give error


I was doing an exercise while I noticed this

this code work okay:

std::vector<std::string> x = { "st1", "st1" };
std::vector<std::string> y = { "st1", "st1" };
assert(x == y); 

while this give me error when trying to compile it

std::vector<std::string> x = { "st1", "st1" };
assert(x == { "st1", "st1" }); 

I have no idea why is that the case, could someone explain why and how to force it to compile if there is way to do it.


Solution

  • The error should have told you something along the line of "{ "st1", "st1" } has no type".

    If you want to construct a second vector you need to call the constructor:

    assert(x == std::vector<std::string>{ "st1", "st1" });