function-declaration

How are functions inside functions called? And can I access those functions or they work like “helper methods”?

时光怂恿深爱的人放手 提交于 2019-12-13 07:14:26
问题 I'm studying Python through The Python Tutorial and I'm currently at Classes (chapter 9), but during the explanation of "scopes and namespaces" I got a question. The author give this example: def scope_test(): def do_local(): spam = "local spam" def do_nonlocal(): nonlocal spam spam = "nonlocal spam" def do_global(): global spam spam = "global spam" spam = "test spam" do_local() print("After local assignment:", spam) do_nonlocal() print("After nonlocal assignment:", spam) do_global() print(

How is this possible to use in c++?

牧云@^-^@ 提交于 2019-12-12 09:30:18
问题 To my surprise, I found that the name of a c++ object can be the same as class name. Can someone explain to me the reason why? When I declare an object of class a as a a1() , it does not raise an error, but doesn't call the constructor. Why is this happening? My code: #include<iostream> using namespace std; class a { public: a() { cout << "in a\n"; } }; int main() { a a1(); a a; } 回答1: When you write a a1(); it is actually being parsed as a function declaration not a call to the default

Defining a function once that appears in multiple name spaces

雨燕双飞 提交于 2019-12-11 05:07:32
问题 I am trying to define a common interface to a set of functions and classes that will have multiple different backend implementations (Using different libraries). As such I'd really rather, simply, define a function in one place and not in each separate namespace. For example, I have a global function: extern void Func(); Now I want to have 3 separate implementations of that function. One would be a straight C, One would be a hand coded assembler and one would be using library 'x'. I am

Anonymous struct as a return type

我是研究僧i 提交于 2019-12-11 03:03:19
问题 The following code compiles fine with vc++ 19.00.23506 (flags: /Wall /WX /Za ) and with vc++ 19.10.25109.0 (flags: /Wall /WX /Za /permissive- , this can be checked at http://webcompiler.cloudapp.net), but doesn't compile with clang 3.8.0 and g++ 6.3.0 (flags: -std=c++11 -Wall -Wextra -Werror -pedantic-errors ). Is it a bug in vc++ and does the standard prohibit such constructions? struct { } foo() { return {}; } int main() { } 回答1: MSVC appears wrong: [dcl.fct]/9 Types shall not be defined in

Nested `constexpr` function calls before definition in a constant-expression context

微笑、不失礼 提交于 2019-12-10 16:46:50
问题 From what I gather from this answer, a constexpr function's result is not a constant-expression if the function has not been declared yet. What surprises me is the following code snippet : constexpr int f(); constexpr int g() { return f(); } constexpr int f() { return 42; } int main() { constexpr int i = g(); return i; } This compiles without trouble and works. Moving f 's definition past main triggers error: 'constexpr int f()' used before its definition , as I would expect. I presume that

Function declaration or function expression

假如想象 提交于 2019-12-10 13:39:54
问题 I just ran into a problem when defining a function in a block scope. Consider the following program: try { greet(); function greet() { alert("Merry Christmas!"); } } catch (error) { alert(error); } I expected this program to alert Merry Christmas! . However in Firefox is gives me the following ReferenceError : ReferenceError: greet is not defined On Opera and Chrome it alerts the greeting as I expected it to. Evidently Firefox treats the function inside the block scope as a FunctionExpression

Implicit function declarations and linkage

前提是你 提交于 2019-12-10 10:12:54
问题 Recently I've learnt about implicit function declarations in C . The main idea is clear but I have some troubles with understanding of the linkage process in this case. Consider the following code ( file a.c ): #include <stdio.h> int main() { double someValue = f(); printf("%f\n", someValue); return 0; } If I try to compile it: gcc -c a.c -std=c99 I see a warning about implicit declaration of function f() . If I try to compile and link: gcc a.c -std=c99 I have an undefined reference error. So

extern declaration and function definition both in the same file

余生长醉 提交于 2019-12-09 14:52:13
问题 I was just browsing through gcc source files. In gcc.c , I found something like extern int main (int, char **); int main (int argc, char **argv) { Now my doubt is extern is to tell the compiler that the particular function is not in this file but will be found somewhere else in the project. But here, definition of main is immediately after the extern declaration. What purpose is the extern declaration serving then? It seems like, in this specific example, extern seems to be behaving like

How to understand this define

橙三吉。 提交于 2019-12-09 14:50:21
问题 Nowadays , i was reading the APUE.and i found the function defined as below: void (*signal(int signo, void (*func)(int)))(int); i was confused, i know signal is pointer to a function and the last (int) is his parameter. i did not know what is (int signo,void (*func)(int)). 回答1: The general procedure: find the leftmost identifier and work your way out. Absent an explicit grouping with parentheses, postfix operators such as () and [] bind before unary operators like * ; thus, the following are

an error about C struct array in formal parameter

人盡茶涼 提交于 2019-12-08 15:58:55
问题 I have got the following code: struct student_info; void paiming1(struct student_info student[]); struct student_info { int num; char name[6]; }; The IDE gives an error error: array type has incomplete element type ‘struct student_info’ void paiming1(struct student_info student[]); But if I use void paiming1(struct student_info *student); it works OK. Why is that? I am using GCC. 回答1: С language unconditionally requires array element type in all array declarations to be complete. Period. 6.7