void

Is sizeof(void()) a legal expression?

ぐ巨炮叔叔 提交于 2019-11-28 21:01:22
From [5.3.3/1] , I found that: The sizeof operator shall not be applied to an expression that has function or incomplete type From [3.9/5] I found that: Incompletely-defined object types and cv void are incomplete types Anyway, for sizeof does not evaluate it's operands, I would have said that sizeof(void()) was a legal expression (actually GCC compiles it and the result is 1). On the other side, from here , void is not mentioned while discussing sizeof , neither when the types having size 1 are mentioned, nor in the list of the ones having an implementation defined size. The question is thus:

Why does this Java 8 lambda fail to compile?

与世无争的帅哥 提交于 2019-11-28 19:59:30
The following Java code fails to compile: @FunctionalInterface private interface BiConsumer<A, B> { void accept(A a, B b); } private static void takeBiConsumer(BiConsumer<String, String> bc) { } public static void main(String[] args) { takeBiConsumer((String s1, String s2) -> new String("hi")); // OK takeBiConsumer((String s1, String s2) -> "hi"); // Error } The compiler reports: Error:(31, 58) java: incompatible types: bad return type in lambda expression java.lang.String cannot be converted to void The weird thing is that the line marked "OK" compiles fine, but the line marked "Error" fails.

If void() does not return a value, why do we use it?

这一生的挚爱 提交于 2019-11-28 17:19:25
void f() means that f returns nothing. If void returns nothing, then why we use it? What is the main purpose of void ? When C was invented the convention was that, if you didn't specify the return type, the compiler automatically inferred that you wanted to return an int (and the same holds for parameters). But often you write functions that do stuff and don't need to return anything (think e.g. about a function that just prints something on the screen); for this reason, it was decided that, to specify that you don't want to return anything at all, you have to use the void keyword as "return

incompatible types: void cannot be converted to int [duplicate]

为君一笑 提交于 2019-11-28 14:23:29
This question already has an answer here: What does “Incompatible types: void cannot be converted to …” mean? 1 answer I'm extremely new when to comes to Java and programming in general. I am trying to create a simple program where you guess my age and if you are right it will say "correct" and if you are wrong it will say "wrong". This is my code: import java.util.InputMismatchException; import java.util.Scanner; // This will import just the Scanner class. public class GuessAge { public static int main(int[] args) { System.out.println("\nWhat is David's Age?"); Scanner userInputScanner = new

variable or field declared void

佐手、 提交于 2019-11-28 09:35:14
I have a function called: void initializeJSP(string Experiment) And in my MyJSP.h file I have: 2: void initializeJSP(string Experiment); And when I compile I get this error: MyJSP.h:2 error: variable or field initializeJSP declared void Where is the problem? It for example happens in this case here: void initializeJSP(unknownType Experiment); Try using std::string instead of just string (and include the <string> header). C++ Standard library classes are within the namespace std:: . This is not actually a problem with the function being "void", but a problem with the function parameters. I

What does (void)var actually do?

半城伤御伤魂 提交于 2019-11-28 08:24:08
Consider the following main() : int main(int argc, char *argv[]) { return (0); } Upon compilation with cc -Wall -Wextra , warnings saying "unused parameter" get generated. When I do not need to use a parameter in a function (for instance in a signal handler function that makes no use of its int parameter), I am used to doing the following: int main(int argc, char *argv[]) { (void)argc; (void)argv; return (0); } (For that particular main() , I sometimes see other people do: argv = argv - argc + argc ) But what does (void)var actually do ? I understand that (void) is a cast, so I guess I am

Why can't we declare a variable of type void?

主宰稳场 提交于 2019-11-28 06:13:11
I'm looking for a formal explanation of that fact in the Standard. I've found what 3.9.1/9 says and trying to give an explanation used that section. Section 3.9.1/9, N3797 : The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void (5.4). An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of

How do I mock a static method that returns void with PowerMock?

余生颓废 提交于 2019-11-28 03:46:58
I have a few static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method that has a return type other than void. But how can I mock a static method that returns void to just " doNothing() "? The non-void version uses these lines of codes: @PrepareForTest(StaticResource.class) ... PowerMockito.mockStatic(StaticResource.class); ... Mockito.when(StaticResource.getResource("string")).thenReturn("string"); However if applied to a StaticResources that returns void , the compile will complain that when(T) is not

What do I return if the return type of a method is Void? (Not void!)

家住魔仙堡 提交于 2019-11-28 03:10:33
Due to the use of Generics in Java I ended up in having to implement a function having Void as return type: public Void doSomething() { //... } and the compiler demands that I return something . For now I'm just returning null , but I'm wondering if that is good coding practice... I'm asking about V ‌oid, not v ‌oid. The class Void , not the reserved keyword void . I've also tried Void.class , void , Void.TYPE , new Void() , no return at all, but all that doesn't work at all. (For more or less obvious reasons) (See this answer for details) So what am I supposed to return if the return type of

How much existing C++ code would break if void was actually defined as `struct void {};`

心不动则不痛 提交于 2019-11-28 02:30:52
问题 void is a bizarre wart in the C++ type system. It's an incomplete type that cannot be completed, and it has all sort of magic rules about the restricted ways it can be employed: A type cv void is an incomplete type that cannot be completed; such a type has an empty set of values. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void ([expr.cast]). An expression of type cv void shall be used only as an expression