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