Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using this deleted function work?

Tags:

c++

struct S {
    S() {}
    S (const S &) = delete;
};

void f1 (S s) {}
void f2 (S &s) {}

int main() {
    S s;

    f2(s);
}

Since S(S &s) is deleted, why does using f2 not throw an error since when it was declared it passes in the arguments S &s? When I use f1(s) it throws an error. I've looked at the definition of deleted functions and I thought this would throw an error, but it doesn't. Why?

like image 314
template boy Avatar asked Dec 01 '22 23:12

template boy


1 Answers

Because passing by reference doesn't create a copy (which is what f2 uses).

f1, on the other hand, takes a pass-by-value parameter, which makes a copy.

like image 63
Luchian Grigore Avatar answered Jan 28 '23 10:01

Luchian Grigore