noreturn

When and how to use @noreturn attribute in Swift?

蹲街弑〆低调 提交于 2019-12-28 13:51:32
问题 I read the code block enclosed in curly braces after the keyword else in the context of a guard-else flow, must call a function marked with the noreturn attribute or transfer control using return , break , continue or throw . The last part is quite clear, while I don't understand well the first. First of all, any function returns something (an empty tuple at least) even if you don't declare any return type. Secondly, when can we use a noreturn function? Are the docs suggesting some core,

Overriding a [[noreturn]] virtual function

允我心安 提交于 2019-12-23 06:48:28
问题 The [[noreturn]] attribute can be applied to functions that are not meant to return. For example: [[noreturn]] void will_throw() { throw std::runtime_error("bad, bad, bad ...."); } But I've encountered the following situation (no, I didn't design this): class B { public: virtual void f() { throw std::runtime_error(""); } }; class D : public B { void f() override { std::cout << "Hi" << std::endl; } }; I would really like to place the attribute [[noreturn]] on the B::f() declaration. But I'm

How to use noreturn with function pointer?

ⅰ亾dé卋堺 提交于 2019-12-21 07:28:20
问题 I am writing a bootloader in C11. When the bootloader needs to transfer the control to the firmware, it reads a function pointer at a predefined memory address and calls it. The code looks like this: typedef void (FirmwareBootFn)(void); typedef struct { uint32_t stackPointer; FirmwareBootFn* programCounter; } FirmwareBootControl; static FirmwareBootControl g_bootControl __attribute__ ((section (".boot_control"))); void Firmware_boot( void ) { setStackPointer( g_bootControl.stackPointer ); g

Is there something like [[noreturn]] in C# to indicate the compiler that the method will never return a value?

允我心安 提交于 2019-12-19 19:18:08
问题 For some of my code I use a method which looks like this: public static void Throw<TException>(string message) where TException : Exception { throw (TException) Activator.CreateInstance(typeof(TException), message); } and I want to use it like this (simple example): public int MyMethod() { if(...) { return 42; } ThrowHelper.Throw<Exception>("Test"); // I would have to put "return -1;" or anything like that here for the code to compile. } now, obviously, I know that there is no way MyMethod

Why noreturn/__builtin_unreachable prevents tail call optimization

蓝咒 提交于 2019-12-19 09:17:10
问题 I have come to fact that all major compilers will not do tail call optimization if a called function does not return (i.e. marked as _Noreturn / [[noreturn]] or there is a __builtin_unreachable() after the call). Is this an intended behavior and not a missed optimization, and if so why? Example 1: #ifndef __cplusplus #define NORETURN _Noreturn #else #define NORETURN [[noreturn]] #endif void canret(void); NORETURN void noret(void); void foo(void) { canret(); } void bar(void) { noret(); } C:

_Noreturn in a struct in c: error: expected specifier-qualifier-list before '_Noreturn'

﹥>﹥吖頭↗ 提交于 2019-12-10 17:27:56
问题 I am trying to compile a piece of code that contains _Noreturn: #ifndef SOMEHEADER_H #define SOMEHEADER_H #include <stdalign.h> #include <stdbool.h> #include <stdint.h> extern struct s { _Noreturn void (*somenoreturnfunc)(bool); } svar; #endif Which gives me: error: expected specifier-qualifier-list before '_Noreturn' on: _Noreturn void (*somenoreturnfunc)(bool); So I tried the suggestion from here: #ifndef SOMEHEADER_H #define SOMEHEADER_H #include <stdalign.h> #include <stdbool.h> #include

Is there something like [[noreturn]] in C# to indicate the compiler that the method will never return a value?

喜夏-厌秋 提交于 2019-12-01 18:07:08
For some of my code I use a method which looks like this: public static void Throw<TException>(string message) where TException : Exception { throw (TException) Activator.CreateInstance(typeof(TException), message); } and I want to use it like this (simple example): public int MyMethod() { if(...) { return 42; } ThrowHelper.Throw<Exception>("Test"); // I would have to put "return -1;" or anything like that here for the code to compile. } now, obviously, I know that there is no way MyMethod could never return anything, because it will always (indirectly) throw an exception. But of course I get

How to declare a lambda's operator() as noreturn?

无人久伴 提交于 2019-12-01 15:45:17
How can the operator() of a lambda be declared as noreturn ? Ideone accepts the following code: #include <cstdlib> int main() { []() [[noreturn]] { std::exit(1); }(); return 0; } Clang 3.5 rejects it with: error: 'noreturn' attribute cannot be applied to types You can try it in godbolt: http://goo.gl/vsuCsF Which one is right? Update : the relevant standard sections appear to be 5.1.2.5, 7.6.3, 7.6.4 but after reading does it still isn't 100% clear to me (i) what is the right behavior, (ii) how to mark the operator() of a lambda as noreturn . Clang is correct. An attribute can appertain to a

How to declare a lambda's operator() as noreturn?

安稳与你 提交于 2019-12-01 13:51:19
问题 How can the operator() of a lambda be declared as noreturn ? Ideone accepts the following code: #include <cstdlib> int main() { []() [[noreturn]] { std::exit(1); }(); return 0; } Clang 3.5 rejects it with: error: 'noreturn' attribute cannot be applied to types You can try it in godbolt: http://goo.gl/vsuCsF Which one is right? Update : the relevant standard sections appear to be 5.1.2.5, 7.6.3, 7.6.4 but after reading does it still isn't 100% clear to me (i) what is the right behavior, (ii)

Why does “noreturn” function return?

房东的猫 提交于 2019-11-30 12:22:18
问题 I read this question about noreturn attribute, which is used for functions that don't return to the caller. Then I have made a program in C. #include <stdio.h> #include <stdnoreturn.h> noreturn void func() { printf("noreturn func\n"); } int main() { func(); } And generated assembly of the code using this: .LC0: .string "func" func: pushq %rbp movq %rsp, %rbp movl $.LC0, %edi call puts nop popq %rbp ret // ==> Here function return value. main: pushq %rbp movq %rsp, %rbp movl $0, %eax call func