我正在编写一个对象分配器,我想通过以下方式调用它:
T result = factoryObject.construct(argA, argB, argC);
Run Code Online (Sandbox Code Playgroud)
我目前有这个设计,它有效......
class Factory {
void* memPool_;
template <typename Tret, typename... Args>
Tret construct(Args&&... args) {
Tret::methodA(std::forward<Args&&>(args));
Tret::Tret(memPool_, std::forward<Args&&>(args);
}
}
Run Code Online (Sandbox Code Playgroud)
...只要我用以下方式调用它:
T result = factoryObject.construct<T>(argA, argB, argC);
Run Code Online (Sandbox Code Playgroud)
我希望能够在不明确指定的情况下做到这一点T。T可能会变得非常复杂,我需要在初始化列表中使用这个工厂内联。我还需要在构造函数初始化列表中使用它,所以我无法调用auto result = factory.construct<T>()(我只需要它根据它正在构造的内容推断返回类型)。
我尝试使用operator()技巧推断类型(根据/sf/answers/182910731/):
public:
template <class T>
operator T() { return T(); }
};
class GC {
public:
static Allocator Allocate() { return Allocator(); }
};
int main() {
int p = GC::Allocate();
}
Run Code Online (Sandbox Code Playgroud)
...但这不允许我传递参数(因为 operator() …
我的使命是创建一个函数,该函数接收可变数量的参数并为任何类型的参数释放所有参数。我尝试使用可变参数函数来做到这一点,但问题是我不知道参数的类型。
关于如何做有什么建议吗?
经过很长一段时间的项目后,我再次与C合作,并试图了解可变功能.基本上,我希望能够将多个字符串传递给函数.
#include<stdarg.h>
int main(int argc, const char * argv[])
{
test_function(2,"test","test2");
test_function(4,"test3","test4","test5","test6");
return 0;
}
void test_function(int args, ...)
{
va_list ap;
va_start(ap, args);
int i;
for(i=0;i<args;i++)
{
printf("Argument:%s\n",va_arg(ap, char*));
}
va_end(ap);
}
Run Code Online (Sandbox Code Playgroud)
我在test_function周围出现错误 - 'test_function'的冲突类型
谁能指出我的错误?
我在java中有一个varargs方法.因此,该方法需要一个Objects 数组.我通过了一个List方法,它工作了!我的意思是,它不仅编译,而且测试是绿色的.所以,我的问题是 - myList.toArray()调用方法时是否需要调用,或者此调用是否自动发生?
提前致谢!
以下是代码:
createSomething(final Object... parameters) {
// varargs method
}
List<Object> data = new ArrayList<Object>();
createSomething(data); // is this wrong?
createSomething(data.toArray()); // should I always do this?
Run Code Online (Sandbox Code Playgroud) 我有两个可变参数函数。其中一个将其参数传递给另一个。问题是可变参数在第二次调用时变成了一个列表。我如何保持它们的可变参数?
=> (defn foo [x & ys] (println x ys))
=> (defn bar [x & ys] (foo (clojure.string/upper-case x) ys))
=> (foo "hi")
hi nil
=> (bar "hi")
HI (nil)
Run Code Online (Sandbox Code Playgroud)
在实际函数中,foo 将其 args 传递给可变参数 java 函数,因此 varargs 确实需要保留 varargs。我该怎么做呢?
我最近在MSVC(2013)中偶然发现了一个奇怪的行为,我想澄清有关变量参数的问题.
在"..."之前看起来有多个参数会导致意外行为
int formatString(const char* msg, char* buffer, int bufferLength, ...)
{
int length = 0;
if (msg != nullptr) {
va_list args;
va_start(args, msg);
length = vsnprintf_s(buffer, bufferLength, bufferLength, msg, args);
va_end(args);
}
return length;
}
Run Code Online (Sandbox Code Playgroud)
像这样调用这个函数
const char* message = "A word: %s and a number %d";
const int bufferLength = 1024;
char buffer[bufferLength];
int formattedMsgLen = formatString(message, buffer, bufferLength, "cheese", 4);
Run Code Online (Sandbox Code Playgroud)
会立即导致程序崩溃.如果我之前将缓冲区设置为0
memset(buffer, 0, 1024); // We know sizeof(char) == 1
Run Code Online (Sandbox Code Playgroud)
这被写入缓冲区:
"A word: A word: and …Run Code Online (Sandbox Code Playgroud) 是否可以像在C++中一样在Java中完成参数转发:
template <typename ...Params>
void f(Params&&... params)
{
y(std::forward<Params>(params)...);
}
Run Code Online (Sandbox Code Playgroud)
例如,在我想要一个工厂功能的情况下:
public static MyClass create( < parameter pack > )
{
return new MyClass(< forward >);
}
Run Code Online (Sandbox Code Playgroud)
有多个构造函数?
值得注意的是,参数包可以包含不同类型的参数.
给定(可变)函数的原因是什么
func varargs(n ...int) {}
Run Code Online (Sandbox Code Playgroud)
可以这样称呼
varargs(1, 2, 3, 4) // Fixed number of arguments
Run Code Online (Sandbox Code Playgroud)
但不包含数组:
a := [4]int{1, 2, 3, 4} // Fixed number of elements
varargs(a...) // Error: cannot use (type [4]int) as type []int in argument
Run Code Online (Sandbox Code Playgroud)
我明白为什么
var s []int = a
Run Code Online (Sandbox Code Playgroud)
无效:它可以防止意外误用,需要手动切片:
s := a[:]
Run Code Online (Sandbox Code Playgroud)
但是,为什么此限制扩展到对可变参数函数的调用?
额外的问题:
相反,为什么会打电话
func fourargs(w, x, y, z int) {}
Run Code Online (Sandbox Code Playgroud)
与4个元素的数组状
fourargs(a...) // Error: not enough arguments in call have ([4]int...)
// want (int, int, …Run Code Online (Sandbox Code Playgroud) package main
import (
"fmt"
"time"
)
func main() {
n := 1024
dst1 := make([]byte, n)
dst2 := make([]byte, 0, n)
dst3 := make([]byte, 0, n)
start := time.Now()
for i := 0; i < n; i++ {
dst1[i] = byte(i)
}
fmt.Println(uint64(time.Since(start).Microseconds()))
start = time.Now()
for i := 0; i < n; i++ {
dst2 = append(dst2, dst1[i])
}
fmt.Println(uint64(time.Since(start).Microseconds()))
start = time.Now()
for i := 0; i < n; i++ {
dst3 = append(dst3, dst1...)
}
fmt.Println(uint64(time.Since(start).Microseconds())) …Run Code Online (Sandbox Code Playgroud) public void test(int... integers, String str) { // error
...
}
Run Code Online (Sandbox Code Playgroud)
错误:
方法 test 的变量参数类型 int 必须是最后一个参数。
public void test(String str, int... integers) {}
Run Code Online (Sandbox Code Playgroud)
效果很好。这有什么原因吗?