From: Acer on
Hi group!

I read the FunctionTest.cpp in /test directory and see this:

struct X
{
X(int x);
int plus(int x);
};
// ...

X one(1);
Loki::Function<int (int)> f2(&one,&X::plus);

It works fine and so I write some similar code for std::vector<int> as
follows:

vector<int> primTable;

Function< size_t () > p_size(&primTable, &vector<int>::size ); //
O.K.
Function< vector<int>::reference (size_t)> p_ref(&primTable,
&vector<int>::at); // generate compile error

Error:

error: no matching function for call to 'Loki::Function<int& ()
(size_t)>::Function(std::vector<int, std::allocator<int> >*,
<unresolved overloaded function type>)'

/home/yangacer/NameDB/loki/include/loki/Function.h:156: note:
candidates are: Loki::Function<R ()(P01)>::Function(int) [with R =
int&, P01 = size_t]

/home/yangacer/NameDB/loki/include/loki/Function.h:156: note:
Loki::Function<R ()(P01)>::Function(const Loki::Function<R ()(P01)>&)
[with R = int&, P01 = size_t]

/home/yangacer/NameDB/loki/include/loki/Function.h:156: note:
Loki::Function<R ()(P01)>::Function() [with R = int&, P01 = size_t]

Is this a known issue or I miss-understood something?

Thanks for your time :-)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
{ Apparently, the poster tried to correct an error from the previous
article which looks very similar to this one but is not the same.

Please be sure to inform the moderators that the old one should be
cancelled; otherwise it can be approved earlier than the new one,
and an article once approved cannot be taken back. -mod }


On Aug 5, 3:06 am, Acer <yanga...(a)gmail.com> wrote:

> Function< size_t () > p_size(&primTable, &vector<int>::size ); //
> O.K.
> Function< vector<int>::reference (size_t)> p_ref(&primTable,
> &vector<int>::at); // generate compile error

Looks like Loki::Function's constructor isn't very smart.
Try (vector<int>::reference (vector<int>::*)(size_t))&vector<int>::at
instead of &vector<int>::at.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Aug 5, 3:06 am, Acer <yanga...(a)gmail.com> wrote:

> Function< size_t () > p_size(&primTable, &vector<int>::size ); //
> O.K.
> Function< vector<int>::reference (size_t)> p_ref(&primTable,
> &vector<int>::at); // generate compile error

Looks like Loki::Function's constructor isn't very smart.
Try (vector<int>::reference(*)(size_t))&vector<int>::at instead of
&vector<int>::at


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Stefan van Kessel on
Hi, Acer

the problem with the std::vector<>::at is that it is const overloaded.
You'd get the same Problem with the struct X if you add a function like
"int plus(int x) const". While I don't really know how the compiler
handles pointers to member functions which have const overloads (maybe
somebody else can elaborate on that?), the following code works for me:


std::vector<int> primTable(7);
primTable[3] = 19;

typedef std::vector<int>::reference
(std::vector<int>::*AtType)(std::vector<int>::size_type);
AtType at = &std::vector<int>::at;
Loki::Function<std::vector<int>::reference
(std::vector<int>::size_type)> p_at(&primTable, at);
p_at(3)=7;
std::cout<< p_at(3) <<std::endl;


Have a nice day,
Stefan

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]