function-call-operator

Check if a class has a possibly-overloaded function call operator

。_饼干妹妹 提交于 2020-06-27 04:11:14
问题 I am wondering whether it would be possible to implement a trait in C++20 to check if a type T is such that it has a possibly overloaded/possibly templated function call operator: operator() . // Declaration template <class T> struct has_function_call_operator; // Definition ??? // Variable template template <class T> inline constexpr bool has_function_call_operator_v = has_function_call_operator<T>::value; so that a code such as the following would lead to the correct result: #include

Why override operator()?

瘦欲@ 提交于 2020-01-08 11:41:19
问题 In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? 回答1: One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it

Why override operator()?

不打扰是莪最后的温柔 提交于 2020-01-08 11:41:09
问题 In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? 回答1: One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it

Function call operator [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-19 07:39:33
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: C++ Functors - and their uses. Why override operator() ? I've seen the use of operator() on STL containers but what is it and when do you use it? 回答1: That operator turns your object into functor. Here is nice example of how it is done. Next example demonstrates how to implement a class to use it as a functor : #include <iostream> struct Multiply { double operator()( const double v1, const double v2 ) const {

How do I implement the Fn trait for one struct for different types of arguments?

只谈情不闲聊 提交于 2019-12-07 05:15:16
问题 I have a simple classifier: struct Clf { x: f64, } The classifier returns 0 if the observed value is smaller than x and 1 if bigger than x . I want to implement the call operator for this classifier. However, the function should be able to take either a float or a vector as arguments. In case of a vector, the output is a vector of 0 or 1 which has the same size as the input vector: let c = Clf { x: 0 }; let v = vec![-1, 0.5, 1]; println!("{}", c(0.5)); // prints 1 println!("{}", c(v)); //

In a function call, what is the operator, and what are the operands?

吃可爱长大的小学妹 提交于 2019-12-06 17:50:47
问题 I am trying to understand some basics of C. KRC's The C Programming Language says A function call is a postfix expression , called the function designator, followed by parentheses containing a possibly empty, comma-separated list of assignment expressions (Par.A7.17), which constitute the arguments to the function. In a function call, what is the operator, and what are the operands? Is () the operator? Is the function name an operand? Are the arguments inside () operands? Is a function

Function call operator [duplicate]

☆樱花仙子☆ 提交于 2019-12-01 05:12:08
Possible Duplicates: C++ Functors - and their uses. Why override operator() ? I've seen the use of operator() on STL containers but what is it and when do you use it? That operator turns your object into functor. Here is nice example of how it is done. Next example demonstrates how to implement a class to use it as a functor : #include <iostream> struct Multiply { double operator()( const double v1, const double v2 ) const { return v1 * v2; } }; int main () { const double v1 = 3.3; const double v2 = 2.0; Multiply m; std::cout << v1 << " * " << v2 << " = " << m( v1, v2 ) << std::endl; } It

What are C++ functors and their uses?

眉间皱痕 提交于 2019-11-25 21:38:33
问题 I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful? 回答1: A functor is pretty much just a class which defines the operator(). That lets you create objects which "look like" a function: // this is a functor struct add_x { add_x(int x) : x(x) {} int operator()(int y) const { return x + y; } private: int x; }; // Now you can use it like this: add_x add42(42); // create an instance of the functor class int i =