我一直在看一个Qt 教程,它使用了我以前没见过的结构:
(void) new QShortcut(Qt::Key_Enter, this, SLOT(fire()));
(void) new QShortcut(Qt::Key_Return, this, SLOT(fire()));
(void) new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
Run Code Online (Sandbox Code Playgroud)
我已经尝试了这个没有(void)它仍然编译和工作,所以这是什么目的(void)?
我有以下方法,我想知道是否有任何可以在下面的默认(void),因为有一个编译器错误,说这里的void无效:
private void applyDefaultsIfNecessary(ApplicationConfiguration configuration)
{
var defaults = new Dictionary<Predicate<ApplicationConfiguration>, Action<ApplicationConfiguration>>()
{
// { rule, action } - if rule is true, execute action
{ (c) => c.ConnectionString == null , (c) => c.ConnectionString = "foo" },
{ (c) => c.OutputExcelFilePath == null, (c) => c.ConnectionString = "bar" },
{ (c) => c.OutputDirectory == null, (c) => c.OutputDirectory = "baz" }
};
//Nothing to select, but we want to loop throough the dict and invoke action, if …Run Code Online (Sandbox Code Playgroud) 让我们说我希望C macro适用于任何类型.我正在使用GCC编译器(> = 4.6)并且可以使用GNU99宏.
//code...
any_type_t *retVal = function_that_runs_very_long_time(a, b, &&c, **d, &e, *f);
//other code...
Run Code Online (Sandbox Code Playgroud)
用于TIMER的宏的用法可以看起来像这样
//code...
any_type_t *retVal =
TIMER(
function_that_runs_very_long_time(a, b, &&c, **d, &e, *f),
"TIMING FOR VALUE <%d, %d>", a, b
);
//other code...
Run Code Online (Sandbox Code Playgroud)
因此TIMER必须返回给定函数的值和其运行的打印持续时间.具有void返回类型的函数存在问题.
我显然可以有两个宏,如TIMER_TYPE和TIMER_VOID,但我想使用单一的时间函数与任何返回值.
谢谢你的建议.
编辑此TIMER宏的示例
#define TIMER(expr, fmt_msg, ...) \
({ \
struct timeval before, after; \
uint64_t time_span; \
int time_span_sec, time_span_usec; \
gettimeofday(&before, NULL); \
typeof(expr) _timer_expr__ = (expr); \ // <- static if?
gettimeofday(&after, NULL); \
time_span = …Run Code Online (Sandbox Code Playgroud) 什么是虚空?有人提供一些例子,正确使用虚空吗?当我们写void main (void)或有main()什么区别?
我已成功async void在ASP.NET Web窗体页面上使用方法.但是,当我尝试在Web用户控件中使用相同的方法,然后将此Web用户控件放到具有async="true"设置的页面时,我不断收到此错误:
此时无法启动异步操作.异步操作只能在异步处理程序或模块中启动,或者在页面生命周期中的某些事件中启动.如果在执行页面时发生此异常,请确保将页面标记为<%@ Page Async ="true"%>.
所以问题是,can void async方法可以用于Web用户控制并嵌入到启用异步的Web表单页面中吗?
我已经使用LiveScript很长一段时间了,我注意到在undefined隐式返回的情况下,使用表达式void 8代替.
当然,我理解使用void,但我无法弄清楚为什么特别使用整数8.
例如,以下LiveScript:
x = if truthy then \success!
Run Code Online (Sandbox Code Playgroud)
将编译为:
var x;
x = truthy ? 'success!' : void 8;
Run Code Online (Sandbox Code Playgroud) 以下内容无法编译:
template<void *p>
class X {
// ...
};
int r;
int main()
{
X<&r> x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误消息是
x.cc:10:6:错误:无法将模板参数'&r'转换为'void*'
显式地转换&r到(void*)也无济于事.错误消息变为:
x.cc:10:14:错误:无法将模板参数'(void*)(&r)'转换为'void*'
标准的哪一部分指明了这种行为?GCC版本是gcc版本5.2.1 20151003(Ubuntu 5.2.1-21ubuntu2)
编辑:
请注意,使用例如int*而不是void*按预期工作.
编辑:(回答自己)
它并不能一起工作的gcc HEAD 6.0.0 20151016(实验)指定-std = C++ 1Z,既不与隐式也不与显式转换到"无效*"时.
它确实与clang HEAD 3.8.0(主干250513)一起使用,并且在指定--std = c ++ 1z并显式转换为*void*时已经(至少)clang 3.6.0(标记/ RELEASE_360/final)没有明确的演员,clang抱怨如下:
x.cc:10:7:错误:在转换的常量表达式中不允许从'int*'转换为'void*'
负责修复c ++语言规范中的这个bug的是N4268,它已经实现了.
请考虑以下代码:
template<typename F>
struct S;
template<typename Ret, typename... Args>
struct S<Ret(Args...)> { };
template<typename... Args>
using Alias = S<void(Args...)>;
int main() {
S<void(int)> s;
Alias<int> alias;
}
Run Code Online (Sandbox Code Playgroud)
它正常工作,如预期的那样,涉及的线S和涉及Alias引擎盖的相同类型S<void(int)>.
现在,请考虑以下更改:
int main() {
S<void(void)> s; // this line compiles
Alias<void> alias; // this line does not
}
Run Code Online (Sandbox Code Playgroud)
我期望它编译,原因与上面提到的类似.
不言而喻,由于涉及到行,它不会编译Alias,而是我收到错误:
用'Alias = S [with Args = {void}]代替'模板'
[...]
错误:参数类型无效'void'
问题很简单:我错过了什么?
当异步服务没有返回值但我想使用Observables我倾向于使用Observable<boolean>.但我对这个布尔值没有意义,因为服务失败或成功,如果失败,我希望Observable处于错误状态.这只为观察到的布尔值留下了"真实"值.
以下是Observable<void>针对这些情况使用的好模式吗?或者是否存在可预见的使用问题Observable<void>
const asyncFuncWithoutResult = (): Observable<void> => {
// fake something async that has no return value
const itWorked = true; // or not
if (itWorked) {
return Observable.of();
} else {
return Observable.throw(Error('It did not work'));
}
}
// call the service
asyncFuncWithoutResult()
.subscribe(
undefined, // nothing will be emitted when using Observable.of()
(err: any) => console.error(err), // handle error state
() => console.log('Success'), // handle success state
);
Run Code Online (Sandbox Code Playgroud) 这是一个简单的类,说明了我的问题:
package com.example;
import java.util.function.*;
public class App {
public static void main(String[] args) {
App a1 = new App();
BiFunction<App, Long, Long> f1 = App::m1;
BiFunction<App, Long, Void> f2 = App::m2;
f1.apply(a1, 6L);
f2.apply(a1, 6L);
}
private long m1(long x) {
return x;
}
private void m2(long x) {
}
}
Run Code Online (Sandbox Code Playgroud)
f1,指的是App::m1,和被绑定到a1中f1的来电apply,工作完全正常-编译器是幸福的呼叫可以通过f1.apply就好制成.
f2,指App::m2,不起作用.
我希望能够定义一个没有返回类型的未绑定非静态方法的方法引用,但我似乎无法使其工作.
void ×10
c++ ×3
c ×2
templates ×2
.net ×1
asp.net ×1
asynchronous ×1
c# ×1
c++11 ×1
compile-time ×1
gcc ×1
java ×1
java-8 ×1
java-stream ×1
javascript ×1
lambda ×1
livescript ×1
observable ×1
rxjs ×1
standards ×1
syntax ×1
types ×1
typescript ×1