|
|
|
|
if (pred(*first)) return first;
and if ((*pred)(*first)) return first;
are exactly equivalent, correct?p - unary predicate which returns true for the required element. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. |
if (pred(*first)) {...}
are exactly equivalent, correct? |
The Predicate concept describes a function object that takes a single iterator argument that is dereferenced and used to return a value testable as a bool. |
In other words, what type replaces "UnaryPredicate"? |
|
|
Built-in function call operator A function call expression, such as E(A1, A2, A3), consists of an expression that names the function, E, followed by a possibly empty list of expressions A1, A2, A3, ..., in parentheses. The expression that names the function can be a) lvalue expression that refers to a function b) pointer to function c) explicit class member access expression that selects a member function d) implicit class member access expression, e.g. member function name used within another member function. http://en.cppreference.com/w/cpp/language/operator_other#Built-in_function_call_operator |
No. |
Identifier foo is the function’s name. But what type and value does foo have? It turns out that foo is actually a constant pointer to a function that takes no parameters and returns an integer. Function pointer foo holds (points to) the address in memory where the code for function foo starts. |
As you can see, the implicit dereference method looks just like a normal function call -- which is what you’d expect, since normal function names are pointers to functions anyway! |
There is an implicit conversion from an lvalue of function type T to a prvalue of 'pointer to function of type T' |
|
|
bool(*ptr_to_function)(int) = &isOdd
?bool(*)(int) ptr_to_function = &isOdd
?
|
|
|
|
|
|
-------- clang++/libc++ ------------ main.cpp:43:18: warning: address of function 'fn' will always evaluate to 'true' [-Wpointer-bool-conversion] std::cout << fn << '\n' ; // implicit conversion: 'function' to 'pointer to function' ~~ ^~ main.cpp:43:18: note: prefix with the address-of operator to silence this warning std::cout << fn << '\n' ; // implicit conversion: 'function' to 'pointer to function' ^ & 1 warning generated. -------------- a. pointer to object ---------------- 0x7fffea2fa960 0x7fffea2fa960 -------------- b. pointer to function ---------------- 1 1 1 true false -------------- c. pointer to member ---------------- true true --------- g++/libstdc++ ------------ main.cpp: In function 'int main()': main.cpp:37:9: warning: the compiler can assume that the address of 'rfn' will always evaluate to 'true' [-Waddress] b = rfn ; // implicit conversion: 'function' to 'pointer to function' ^~~ main.cpp:37:9: warning: the compiler can assume that the address of 'rfn' will always evaluate to 'true' [-Waddress] main.cpp:43:18: warning: the address of 'int fn(int)' will always evaluate as 'true' [-Waddress] std::cout << fn << '\n' ; // implicit conversion: 'function' to 'pointer to function' ^~ main.cpp:51:9: warning: the compiler can assume that the address of 'rfn' will always evaluate to 'true' [-Waddress] if(!rfn) {} // implicit conversion: 'function' to 'pointer to function', ^~~ main.cpp:51:9: warning: the compiler can assume that the address of 'rfn' will always evaluate to 'true' [-Waddress] -------------- a. pointer to object ---------------- 0x7fff1f3d6900 0x7fff1f3d6900 -------------- b. pointer to function ---------------- 1 1 1 true false -------------- c. pointer to member ---------------- true true |
Why does this print 1 in all cases?: |
In function 'int main()': 10:18: warning: the address of 'int foo()' will always evaluate as 'true' [-Waddress] 11:19: warning: the address of 'int foo()' will always evaluate as 'true' [-Waddress] |