Let's say I have the function:
void foo(Object& o) {
/* only query o, dont alter it*/
}
Is it possible to call this function only with already constructed objects and have Visual Studio throw a compile error if I call the function with a temporary object?
struct Object {
/*Members*/
}
void foo(Object& o) {
/* only query o, dont alter it*/
}
int main() {
Object o = Object();
foo(o); // allow this
foo(Object()) // but disallow this
}
If your parameter is not
const, the function won't accept temporaries.If your parameter is
const, the function accepts both temporary and regular objects.But if you want to prevent that, you can use the following
Live