return-type

function returning std::string crashes without return statement, unlike a function returning int without return statement

[亡魂溺海] 提交于 2021-02-19 03:47:09
问题 #include <iostream> #include <string> using namespace std; string crash() { } int noCrash() { } int main() { crash(); // crashes // noCrash(); // doesn't crash return 0; } The function crash(), crashes with Mingw g++ 4.6.2 and the function noCrash() executes with no issues. Why does the function returning string crash without a return statement? 回答1: Both are undefined behaviors, even noCrash can crash. 回答2: From standard 6.6.3/2 A return statement without an expression can be used only in

GCC: How to customising warning and errors generated from compilation

时光怂恿深爱的人放手 提交于 2021-02-11 12:51:38
问题 My Usecase: I have two c language files: ApplicationCode.c and UserCode.c . Application code is something generated by my app and UserCode is available to my application user where he can write his own code. ApplicationCode.c internally uses UserCode.c and calls out its methods/function. One thing you can assume here that ApplicationCode.c will never have any errors but could have warnings generated by gcc compiler. And UserCode.c can have both errors and warnings. So, While compiling code I

Return CompletableFuture<Void> or CompletableFuture<?>?

拜拜、爱过 提交于 2021-02-05 12:54:51
问题 I want to write an asynchronous method that returns a CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return CompletableFuture<Void> or CompletableFuture<?> ? Is there a reason to prefer one or the other, or are they interchangeable? CompletableFuture itself returns CompletableFuture<Void> from many of its methods. java.nio has a Future<Void> in AsynchronousSocketChannel: Future<Void> connect(SocketAddress remote

Why is an explicit cast not needed here?

核能气质少年 提交于 2021-02-04 17:10:06
问题 class MyClass { void myMethod(byte b) { System.out.print("myMethod1"); } public static void main(String [] args) { MyClass me = new MyClass(); me.myMethod(12); } } I understand that the argument of myMethod() being an int literal, and the parameter b being of type byte, this code would generate a compile time error. (which could be avoided by using an explicit byte cast for the argument: myMethod((byte)12) ) class MyClass{ byte myMethod() { return 12; } public static void main(String [ ] args

What does Void return type mean in Kotlin

怎甘沉沦 提交于 2021-01-20 17:44:48
问题 I tried to create function without returning value in Kotlin. And I wrote a function like in Java but with Kotlin syntax fun hello(name: String): Void { println("Hello $name"); } And I've got an error Error:A 'return' expression required in a function with a block body ('{...}') After couple of changes I've got working function with nullable Void as return type. But it is not exactly what I need fun hello(name: String): Void? { println("Hello $name"); return null } According to Kotlin

Template parameter as return type

喜欢而已 提交于 2020-08-22 19:55:21
问题 I stumbled upon this some time ago and I finally need to get an answer to this! The problem is as follows: #include <iostream> using namespace std; template <class FIRST, class SECOND> FIRST multiply(FIRST a, SECOND b){ return a * b; } int main(){ int x = 39; double y = 38.38; cout << multiply(x, y) << endl; } In this C++ code I have two template parameters and the return type of the function is the type of one parameter. My problem with that is that the return type has to be (in this case)

Template parameter as return type

橙三吉。 提交于 2020-08-22 19:53:33
问题 I stumbled upon this some time ago and I finally need to get an answer to this! The problem is as follows: #include <iostream> using namespace std; template <class FIRST, class SECOND> FIRST multiply(FIRST a, SECOND b){ return a * b; } int main(){ int x = 39; double y = 38.38; cout << multiply(x, y) << endl; } In this C++ code I have two template parameters and the return type of the function is the type of one parameter. My problem with that is that the return type has to be (in this case)