Binding temporaries to non-const references in case of exceptions

Deadly 提交于 2020-01-15 08:39:05

问题


I have always read that temporaries are allowed to bind only with non-const reference arguments in case of function calls..

CASE 1:-

For example:-

class Simple{
    public: 
       int i;
       Simple(Simple &f)
       {
         i = f.i + 1;
       }
       Simple(int j)
       {
         i = j;
       }
  };

int main()
{
   Simple f1 = Simple(2);   // error no matching call fruit::fruit(fruit)...
   return 0;
}

This would give me error as I am trying to bind temporary with non-const reference arguments.

CASE 2:-

try
{
 throw e;
}
catch ( exception& e )
{
}

I have learnt when we throw an exception what really get passed to catch is the copy of original exception thrown i.e a temporary is created for the object thrown and then this would be passed to catch clause.

What catch is doing is catching this exception by non-const reference. This is in contrast to what I have shown in CASE 1.

So, my questions are:-

1) Are there specific scenarios where binding temporary to non-const reference is allowed.

2) If there are then what factors are taken into account while allowing these exceptions.


回答1:


Are there specific scenarios where binding temporary to non-const reference is allowed.

For lvalue-references (that is, the type T&) there isn't. You can't bind a temporary to a non-const lvalue-reference because it doesn't make much sense to modify, say, a literal like 42. They can bind to const lvalue-references because then a promise has been made not to modify the object to which the reference is bound.

They can in fact bind to rvalue-references (T&&), but that's unrelated to this thread.

If there are then what factors are taken into account while allowing these exceptions.

It is true that temporaries cannot bind to non-const lvalue-references, but there's a certain provision made for exception objects:

Taken from the C++11 standard (closest draft n3337):

§15.1/3 A throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type from “array of T” or “function returning T” to “pointer to T” or “pointer to function returning T”, respectively. The temporary is an lvalue and is used to initialize the variable named in the matching handler (15.3). [..]

emphasis mine

cppreference simplifies this into:

Unlike other temporary objects, the exception object is considered to be an lvalue argument when initializing the catch clause parameters, so it can be caught by lvalue reference, modified, and rethrown.

So for this case you can in fact bind the exception to a non-const lvalue-reference.



来源:https://stackoverflow.com/questions/26806857/binding-temporaries-to-non-const-references-in-case-of-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!