void

Construction of a void Type?

一世执手 提交于 2019-11-27 13:40:27
问题 I was given a piece of code that uses void() as an argument. The code doesn't compile... obviously? Can we instantiate anything of type void ? I believed the answer was no, with the exception of a void* . For example: Writing the function void askVoid(void param) {} errors: A parameter may not have void type Writing the function void askNaught() {} and calling it with askNaught(void())` errors: error C2660: takeNaught : function does not take 1 arguments Writing the templatized function

Is sizeof(void()) a legal expression?

社会主义新天地 提交于 2019-11-27 13:24:15
问题 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

When printf is an address of a variable, why use void*?

亡梦爱人 提交于 2019-11-27 12:54:58
I saw some usage of (void*) in printf() . If I want to print a variable's address, can I do it like this: int a = 19; printf("%d", &a); I think, &a is a 's address which is just an integer, right? Many articles I read use something like this: printf("%p", (void*)&a); What does %p stand for? (A pointer?) Why use (void*) ? Can't I use (int)&a instead? Pointers are not numbers. They are often internally represented that way, but they are conceptually distinct. void* is designed to be a generic pointer type. Any pointer value (other than a function pointer) may be converted to void* and back again

Why does this Java 8 lambda fail to compile?

删除回忆录丶 提交于 2019-11-27 11:51:05
问题 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

Returning from a void function [closed]

帅比萌擦擦* 提交于 2019-11-27 11:18:05
Which is more correct way to return from function: void function() { // blah some code } OR void function() { // blah some code return; } Rationale for second way: It expresses developer intentions more clearly. It helps detecting function end at pre-compile time: Suppose you have such scenario- you have bunch of functions and you must inject some code at the end of those functions. But for some reasons you don't want / or can't modify such huge amount of functions. What can you do about that ? Return & macro comes into play, for example: #include<stdio.h> #define MAX_LINES 1000 #define XCAT(a

Understanding the difference between f() and f(void) in C and C++ once and for all

Deadly 提交于 2019-11-27 10:39:52
Ok, so I have heard different opinions on this subject and just want to make sure I understand it correctly. For C++ Declarations void f(); and void f(void); mean precisely the same thing, the function f does not take any parameters. Ditto for definitions. For C Declaration void f(void); means that f does not take any parameters. Declaration void f(); means that function f may or may not have parameters, and if it does, we don't know what kind of parameters those are, or how many there is of them. Note that it is NOT the same as ellipsis, we can't use va_list . Now here is where things get

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

∥☆過路亽.° 提交于 2019-11-27 10:35:48
问题 void f() means that f returns nothing. If void returns nothing, then why we use it? What is the main purpose of void ? 回答1: 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

Returning a void?

假如想象 提交于 2019-11-27 09:11:43
I do not understand why this code compiles without error: #include <iostream> template <class T> struct Test { static constexpr T f() {return T();} }; int main() { Test<void> test; test.f(); // Why not an error? return 0; } Is it ok according to the standard, or is it a compiler tolerance? Shafik Yaghmour This looks valid by the draft C++11 standard , if we look at section 5.2.3 Explicit type conversion (functional notation) paragraph 2 says ( emphasis mine ): The expression T() , where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv

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

三世轮回 提交于 2019-11-27 08:35:36
问题 This question already has an answer here : What does “Incompatible types: void cannot be converted to …” mean? (1 answer) Closed 2 years ago . 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

What is System.Void?

感情迁移 提交于 2019-11-27 07:34:11
I know that methods declared with void does not return anything. But it seems that in C# void is more then just a keyword, but a real type. void is an alias for System.Void like int that is for System.Int32 . Why am I not allowed to use that type? It does not make any sense, but this are just some thoughts about the logic. Neither var nothing = new System.Void(); (which says i should use void (Not an alias?)) nor var nothing = new void(); compiles. It is also not possible to use something like that: void GiveMeNothing() { } void GiveMeNothingAgain() { return GiveMeNothing(); } So whats the