void

What's the difference between Void and no parameter?

戏子无情 提交于 2019-11-30 10:32:34
问题 I have a class which defines two overloaded methods public void handle(Void e) protected void handle() Obviously they are different, especially handle(Void e) is public . What's the difference between those two? How to call the first method? I am using handle(null) - is this correct? 回答1: The first function is a function of a single argument, which must be provided and can only validly take the value null . Any value other than null will not compile. The second function doesn't take any

Java 8 lambda Void argument

徘徊边缘 提交于 2019-11-30 10:13:36
问题 Let's say I have the following functional interface in Java 8: interface Action<T, U> { U execute(T t); } And for some cases I need an action without arguments or return type. So I write something like this: Action<Void, Void> a = () -> { System.out.println("Do nothing!"); }; However, it gives me compile error, I need to write it as Action<Void, Void> a = (Void v) -> { System.out.println("Do nothing!"); return null;}; Which is ugly. Is there any way to get rid of the Void type parameter? 回答1:

Is void{} legal or not?

痴心易碎 提交于 2019-11-30 08:09:35
This is a follow-up up of this question. In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression . That was fine, it made sense and that was all. Then I came through [7.1.7.4.1/2] ( placeholder type deduction ) of the working draft. There it is said that: [...] - for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type, T is the declared return type and e is the operand of the return statement. If the return statement has no operand, then e is void{} ; [...] So,

Why does C# not allow me to call a void method as part of the return statement?

与世无争的帅哥 提交于 2019-11-30 07:51:40
I am curious if there is a legitimate reason as to why C# does not support calling a void method as part of the return statement when the calling method's return type is also void. public void MethodA() { return; } public void MethodB() { return MethodA(); } So we would normally see this: public void MethodMeh() { if (expression) { MethodA(); return; } // Do more stuff } ... when we could be using this instead: public void MethodAwesome() { if (expression) return MethodA(); // Do more stuff } Is this a language limitation due to how C# handles void? Because it's simply the way the language is

How can I return a default value for an attribute? [duplicate]

拈花ヽ惹草 提交于 2019-11-30 06:06:13
This question already has an answer here: A get() like method for checking for Python attributes 3 answers I have an object myobject , which might return None . If it returns None , it won't return an attribute id : a = myobject.id So when myobject is None , the stament above results in a AttributeError : AttributeError: 'NoneType' object has no attribute 'id' If myobject is None, then I want a to be equal to None . How do I avoid this exception in one line statement, such as: a = default(myobject.id, None) Inbar Rose You should use the getattr wrapper instead of directly retrieving the value

Can a C++ function be declared such that the return value cannot be ignored?

这一生的挚爱 提交于 2019-11-30 06:01:33
I'm trying to determine whether a C++ function can be declared in such a way that the return value cannot be ignored (ideally detected at compile time). I tried to declare a class with a private (or in C++11, delete d) operator void() to try to catch the implicit conversion to void when a return value is unused. Here's an example program: class Unignorable { operator void(); }; Unignorable foo() { return Unignorable(); } int main() { foo(); return 0; } Unfortunately, my compiler (clang-703.0.31) says: test.cpp:2:5: warning: conversion function converting 'Unignorable' to 'void' will never be

Can there be a C++ type that takes 0 bytes

醉酒当歌 提交于 2019-11-30 04:47:41
问题 I'm trying to declare a C++ variable that takes up zero bytes. Its in a union, and I started with the type as int[0]. I don't know if that is actually zero bytes (although sizeof(int[0]) was 0). I need a better way to declare a 0 byte type, and hopefully one that can be typedefed to something like nullType or emptyType. The variable is in a union, so in the end memory is reserved anyway. I tried void on the off chance it would work, but C++ complained. I'm using Ubuntu 10.10, with a current

Object[] cannot be cast to Void[] in AsyncTask

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:24:53
I'm getting this error inside an extended asynctask, but i'm really sure that Object[] IS a Void[]. This is my custom AsyncTask: public abstract class RepeatableAsyncTask<A, B, C> extends AsyncTask<A, B, C> { private static final String TAG = "RepeatableAsyncTask"; public static final int DEFAULT_MAX_RETRY = 5; private int mMaxRetries = DEFAULT_MAX_RETRY; private Exception mException = null; /** * Default constructor */ public RepeatableAsyncTask() { super(); } /** * Constructs an AsyncTask that will repeate itself for max Retries * @param retries Max Retries. */ public RepeatableAsyncTask(int

Using `void_t` to check if a class has a method with a specific signature

ぐ巨炮叔叔 提交于 2019-11-30 04:08:23
问题 At the moment, I'm using this method to check if a class has a method with a specific signature. After attending Walter E. Brown's metaprogramming CppCon2014 talk, I started wondering if void_t could be used in this particular situation to make the code cleaner and more readable. However I'm having trouble thinking in terms of void_t - so far I understand that void_t can help me determine at compile-time whether or not an expression is valid. Example: template< class, class = void > struct

Array of POINTERS to Multiple Types, C

蓝咒 提交于 2019-11-30 04:04:11
Is it possible to have an array of multiple types by using malloc ? EDIT: Currently I have: #include <stdio.h> #include <stdlib.h> #include <string.h> #define int(x) *((int *) x) int main() { void *a[10]; a[0] = malloc(sizeof(int)); int(a[0]) = 4; char *b = "yola."; a[1] = malloc(strlen(b)*sizeof(char)); a[1] = b; printf("%d\n", int(a[0])); printf("%s\n", a[1]); } But it's messy. Other ways? EDIT: Cleaned it up a bit. You can't have an array of different types, exactly. But you can achieve a similar effect (for some purposes at least) in a number of different ways. If you just want a few