通常,在Delphi中,可以使用'array of const'方法声明具有可变数量参数的函数.但是,为了与用C编写的代码兼容,有一个未知的'varargs'指令可以添加到函数声明中(我在阅读Rudy的优秀" 融合陷阱 "文档时学到了这一点).
例如,可以在C中使用函数,声明如下:
void printf(const char *fmt, ...)
Run Code Online (Sandbox Code Playgroud)
在Delphi中,这将成为:
procedure printf(const fmt: PChar); varargs;
Run Code Online (Sandbox Code Playgroud)
我的问题是:在实现使用'varargs'指令定义的方法时,如何获取堆栈的内容?
我希望有一些工具存在,比如va_start(),va_arg()和va_end()函数的Dephi翻译,但我无法在任何地方找到它.
请帮忙!
PS:请不要讨论"为什么"或"const数组"的替代方案 - 我需要这个来为Xbox游戏中的函数编写类似C的补丁(参见Sourceforge上的Delphi Xbox模拟器项目'Dxbx'详情).
UIActionSheet适用于:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
otherButtonTitles:@"Button1", @"Button2", nil];
Run Code Online (Sandbox Code Playgroud)
我试图将NSArray传递给"otherButtonTitles"消息.
我尝试使用以下方法传递NSArray:
otherButtonTitles:[array]
Run Code Online (Sandbox Code Playgroud)
但消息是期待NSStrings列表.
我能想到将NSArray分解为一组NSStrings的唯一方法是使用componentsJoinedByString,但它给我一个逗号分隔的列表,它是一个NSString.
正确方向的一点将不胜感激.
我有以下功能:
void doStuff(int unusedParameter, ...)
{
va_list params;
va_start(params, unusedParameter);
/* ... */
va_end(params);
}
Run Code Online (Sandbox Code Playgroud)
作为重构的一部分,我想删除未使用的参数,而不另行更改函数的实现.据我所知,va_start当你没有最后一个非变量参数引用时,就不可能使用它.有没有办法解决?
背景:它实际上是一个C++程序,所以我可以使用这里建议的一些运算符重载魔法,但我希望此时不必更改接口.
现有函数通过要求变量参数列表以空值终止并扫描NULL来完成其工作,因此它不需要一个前导参数来告诉它有多少参数.
在回应评论:我不具备删除未使用的参数,但我会做到这一点,如果有一个干净的方式来做到这一点.我希望有一些我想念的简单.
我有一个功能
void foo(int cnt, va_list ap);
Run Code Online (Sandbox Code Playgroud)
我需要使用它,但要求非常严格,数量各va_list不相同,并且在运行期间会发生变化.我想做的是:
创建一个va_list(期望的char*)表单
QList<Contact*>
Run Code Online (Sandbox Code Playgroud)
哪里Contact是定义的类
class Contact
{
public:
QString getName();
private:
QString m_name;
};
Run Code Online (Sandbox Code Playgroud)
我想在循环中填充va_list例如:
for (int idx = 0; idx<contacts.count(); idx++)
{
contacts.at(idx)->getName(); // this i would like to pass to va_list
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道我怎么能做到这一点?
请看下面的示例,第getMethod()一个在Eclipse中生成警告的调用.第二个不起作用,失败了NoSuchMethodException.
对于从类型调用varargs方法,
null应明确地转换类型Class<?>[]的参数.也可以将它转换为Class以进行varargs调用.getMethod(String, Class<?>...)Class<Example>
我跟着警告,没有任何工作了.
import java.lang.reflect.Method;
public class Example
{
public void exampleMethod() { }
public static void main(String[] args) throws Throwable
{
Method defaultNull = Example.class.getMethod("exampleMethod", null);
Method castedNull = Example.class.getMethod("exampleMethod", (Class<?>) null);
}
}
Run Code Online (Sandbox Code Playgroud)
第二个调用产生此错误:
Exception in thread "main" java.lang.NoSuchMethodException:
Example.exampleMethod(null)
at java.lang.Class.getMethod(Class.java:1605)
at Example.main(Example.java:12)
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这种行为吗?什么是避免警告的正确方法?
在一个多线程程序中,我正在编写一个自定义打印函数,它接受一个可变参数列表.
void t_printf(char * str, ...)
{
if(file_ptr != NULL)
{
va_list ap;
va_start(ap, str);
vfprintf(file_ptr, str, ap);
va_end(ap);
fflush(file_ptr);
}
}
Run Code Online (Sandbox Code Playgroud)
在这个函数里面,我想将当前线程id(使用pthread_self())添加到要打印的消息中.我该怎么做?有没有办法将其添加到现有的va_list?
在Java 7中,您可以选择放置@SafeVarargs注释以抑制在使用不可重新生成的varargs参数编译方法时获得的警告.Project Coin的提议规定,当方法确保只有与varargs参数相同类型的元素存储在varargs数组中时,才应使用注释.
什么是非安全方法的例子?
在C++ 14中,广义lambda捕获让我们做:
template<class T>
auto pack(T t)
{
return [t=std::move(t)](auto&& f){f(t);};
};
Run Code Online (Sandbox Code Playgroud)
但它没有玩param-pack:
template<class... T>
auto pack(T... t)
{
return [t=std::move(t)...](auto&& f){f(t...);};
};
Run Code Online (Sandbox Code Playgroud)
是否有任何特殊语法或进一步的标准提案来解决这个问题?
该<stdarg.h>头文件是用来做函数接受的参数未定义的数字,对不对?
所以,必须使用的printf()功能是接受大量的论点(如果我弄错了,请纠正我).
我在gcc的stdio.h文件中找到以下行:<stdio.h><stdarg.h>
#if defined __USE_XOPEN || defined __USE_XOPEN2K8
# ifdef __GNUC__
# ifndef _VA_LIST_DEFINED
typedef _G_va_list va_list;
# define _VA_LIST_DEFINED
# endif
# else
# include <stdarg.h>//////////////////////stdarg.h IS INCLUDED!///////////
# endif
#endif
Run Code Online (Sandbox Code Playgroud)
我无法理解其中的大部分内容,但它似乎包括在内 <stdarg.h>
所以,如果printf()使用<stdarg.h>接受可变数量的参数,并stdio.h已printf(),一个C程序中使用printf()无需包括<stdarg.h>不是吗?
我尝试了一个程序,它有printf()一个用户定义的函数接受可变数量的参数.
我尝试的程序是:
#include<stdio.h>
//#include<stdarg.h>///If this is included, the program works fine.
void fn(int num, ...)
{
va_list vlist;
va_start(vlist, num);//initialising va_start (predefined)
int i; …Run Code Online (Sandbox Code Playgroud) 当实现一个接受指针参数包的函数时Ts...,为什么我const不能对指针进行限定,正如常规参数一样?
我在最新的GCC和Clang上遇到了不匹配的签名错误,我不明白为什么,因为指针const只是一个实现细节(因此它对于常规参数是合法的).
template<typename... Ts>
class C
{
void f(int*);
void g(Ts*...);
};
template<typename... Ts>
void C<Ts...>::f(int* const) {} // Legal
template<typename... Ts>
void C<Ts...>::g(Ts* const...) {} // Compiler error
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
prog.cc:12:16: error: out-of-line definition of 'g' does not match any declaration in 'C<Ts...>'
void C<Ts...>::g(Ts* const...) {}
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
您还可以在此处查看代码和错误.