9 hours ago, MarcusAseth said:This seems wrong to me (unless your return type is a reference), the returned type from a function cannot be assigned to because it not a modifiable lvalue, regardless wether it has const or not.
That example still won't compile when trying to assign without const in the return type
Give it a try in http://cpp.sh/:
struct Vector2 {
Vector2(float x, float y)
: m_x(x), m_y(y) {}
float m_x, m_y;
};
Vector2 operator*(const Vector2 &v1, const Vector2 &v2) noexcept {
return Vector2(v1.m_x + v2.m_x, v1.m_y + v2.m_y);
}
int main() {
const Vector2 a(1.0f, 2.0f);
const Vector2 b(3.0f, 4.0f);
const Vector2 c(5.0f, 6.0f);
(a*b) = c;
}
and here you have a snippet which has an if test that will pass:
#include <iostream>
struct Vector2 {
Vector2(float x, float y)
: m_x(x), m_y(y) {}
operator bool() const noexcept {
return m_x != 0.0f && m_y != 0.0f;
}
float m_x, m_y;
};
Vector2 operator*(const Vector2 &v1, const Vector2 &v2) noexcept {
return Vector2(v1.m_x + v2.m_x, v1.m_y + v2.m_y);
}
int main()
{
const Vector2 a(1.0f, 2.0f);
const Vector2 b(3.0f, 4.0f);
const Vector2 c(5.0f, 6.0f);
if ((a*b) = c) {
std::cout << "Hello!";
}
}
And for std::string:
#include <iostream>
#include <string>
std::string f() {
return std::string("b");
}
int main() {
const std::string a = "a";
f() = a;
}
std::string does not provide implicit casting to bools