Can Lithuanian int assignment in C ++ throw an exception?

This code is as follows:

void f()
{
   int i;
   i = 0;
}

Is it possible that the system may throw an exception due to a simple assignment?

[Edit: For those who say, โ€œNo exception can happen,โ€ can you point Me toward the part of the C ++ standard that talks about this? It's hard for me to find him.]

+5
source share
5 answers

Although it will probably be difficult for you to find confidence in it in the standard, a simple rule of thumb is that anything that is legitimate in C may not give up. [Edit: the closest that I know of, a direct statement on this subject is in ยง15 / 2, which states that:

, throw, "throw exception"; [...]

, , throw, .]

: UB. - ++, , , operator = new.

. , , . , , . , , - .

+7

, , :

int operator"" _t(const char *) { throw 0; } // C++11 user defined literal 

struct foo {
  foo(int) { throw 0; }
  operator int() { throw 0; }
  foo& operator=(int) { throw 0; }
};

int main() {
  int i;
  i = 0; // can't throw
  i = 0_t; // User defined literal throws
  foo f = 0; // Constructor throws
  i = f; // conversion operator throws
  f = 0; // assignment throws
  f = f; // both conversion and assignment would like to throw
}

( ++ 11)

+6

, 0 ( int) int, ยง 5.17 , , . int, ยง5.17 , " cv-unqualified . " :

  • , int, ( C : int , , )

  • , undefined, int, undefined ( )

  • , . .

: , , , . , operator=. , , .

+3

: operator=() ; , , operator T() . , - , , , .

+2

int, - .

- , , , (, ).

0

All Articles