从ISO/IEC 14882:2011(E)的§8.3.5.11开始:
函数类型的typedef可用于声明函数,但不得用于定义函数
标准继续给出这个例子:
typedef void F();
F fv; // OK: equivalent to void fv();
F fv { } // ill-formed
void fv() { } // OK: de?nition of fv
Run Code Online (Sandbox Code Playgroud)
这条规则的动机是什么?它似乎限制了函数typedef的潜在表达用途.
考虑一下filterNot(基本上与之相反filter)的实现:
def filterNot(f, sequence):
return filter(lambda x: not f(x), sequence)
Run Code Online (Sandbox Code Playgroud)
参数f可以是"函数"或"方法"或lambda- 甚至是类定义的对象__call__.
现在考虑一下这个参数的docstring行:
:param ??? f: Should return True for each element to be abandoned
Run Code Online (Sandbox Code Playgroud)
现在,应该取代什么?- 如何在docstring中引用参数类型f.callable是显而易见的选择(如果我发号施令,我会指示:P)但是有没有既定的惯例?
例如,
#include <stdio.h>
void foo();
int main(void)
{
foo();
foo(42);
foo("a string", 'C', 1.0);
return 0;
}
void foo()
{
puts("foo() is called");
}
Run Code Online (Sandbox Code Playgroud)
输出:
foo() is called
foo() is called
foo() is called
Run Code Online (Sandbox Code Playgroud)
这段代码编译得很好(没有使用clang的警告)并运行良好.但我想知道传递给的值会发生什么foo()?他们被推入堆栈还是被丢弃?
也许这个问题听起来毫无用处,但它确实有意义.例如,当我有int main(),而不是int main(void)传递一些命令行参数时,行为main()会受到影响吗?
此外,在使用时<stdarg.h>,...ISO C 之前至少需要一个命名参数.我们是否可以使用这样的声明void foo()从函数的零参数传递到无限参数?
我注意到这void foo()是一个"非原型声明",这void foo(void)是一个"原型宣言".这有点相关吗?
澄清
似乎这个问题被标记为重复,空参数列表是什么意思?[重复](有趣的是,这个问题也是重复的......).事实上,我不认为我的问题与那个问题有关.它侧重于" void foo()C中的含义",但我知道这意味着"我可以传递任意数量的论据",而且我也知道这是一个过时的功能.
但这个问题完全不同.关键字是"假设".我只是想知道我是否传递了不同数量的参数void foo(),就像上面的示例代码一样,它们可以在里面使用foo()吗?如果是这样,这是怎么做到的?如果没有,传递的参数会有什么不同吗?那是我的问题.
c parameters declaration command-line-arguments function-declaration
更确切地说,函数声明参数中使用的"callable".喜欢下面这个.
function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}
Run Code Online (Sandbox Code Playgroud)
它对我们有什么好处?
为什么以及如何使用它?
也许这对你来说非常基础,但是,我已经尝试过寻找它而我却得不到任何答案.至少,我无法理解.
希望得到一个假人的答案.我是编码的新手...... XD
php callable type-hinting function-declaration type-declaration
可能重复:
在R中生成调用图
我想系统地分析给定的函数,以找出在该函数中调用的其他函数.如果可能的话,递归地.
我在milktrader的博客文章中遇到了这个函数,我可以用它来做类似的包(或命名空间)
listFunctions <- function(
name,
...
){
name.0 <- name
name <- paste("package", ":", name, sep="")
if (!name %in% search()) {
stop(paste("Invalid namespace: '", name.0, "'"))
}
# KEEP AS REFERENCE
# out <- ls(name)
funlist <- lsf.str(name)
out <- head(funlist, n=length(funlist))
return(out)
}
> listFunctions("stats")
[1] "acf" "acf2AR" "add.scope"
[4] "add1" "addmargins" "aggregate"
[7] "aggregate.data.frame" "aggregate.default" "aggregate.ts"
[10] "AIC" "alias" "anova"
....
[499] "xtabs"
Run Code Online (Sandbox Code Playgroud)
然而,我想要一个函数,name它将是函数的名称,返回值是在其中调用的函数的字符向量(或者列表,如果以递归方式完成)name.
我实际上需要某种基于字符的输出(矢量或列表).这样做的原因是,我工作的一个通用的包装功能并行的abitrary"内部函数",你不必经过耗时的试错的过程,以找出其他功能内在的功能取决于.所以我之后的函数的输出将直接用于snowfall::sfExport() …
我正在尝试创建一个可以接受可选参数的泛型函数。这是我到目前为止的内容:
func somethingGeneric<T>(input: T?) {
if (input != nil) {
print(input!);
}
}
somethingGeneric("Hello, World!") // Hello, World!
somethingGeneric(nil) // Errors!
Run Code Online (Sandbox Code Playgroud)
String如图所示,它适用于,但不适用于nil。与一起使用会nil产生以下两个错误:
func somethingGeneric<T>(input: T?) {
if (input != nil) {
print(input!);
}
}
somethingGeneric("Hello, World!") // Hello, World!
somethingGeneric(nil) // Errors!
Run Code Online (Sandbox Code Playgroud)
我在做什么错,应该如何正确声明/使用此函数?另外,我想使函数的使用尽可能简单(我不想做类似的事情nil as String?)。
虽然我偶然发现了这个问题的MCVE ,但我发现编译器之间存在以下差异:
请考虑以下代码:
// constexpr int f(); // 1
constexpr int g() {
constexpr int f(); // 2
return f();
}
constexpr int f() {
return 42;
}
int main() {
constexpr int i = g();
return i;
}
Run Code Online (Sandbox Code Playgroud)
此代码在Clang 3.8.0上编译,但在GCC 6.1.0上失败:
error: 'constexpr int f()' used before its definition
Run Code Online (Sandbox Code Playgroud)
在两个编译器上评论// 2和取消注释// 1.
有趣的是,移动f的定义代替了// 1编译,但在// 2以下情况下触发了警告:
warning: inline function 'constexpr int f()' used but never defined
Run Code Online (Sandbox Code Playgroud)
哪个编译器是对的?
c++ language-lawyer compiler-bug function-declaration constexpr
我试图从sqlite3.c中解读这个声明
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
Run Code Online (Sandbox Code Playgroud)
似乎它正在声明一个函数,因为随后会有这个
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
return pVfs->xDlSym(pVfs, pHdle, zSym);
}
Run Code Online (Sandbox Code Playgroud)
然后似乎是对函数的调用
xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
Run Code Online (Sandbox Code Playgroud)
和
xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
Run Code Online (Sandbox Code Playgroud)
但我无法理解宣言.我突出了我无法理解的东西
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
^ ^^^^^^^
Run Code Online (Sandbox Code Playgroud)
我想知道为什么宣言不是这样的
SQLITE_PRIVATE void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *);
Run Code Online (Sandbox Code Playgroud)
我希望可能有一个类似的问题已经被问到,但是搜索类似的术语(,)并void没有真正得到任何结果.所以,如果这是一个骗局,我会很高兴它被关闭.
我有以下代码:
struct student_info;
void paiming1(struct student_info student[]);
struct student_info
{
int num;
char name[6];
};
Run Code Online (Sandbox Code Playgroud)
IDE出错
error: array type has incomplete element type ‘struct student_info’
void paiming1(struct student_info student[]);
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用void paiming1(struct student_info *student);它工作正常.这是为什么?我正在使用GCC.
我正在学习教程,我的代码看起来很正常,但我收到一条消息,上面写着
This old-style function definition is not preceded by a prototype
Run Code Online (Sandbox Code Playgroud)
代码.c:
void viderBuffer()
{
int c = 0;
while (c != '\n' && c != EOF)
{
c = getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助。对不起,如果我的帖子不完美,我是新来的。
c ×4
function ×3
c++ ×2
declaration ×2
callable ×1
compiler-bug ×1
constexpr ×1
dependencies ×1
docstring ×1
generics ×1
null ×1
parameters ×1
php ×1
python ×1
r ×1
recursion ×1
swift ×1
type-hinting ×1
typedef ×1
types ×1