例如,((fn-stringappend string-append) "a" "b" "c") 我知道如何处理这个问题(f x y z).但是,如果参数数量未知,该怎么办?有没有办法处理这类问题?
...在ObjectiveC中遇到一些问题.
我基本上包装一个方法,并希望接受一个nil终止列表,并直接将相同的列表传递给我正在包装的方法.
这是我的,但它会导致EXC_BAD_ACCESS崩溃.检查当地的变量,它似乎otherButtonTitles只是一个NSString传入的时间otherButtonTitles:@"Foo", nil]
+ (void)showWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title
message:message
delegate:delegate
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles] autorelease];
[alert show];
}
Run Code Online (Sandbox Code Playgroud)
我如何简单地从参数传出到传出参数,从而保留完全相同的nil终止列表?
为了使我的代码更短更容易更改,我想替换类似的东西
enum{ E_AAA, E_BBB, E_CCC };
static const char *strings{"AAA", "BBB", "CCC" };
Run Code Online (Sandbox Code Playgroud)
使用宏,如INIT(AAA,BBB,CCC); 但是当我尝试使用变量参数和字符串化做一个宏时,我得到一个错误,因为没有声明参数.
有关如何做到这一点的任何想法?
我正在编写一个类似于boost :: promote的促销模板别名,但是对于C++ 11.这样做的目的是在从varidic函数检索参数时避免警告.例如
template <typename T>
std::vector<T> MakeArgVectorV(int aArgCount, va_list aArgList)
{
std::vector<T> args;
while (aArgCount > 0)
{
args.push_back(static_cast<T>(va_arg(aArgList, Promote<T>)));
--aArgCount;
}
return args;
}
Run Code Online (Sandbox Code Playgroud)
Promote模板别名在可变参数的默认参数提升之后提升类型:1)小于int的整数被提升为int 2)float被提升为double
我的问题是可以提升标准C++枚举,但不提升C++ 11枚举类(编译器不会生成警告).我希望Promote使用常规枚举但忽略C++ 11枚举类.
如何区分我的Promote模板别名中的枚举类和枚举?
我必须在嵌入式应用程序中使用IAR编译器(它没有名称空间,异常,多重/虚拟继承,模板受位限制并且仅支持C ++ 03)。我无法使用参数包,因此尝试使用可变参数创建成员函数。我知道可变参数通常是不安全的。但是this在va_start宏中使用指针是否安全?
如果我使用普通的可变参数函数,则需要一个伪参数才能...访问其余参数。我知道可变参数宏之前不需要参数,...但我不希望使用它。如果我使用成员函数,则它this之前具有隐藏参数,...因此我尝试了它:
struct VariadicTestBase{
virtual void DO(...)=0;
};
struct VariadicTest: public VariadicTestBase{
virtual void DO(...){
va_list args;
va_start(args, this);
vprintf("%d%d%d\n",args);
va_end(args);
}
};
//Now I can do
VariadicTestBase *tst = new VariadicTest;
tst->DO(1,2,3);
Run Code Online (Sandbox Code Playgroud)
tst->DO(1,2,3);按预期打印123。但是我不确定这是否只是一些随机/未定义的行为。我知道tst->DO(1,2);会像普通的prinf一样崩溃。我不介意。
假设我有一些像这样的结构:
struct MyStruct1 {
inline void DoSomething() {
cout << "I'm number one!" << endl;
}
};
struct MyStruct2 {
static int DoSomething() {
cout << "I'm the runner up." << endl;
return 1;
}
};
struct MyStruct3 {
void (*DoSomething)();
MyStruct3() {
DoSomething = &InternalFunction;
}
static void InternalFunction() {
cout << "I'm the tricky loser." << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,对于所有三个结构,我可以在该结构的对象上调用DoSomething()并使其工作(尽管每个结构的实现方式不同):
MyStruct1 a;
MyStruct2 b;
MyStruct3 c;
a.DoSomething(); // works, calls Struct1's instance function
b.DoSomething(); // works, calls Struct2's …Run Code Online (Sandbox Code Playgroud) 我只是想知道C++ 0x std lib中是否有任何东西可用于计算参数包中的参数数量?我想在下面的代码中删除field_count.我知道我可以构建自己的计数器,但看起来这似乎是一个显而易见的事情,包括在C++ 0x std lib中,我想确定它不存在:)本土计数器实现也非常欢迎.
template<const int field_count, typename... Args> struct Entity {
const tuple<Args...> data;
const array<const char*, field_count> source_names;
Entity() : data() {
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个函数foo,它使用可变参数函数指针作为其参数.
我想在函数声明之前使用"using"来定义参数的类型.
template <typename ... vARGS>
using TFuncType = void(*)(vARGS ... V_args);
template <typename ... vARGS>
void foo(TFuncType<vARGS ...> funcptr) {}
void bar(int i) {}
int main() {
foo(&bar); // This line fails to compile.
}
Run Code Online (Sandbox Code Playgroud)
这不编译.错误(通过使用c ++ 1z的clang)是:
/make/proj/test/variadic-funcparam-deduce2.cpp:39:5: error: no matching function for call to 'foo'
foo(&bar);
^~~
/make/proj/test/variadic-funcparam-deduce2.cpp:33:36: note: candidate template ignored: substitution failure [with vARGS = int]
template <typename ... vARGS> void foo(TFuncType<vARGS ...> funcptr) {}
Run Code Online (Sandbox Code Playgroud)
为什么"int"替换失败了?
如果我在foo()中明确写出类型,我可以成功编译:
template <typename ... vARGS>
void foo(void(*funcptr)(vARGS …Run Code Online (Sandbox Code Playgroud) 使用和提供什么固有的优势boost::any和boost::any_cast提供?void*dynamic_cast
我有一个函数,试图将东西记录到控制台和日志文件,但它不起作用.第二次使用变长参数会将垃圾写入控制台.有任何想法吗?
void logPrintf(const char *fmt, ...) {
va_list ap; // log to logfile
va_start(ap, fmt);
logOpen;
vfprintf(flog, fmt, ap);
logClose;
va_end(ap);
va_list ap2; // log to console
va_start(ap2, fmt);
printf(fmt, ap2);
va_end(ap2);
}
Run Code Online (Sandbox Code Playgroud)