我想使用我的程序:
./program -I /usr/include/ /usr/bin/ /usr/local/include/ ...
Run Code Online (Sandbox Code Playgroud)
交换机可以像var args列表一样继续运行.我怎么能在C99那样做?最好得到一个类似char **args_list或char *args_list[]包含所有喜欢的东西/usr/include和/usr/bin/.
我想将va_list传递给另一个函数.这是我想要做的一个例子:
void my_printf_1(char* string, ...) {
va_list ap;
va_start(ap, string);
printf(string, ap);
va_end(ap);
}
void my_printf_2(char* string, ...) {
va_list ap;
va_start(ap, string);
printf(string, *ap);
va_end(ap);
}
int main(void) {
my_printf_1("Hello %i\n", 12); //Shows me the pointer
my_printf_1("Hello \n"); //Runtime Error - too many arguments
my_printf_2("Hello %i\n", 12); //Displays correctly because 12 < char
my_printf_2("Hello %i\n", 500); //Displays incorrectly because 500 > char
my_printf_2("Hello %i %i\n", 12, 12); //Displays 12, then crashes Runtime Error not enough arguments
my_printf_2("Hello \n"); //Runtime …Run Code Online (Sandbox Code Playgroud) 我想在Scala中编写一个类,它采用任意数量的字节或像这样的布尔值
class Bytes(data: Byte*) {
def this(data: Boolean*) = this {
val res: Array[Byte] = convBools2Bytes(data)
res: _*
}
// […]
}
Run Code Online (Sandbox Code Playgroud)
其中convBools2Bytes是一个转换函数Array[Boolean]到Array[Byte]:
def convBools2Bytes(data: Array[Boolean]): Array[Byte]
Run Code Online (Sandbox Code Playgroud)
这给了我以下编译器错误:
[error] Bytes.scala:5: no `: _*' annotation allowed here
[error] (such annotations are only allowed in arguments to *-parameters)
[error] res: _*
[error] ^
Run Code Online (Sandbox Code Playgroud)
据我了解,该res: _*语句将Array[Byte]转换为重复参数列表(如"Scala编程"第2章第8节中所述).
为什么会出现这样的错误,我该如何避免呢?
为什么主方法不调用第一种方法?
public class Test {
public static void printValue(byte...b) {
System.out.println("long");
}
public static void printValue(int i, int j, int k) {
System.out.println("int");
}
public static void main(String... args) {
byte b = 9;
printValue(b,b,b);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么输出int?
我正在使用可变参数模板,我坚持以下内容:
template <class T1, class T2>
auto sum(T1 a, T2 b) ->decltype(a + b){
return a + b;
}
template <class T1, class T2, class... T3>
auto sum(T1 a, T2 b, T3... tail) ->decltype(a + b){
return a + sum(b, tail...);
}
Run Code Online (Sandbox Code Playgroud)
函数调用:
cout << sum(1, 2, 3, 4) << endl; // 10 - OK
cout << sum(1.5, 2, 3, 4) << endl; // 10.5 - OK
cout << sum(1, 2, 3.5, 4) << endl; // 10 !! wrong result …Run Code Online (Sandbox Code Playgroud) 我实现了一个带有重载方法的Scala类,该方法可以带一个Iterable[String]或一个String*varargs参数:
class StackOverflow(names: Iterable[String]) {
// This function creates a copy of the StackOverflow object
// copy is needed but this cannot be a case class.
private def copy(names: Iterable[String] = names) = new StackOverflow(names) // <- line 19
// overloaded methods
def withNames(names: Iterable[String]) = this.copy(names = names) // <- line 24
def withNames(names: String*) = require(names.nonEmpty); withNames(names.toIterable) // <- line 26
}
object App {
def main(args: Array[String]) = {
val x1 = new …Run Code Online (Sandbox Code Playgroud) libc关于变参函数,这是要说的:
由于原型没有为可选参数指定类型,因此在对可变参数函数的调用中,默认参数提升是对可选参数值执行的。这意味着将char或short int类型的对象(无论是否带符号)提升为int或unsigned int;并且将float类型的对象提升为double类型。因此,如果调用方将char作为可选参数传递,它将被提升为int
那么,为什么会有人使用"%c",或"%hd"在printf的?他们应该只使用"%d"。
我还看到没有的格式说明符float。float必须与之%f共存,double因为由于晋升,不可能接受浮夸作为可变参数。
我知道scanf,参数是指针,并且没有提升。
我是否有任何理由想念为什么以及何时"%c"必须存在printfs?
我对"The C++ programming language 4th,28.4.4"中的代码感到困惑
template<typename T>
struct get_f_result {
private:
template<typename X>
static auto check(X const& x) ?> decltype(f(x)); // can call f(x)
static substitution_failure check(...); // cannot call f(x)
public:
using type = decltype(check(std::declval<T>()));
};
Run Code Online (Sandbox Code Playgroud)
我特别困惑的部分是这一行:
static substitution_failure check(...); // cannot call f(x)
Run Code Online (Sandbox Code Playgroud)
但我记得...不能接受非豆荚类型?那怎么可能呢?
我想编写一个具有可变数量参数的函数(使用...),该函数调用具有相同参数的另一个函数,并在末尾调用一个新的函数。订单很重要!下面的示例仅用于演示。
function foo(...$params) {
$extraVariable = 6;
var_dump(...$params, $extraVariable);
}
foo(2, 4, 1, 4);
Run Code Online (Sandbox Code Playgroud)
运行它时,出现以下错误消息:
PHP致命错误:在第3行的/home/user/main.php中解压缩参数后,无法使用位置参数
我如何实现我的目标?
一printf类函数从不同的参数个数函数调用很容易足以让-只使用一个v的那些功能(-version vprintf,vsprintf,CString::FormatV,等)。但是,如果我将呼叫链接起来怎么办?这是简单的代码:
#include <stdarg.h>
#include <iostream>
void direct(const char * _fmt, bool _extra, ...){
va_list args;
va_start(args, _extra);
char ca[200];
vsprintf(ca, _fmt, args);
std::cout << ca << std::endl;
va_end(args);
}
void chained(const char * _fmt, ...){
va_list args;
va_start(args, _fmt);
direct(_fmt, false, args);
va_end(args);
}
int main(){
direct("direct works just fine: %d", false, 1);
chained("indirect produces garbage: %d", 1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
示例输出如下:
direct works just fine: 1
indirect produces garbage: …Run Code Online (Sandbox Code Playgroud)