Search code examples
c++exceptionlanguage-lawyerpass-by-valuenoexcept

Where are exceptions thrown when arguments are passed by value


I have a type throwing when copied:

struct A { A(const A&) { throw 1; } };

void doit(A)
{
}

int main()
{
    A a;
    doit(a);
    return 0;
}

Is the exception thrown inside or outside of the function? Can I declare the function as noexcept?


Solution

  • See C++17 [expr.call]/4

    ... The initialization and destruction of each parameter occurs within the context of the calling function. [ Example: The access of the constructor, conversion functions or destructor is checked at the point of call in the calling function. If a constructor or destructor for a function parameter throws an exception, the search for a handler starts in the scope of the calling function; in particular, if the function called has a function-try-block (Clause 18) with a handler that could handle the exception, this handler is not considered. — end example ]

    So the exception is, as you would put it, thrown "outside of the function". You can declare it noexcept.