function-prototypes

Forward declaring a function in a namespace inside another function in that namespace

自作多情 提交于 2021-01-27 04:51:30
问题 I have two source files, a.cpp and b.cpp . In a.cpp , I have a function, foo : namespace ns { void foo() { std::cout << "foo!"; } } In b.cpp , I have another function in namespace ns in which I'd like to prototype and call foo : namespace ns { void bar() { void foo(); foo(); } } While the above is syntactically valid, it leads the compiler to think that foo is in the global namespace (or at least that's what I've deduced from the linker errors I get when I do this). My first two ideas to fix

Forward declaring a function in a namespace inside another function in that namespace

喜夏-厌秋 提交于 2021-01-27 04:51:08
问题 I have two source files, a.cpp and b.cpp . In a.cpp , I have a function, foo : namespace ns { void foo() { std::cout << "foo!"; } } In b.cpp , I have another function in namespace ns in which I'd like to prototype and call foo : namespace ns { void bar() { void foo(); foo(); } } While the above is syntactically valid, it leads the compiler to think that foo is in the global namespace (or at least that's what I've deduced from the linker errors I get when I do this). My first two ideas to fix

foo(void) vs foo(void *)

[亡魂溺海] 提交于 2021-01-27 02:28:20
问题 Functionally and syntactically speaking, is there a difference between a function whose prototype is int foo(void) and int foo(void *) ? I know the difference between, for example, int bar(int) and int bar(int *) - one of them is looking for an int, and the other is looking for an int pointer. Does void behave the same way? 回答1: From this answer on Software Engineering, void is treated specially depending on how it's used. In C and C++ , void is used to indicate an absence of a data type,

foo(void) vs foo(void *)

别等时光非礼了梦想. 提交于 2021-01-27 02:27:32
问题 Functionally and syntactically speaking, is there a difference between a function whose prototype is int foo(void) and int foo(void *) ? I know the difference between, for example, int bar(int) and int bar(int *) - one of them is looking for an int, and the other is looking for an int pointer. Does void behave the same way? 回答1: From this answer on Software Engineering, void is treated specially depending on how it's used. In C and C++ , void is used to indicate an absence of a data type,

Why do function prototypes include parameter names when they're not required?

不打扰是莪最后的温柔 提交于 2019-12-29 04:20:32
问题 I always thought that a function prototype must contain the parameters of the function and their names. However, I just tried this out: int add(int,int); int main() { std::cout << add(3,1) << std::endl; } int add(int x, int y) { return x + y; } And it worked! I even tried compiling with extreme over-caution: g++ -W -Wall -Werror -pedantic test.cpp And it still worked. So my question is, if you don't need parameter names in function prototypes, why is it so common to do so? Is there any

What is the effect of casting a function pointer void?

╄→гoц情女王★ 提交于 2019-12-25 08:57:07
问题 So I'm trying to write a buffering library for the 64th time and I'm starting get into some pretty advanced stuff. Thought I'd ask for some proffesional input on this. In my first header file I have this: typedef struct StdBuffer { void* address; } StdBuffer; extern void StdBufferClear(StdBuffer); In another header file that #includes the first header file I have this: typedef struct CharBuffer { char* address; } CharBuffer; void (*CharBufferClear)(CharBuffer) = (void*) StdBufferClear; Will

Does calling printf without a proper prototype invoke undefined behavior?

大兔子大兔子 提交于 2019-12-23 09:40:45
问题 Does this innocent looking program invoke undefined behavior: int main(void) { printf("%d\n", 1); return 0; } 回答1: Yes invoking printf() without a proper prototype (from the standard header <stdio.h> or from a properly written declaration) invokes undefined behavior. As documented in C11 Annex J (informative only) J2 Undefined Behavior For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an