为什么以下代码无法编译?即使这样做也是合法的void* ptr = 0;
template <void* ptr = 0>
void func();
int main() {
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我问,因为我发现一个非常值得信赖的来源做了类似的事情,但无法在我的机器上编译
注意应该已经发布了编译器错误以及我的问题,所以在这里
so_test.cpp:1:23: error: null non-type template argument must be cast to template parameter type 'void *'
template <void* ptr = 0>
^
static_cast<void *>( )
so_test.cpp:1:17: note: template parameter is declared here
template <void* ptr = 0>
^
so_test.cpp:5:5: error: no matching function for call to 'func'
func();
^~~~
so_test.cpp:2:6: note: candidate template ignored: substitution failure [with ptr = …Run Code Online (Sandbox Code Playgroud) 在我的回答中,我提到解除引用void指针是一个坏主意.但是,当我这样做时会发生什么?
#include <stdlib.h>
int main (void) {
void* c = malloc(4);
*c;
&c[0];
}
Run Code Online (Sandbox Code Playgroud)
汇编:
gcc prog.c -Wall -Wextra
prog.c: In function 'main':
prog.c:4:2: warning: dereferencing 'void *' pointer
*c;
^~
prog.c:5:4: warning: dereferencing 'void *' pointer
&c[0];
^
prog.c:5:2: warning: statement with no effect [-Wunused-value]
&c[0];
^
Run Code Online (Sandbox Code Playgroud)
这是来自Wandbox的图片,对于那些说它没有发生的人:
和Live demoIdeone中的一个.
它实际上会尝试读取c指向的内存,然后获取该结果,但实际上什么都不做?或者这条线根本没有效果(但GCC不会产生警告).
我在想,既然编译器对数据类型一无所知,在不知道类型大小的情况下,它将无法做很多事情.
为什么derefencing a void*不会产生错误,只是警告?
如果我尝试作业,我会收到错误:
无效使用void表达式
但不应该单独解除引用会产生错误吗?
我在 TypeScript 上遇到了一个奇怪的问题。我最近了解了void ...operator,因为我需要应用它所以eslint不会报告no-floating-promises。然而,这个特定的代码片段以某种方式导致了我无法在 TypeScript Playground 上重现的问题:
class A {
async a() {}
async onTickAsync(repeat: boolean) {
try {
await this.a();
} catch(e) {
console.error(e);
} finally {
if (repeat) {
window.setTimeout(() => void this.onTickAsync(true), 200);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
VS Code 会报这个错误:
TS7011:缺少返回类型注释的函数表达式隐式具有“any”返回类型。
但是,该问题在TS Playground上无法重现。VS Code 和 Playground 都使用 TypeScript 4.5.4。这是我的tsconfig.json:
class A {
async a() {}
async onTickAsync(repeat: boolean) {
try {
await this.a();
} catch(e) { …Run Code Online (Sandbox Code Playgroud) 我见过一个函数,其原型是:
int myfunc(void** ppt)
Run Code Online (Sandbox Code Playgroud)
该函数在C文件中作为a = myfunc(mystruct**var1)调用;
其中mystruct是我们拥有的结构之一的typedef.
这在MSVC6.0中没有任何编译错误,但是当我用其他一些C编译器编译它时,它会在调用此函数的地方出错,并显示错误消息:
类型mystruct**的参数与void**类型的参数不兼容
myfunc()的参数保持为void**,因为它似乎是一个通用的malloc类函数,可以使用各种结构变量类型进行内存分配
mystruct**,但它没有工作]-广告
是否可以使用多种类型的数组malloc?
编辑:
目前我有:
#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]);
}
Run Code Online (Sandbox Code Playgroud)
但它很乱.其他方法?
编辑:清理了一下.
我正在尝试声明一个占用零字节的C++变量.它在一个联合中,我以int [0]的类型开始.我不知道这是否实际上是零字节(虽然sizeof(int [0])是0).我需要一种更好的方法来声明一个0字节类型,并希望可以将其类型化为nullType或emptyType.变量在一个联合中,所以无论如何最终都会保留内存.我尝试了无效的机会,但是C++抱怨道.我正在使用Ubuntu 10.10,包含当前版本的内核,以及最新的GCC.这是联盟:
union RandomArgumentTypesFirst
{
uint uintVal;
nullType nullVal;
}
Run Code Online (Sandbox Code Playgroud)
这是typedef:
typedef int[0] nullType;
Run Code Online (Sandbox Code Playgroud)
编译器对typedef说了这个:
error: variable or field ‘nullVal’ declared voidmake[2]:
Run Code Online (Sandbox Code Playgroud)
当我输入时int[0],它有效.有什么建议?
编辑:正如@fefe在评论中所说,int[0]可能由编译器作为扩展提供.GCC的网站说默认情况下编译器有很多扩展.
我有一个这种类:
class A<TResult>
{
public TResult foo();
}
Run Code Online (Sandbox Code Playgroud)
但有时我需要将此类用作非泛型类,即类型TResult为void.
我无法通过以下方式实例化该类:
var a = new A<void>();
Run Code Online (Sandbox Code Playgroud)
另外,我宁愿不指定省略尖括号的类型:
var a = new A();
Run Code Online (Sandbox Code Playgroud)
我不想重写全班,因为它做同样的事情.
我想创建一个SwingWorker具有最终和中间结果类型的具体类void.我写了以下代码:
class AnswerWorker extends SwingWorker<void, void> {
protected void doInBackGround() {
System.out.println("what is your problem!!");
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
Multiple markers at this line-
Syntax error on token "void", Dimensions expected after this token.
Syntax error on token "void", Dimensions expected after this token.
Run Code Online (Sandbox Code Playgroud)
然而,当我将代码更改void为Void(即小v到大写V)时,它工作正常,尽管我仍然被迫return null;在doInBackground()方法结束时.
为什么是这样?我知道Void是一个java.lang包中的类,但是文档没有说太多关于它的内容(至少我不能遵循它:p)
Thanx提前!!
我的经验是基于C语言(C,C++,Java,C#),其中函数的返回类型可以是无效的,即没有返回.但在Ruby中,似乎每个函数都返回一些东西.我想知道在Ruby中返回什么方法不会返回Java或C++中的任何内容.我最好的猜测是nil或者包含方法的对象(用于链接)或者只是忽略最后发生的事情,但我似乎无法找到任何关于此的信息.
Ruby中什么是void的替代品?
目前,我正在使用此方法来检查类是否具有特定签名的方法.
在参加了Walter E. Brown的元编程CppCon2014演讲之后,我开始想知道是否void_t可以在这种特殊情况下使用它来使代码更清晰,更易读.
但是我在思考方面遇到了麻烦 void_t- 到目前为止我理解这void_t可以帮助我在编译时确定表达式是否有效.
例:
template< class, class = void >
struct has_type_data_member : false_type { };
template< class T >
struct has_type_data_member<T, void_t<decltype(T::data)>> : true_type { };
Run Code Online (Sandbox Code Playgroud)
如果decltype(T::type)是有效表达式,则为has_type_data_member<T>真正的编译时常量.因此,我们确信T有一个成员字段data.
我想使用相同的方法来检查类型T是否具有具有特定名称和特定签名的方法.
假设我想检查是否T有一个名为getCount()return 的方法int.这是我期望的工作((Ideone.com链接)):
template< class, class = void >
struct hasGetCount : false_type { };
template< class T >
struct hasGetCount<T, VoidT<decltype(T::getCount)>> …Run Code Online (Sandbox Code Playgroud)