473,657 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static operator() in functor?

Hi everyone,

Can someone point me out why I can't declare the operator() of a
functor as static? The reason behind this is that I want to be able to
call to the function without instantiating the Functor object. The
code is as follows:

#include <iostream>
using namespace std;

class Functor
{
public:

static void operator()(doub le c, double f){
cout<<"c -"<<c<<endl;
cout<<"f -"<<f<<endl;
}
};

int main()
{
Functor::operat or()(4,5);
return 1;
}

The error message is:
aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx:9: error: 'static void Functor::operat or()(double, double)'
must be a nonstatic member function

Thank you.

May 27 '07 #1
2 20143
aaragon wrote:
Hi everyone,

Can someone point me out why I can't declare the operator() of a
functor as static?
An operator operates on an instance of a class.

--
Ian Collins.
May 27 '07 #2
On May 27, 4:08 am, aaragon <alejandro.ara. ..@gmail.comwro te:
Can someone point me out why I can't declare the operator() of a
functor as static?
Because the standard forbids it.

I don't think that there is any underlying technical reason, per
se; it would be easy for the compiler to implement. But what
would the operator syntax be if you didn't have an object; the
purpose of an overloaded operator is to be able to use the
operator syntax. Thus:

struct Toto
{
static void operator()() ; // suppose this were legal...
} ;

Toto aToto ;

aToto() ; // fine...
Toto::operator( )() ; // also OK.

// But how would use use the operator syntax without an
// object?

Given this, it makes "logical" sense to require it to be a
non-static member.
The reason behind this is that I want to be able to
call to the function without instantiating the Functor object. The
code is as follows:
#include <iostream>
using namespace std;
class Functor
{
public:
static void operator()(doub le c, double f){
cout<<"c -"<<c<<endl;
cout<<"f -"<<f<<endl;
}
};
int main()
{
Functor::operat or()(4,5);
return 1;
}
The error message is:
aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx:9: error: 'static void Functor::operat or()(double, double)'
must be a nonstatic member function
The obvious solution: give the function a name, and call the
named function in your operator()(). The raison d'être of an
operator is to allow use with the operator syntax.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3652
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
2
2393
by: Charles-Antoine Giuliani | last post by:
Is it possible to overload multiple times operator() ? For example for using one or two arguments ? My visual c++ does not seem to like it : I was trying to implement an arbitrary integer precision class (I know such libraries already exist) and I wrote the following piece of code : template <typename T> class plus_with_carry { public: plus_with_carry(void) : carry(false) {};
6
1542
by: Michal Wyrebski | last post by:
Hello, I'm new in this group and I don't know if my questions are too silly, but I'm intermediate programmer and don't have enought experience. Please be charitable. I don't know how to write an operator() class to be properly executed inside for_each()? Maybe example will be more accurate:
8
1521
by: tron.thomas | last post by:
I'm using Microsoft Development Environment 2002 Version 7.0.9466 on Windows XP Professional Service Pack 2. If I compile the following code at maximum warning level: #include <functional> class SetValue : public std::unary_function<int, void> { public: explicit SetValue(int& value) : m_value(value) {}
3
4443
by: chsalvia | last post by:
In generic programming, static member functions and functors seem to be very useful. I've noticed that some libraries take the approach of using templated static member functions to provide generic functionality, while others use functors. Take for example the std::string char_traits class. Here, a static member "eq" is defined to serve as a generic binary comparison function. My implementation defines it as; static bool eq(const...
12
2144
by: mathieu | last post by:
Hi, Consider the following (*). Is there a way to rewrite it so that it remains convenient (N is being recomputed when array v is modified) *and* compiles :) Thanks, -Mathieu (*)
2
1593
by: mathieu | last post by:
Hi there, I was recently help for an issue with the following code: template <typename T, unsigned int N> struct Functor { T values; };
2
5508
by: Paul Bilokon | last post by:
Hi all, A very simple question, but I couldn't find a definitive answer in the literature. Functors provide you with the function call syntax: foo(1, 2, 3) may be a call to a functor. Now, what if I have a pointer to a functor instead? Is the following syntax the recommended, or perhaps the only, way to call operator() on a functor pointer: fooPtr->operator()(1, 2, 3) Many thanks! Regards, Paul
3
1763
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...); 3: delete p; line 2 I know it should be fine because global operator new should return
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.