473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

function template and operator () overload

Hi, I have a interesting problem here,

class absOP{
template<class T> T operator(T val) { return val < 0 ? -val : val; }
};

Now the problem is I can't seem to use this overloaded operator,

absOP op(..);

int i = -42;
float f = -2.3;

int ai = op(i); //error
ai = op<int>(i); //error
ai = op(i)<int>; //error
ai = <int>op(i); //error

Alas, I tried a couple sane ways to use function template of overloaded
operator () but none seemed to work...Any idea or help is appreciated.

Feb 24 '06 #1
5 2739
Fei Liu wrote:
Hi, I have a interesting problem here,

class absOP{
template<class T> T operator(T val) { return val < 0 ? -val : val; }
Did you mean to write

template<class T> T operator()(T val) { return val < 0 ? -val : val; }

? Notice the "empty set of parens" after the word 'operator'. It is also
declared private at this point.
};

Now the problem is I can't seem to use this overloaded operator,

absOP op(..);

int i = -42;
float f = -2.3;

int ai = op(i); //error
ai = op<int>(i); //error
ai = op(i)<int>; //error
ai = <int>op(i); //error

Alas, I tried a couple sane ways to use function template of overloaded
operator () but none seemed to work...Any idea or help is appreciated.


This is covered by FAQ 5.8.

#include <iostream>
using namespace std;

struct abs_op {
template<class T> T operator()(T t) { return t > 0 ? t : -t; }
};

int main()
{
int i = -42, ii = 42;
double d = 3.14159, dd = -3.14159;

abs_op a;

cout << a(i) << ' ' << a(ii) << ' ' << a(d) << ' ' << a(dd) << endl;
}
V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #2
Thanks, Victor. I am sorry I didn't put enough thought in this example.
It's a little bit more complicated than the example I gave,
class somedata{
void * data;
template<class T> T operator()(size_t index) { return
*(reinterpret_cast<T*>data + index); }
};

somedata x(....); // data is initialized but actual type of data is
unknown. data is an array
float ele = x(3); // retrieve the 4th element of data array inside x,
error
float ele = <float>x(3); //error
float ele = x<float>(3); //error
float ele = x(3)<float>; //error

I hope this made it clearer.

Victor Bazarov wrote:
Fei Liu wrote:
Hi, I have a interesting problem here,

class absOP{
template<class T> T operator(T val) { return val < 0 ? -val : val; }


Did you mean to write

template<class T> T operator()(T val) { return val < 0 ? -val : val; }

? Notice the "empty set of parens" after the word 'operator'. It is also
declared private at this point.
};

Now the problem is I can't seem to use this overloaded operator,

absOP op(..);

int i = -42;
float f = -2.3;

int ai = op(i); //error
ai = op<int>(i); //error
ai = op(i)<int>; //error
ai = <int>op(i); //error

Alas, I tried a couple sane ways to use function template of overloaded
operator () but none seemed to work...Any idea or help is appreciated.


This is covered by FAQ 5.8.

#include <iostream>
using namespace std;

struct abs_op {
template<class T> T operator()(T t) { return t > 0 ? t : -t; }
};

int main()
{
int i = -42, ii = 42;
double d = 3.14159, dd = -3.14159;

abs_op a;

cout << a(i) << ' ' << a(ii) << ' ' << a(d) << ' ' << a(dd) << endl;
}
V
--
Please remove capital As from my address when replying by mail


Feb 24 '06 #3
Fei Liu wrote:
[...]
First of all, please don't top-post. I've rearranged it.
Victor Bazarov wrote:
Fei Liu wrote:
Hi, I have a interesting problem here,

class absOP{
template<class T> T operator(T val) { return val < 0 ? -val : val; }
Did you mean to write

template<class T> T operator()(T val) { return val < 0 ? -val : val; }

? Notice the "empty set of parens" after the word 'operator'. It is also
declared private at this point.

};

Now the problem is I can't seem to use this overloaded operator,

absOP op(..);

int i = -42;
float f = -2.3;

int ai = op(i); //error
ai = op<int>(i); //error
ai = op(i)<int>; //error
ai = <int>op(i); //error

Alas, I tried a couple sane ways to use function template of overloaded
operator () but none seemed to work...Any idea or help is appreciated.


This is covered by FAQ 5.8.

#include <iostream>
using namespace std;

struct abs_op {
template<class T> T operator()(T t) { return t > 0 ? t : -t; }
};

int main()
{
int i = -42, ii = 42;
double d = 3.14159, dd = -3.14159;

abs_op a;

cout << a(i) << ' ' << a(ii) << ' ' << a(d) << ' ' << a(dd) << endl;
}

Thanks, Victor. I am sorry I didn't put enough thought in this example.
It's a little bit more complicated than the example I gave,
It's not just "more complicated". The template doesn't have the argument
of type 'T', and that makes it _impossible_ to tell which 'T' is going to
be used. The compiler cannot deduce it from 'size_t', and the fact that
you're using the _operator_ syntax makes it impossible to provide any
information for the deduction.
class somedata{
void * data;
template<class T> T operator()(size_t index) { return
*(reinterpret_cast<T*>data + index); }
};

somedata x(....); // data is initialized but actual type of data is
unknown. data is an array
float ele = x(3); // retrieve the 4th element of data array inside x,
error
float ele = <float>x(3); //error
float ele = x<float>(3); //error
float ele = x(3)<float>; //error

I hope this made it clearer.


Yes. You're SOL. Don't use operator syntax. Convert to a regular
function, like so

class somedata {
...
template<class T> T doit()(size_t index) ...
};

float ele = x.doit<float>(3);

V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #4
Unfortunately, I cannot convert it to a function. So it seems it's not
possible to make it work when function template and operator () are
used together the way I intended. Thanks again.

Feb 24 '06 #5

Fei Liu wrote:
Unfortunately, I cannot convert it to a function. So it seems it's not
possible to make it work when function template and operator () are
used together the way I intended. Thanks again.


It's possible.

template<class T> struct type {};

struct S
{
template<class T>
T operator()(type<T>, size_t);
};

int main(int ac, char** av)
{
S s;
s(type<int>(), 1);
}

Feb 25 '06 #6

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
10
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
1
by: amhoov | last post by:
Hi all, OS - MacOS X 10.4.9 Compiler - gcc 4.0 (XCode) I'm *extremely* new to C++ (but have extensive experience with Java), so please go easy on me. I'm running into a very odd situation...
5
by: C++Liliput | last post by:
Consider the following code template<class T1, class T2> T2 sum(T1 a, T1 b) { T2 ret = a + b; return ret; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.