473,405 Members | 2,171 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,405 software developers and data experts.

Objects w/ no type?!!?

Read this code I found on the Internet:

Object o,*oo; // Some objects
int (Object::*ptr_to_o_func)(double);
// ptr_to_obj_func is a pointer to a
// member function of Object

oo = getObjectPtr();

ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar;

std::cout << "Value is " <<
(o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
<< endl;

o.*ptr_to_o_func and oo->*ptr_to_o_func are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!

Jul 2 '07 #1
6 1517
On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:

Object o,*oo; // Some objects
int (Object::*ptr_to_o_func)(double);
// ptr_to_obj_func is a pointer to a
// member function of Object

oo = getObjectPtr();

ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar;

std::cout << "Value is " <<
(o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
<< endl;

o.*ptr_to_o_func and oo->*ptr_to_o_func are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!
o.*ptr_to_o_func and oo->*ptr_to_o_func are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).

I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.

--
Erik Wikström
Jul 2 '07 #2
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-07-02 23:30, Protoman wrote:


Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_to_o_func)(double);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar;
std::cout << "Value is " <<
(o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
<< endl;
o.*ptr_to_o_func and oo->*ptr_to_o_func are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!

o.*ptr_to_o_func and oo->*ptr_to_o_func are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).

I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Yes, I know but this guy says they have no type;
http://www.everything2.com/index.pl?...th%20no%20type

Jul 2 '07 #3
Protoman wrote:
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_to_o_func)(double);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar;
std::cout << "Value is " <<
(o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
<< endl;
o.*ptr_to_o_func and oo->*ptr_to_o_func are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!

o.*ptr_to_o_func and oo->*ptr_to_o_func are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).

I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.

Yes, I know but this guy says they have no type;
http://www.everything2.com/index.pl?...th%20no%20type

This person is either confused, or he is using a different definition of
type than the standard. His argument is that because

o.*ptr_to_o_func

cannot be assigned to anything, it has no type. Odd that he has never tried

o.foo,

which has the same "problem". Both o.*ptr_to_o_func and o.foo has the same
type: "The expression designates a nonstatic member function" says the
standard in 5.2.5. Such an expression can only be used with the function
call operator.

--
rbh
Jul 2 '07 #4
On 2 Jul, 16:40, Robert Bauck Hamar <roberth+n...@ifi.uio.nowrote:
Protoman wrote:
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_to_o_func)(double);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar;
std::cout << "Value is " <<
(o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
<< endl;
o.*ptr_to_o_func and oo->*ptr_to_o_func are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!
o.*ptr_to_o_func and oo->*ptr_to_o_func are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).
I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.
Yes, I know but this guy says they have no type;

http://www.everything2.com/index.pl?...bjects%20with%...

This person is either confused, or he is using a different definition of
type than the standard. His argument is that because

o.*ptr_to_o_func

cannot be assigned to anything, it has no type. Odd that he has never tried

o.foo,

which has the same "problem". Both o.*ptr_to_o_func and o.foo has the same
type: "The expression designates a nonstatic member function" says the
standard in 5.2.5. Such an expression can only be used with the function
call operator.

--
rbh- Hide quoted text -

- Show quoted text -
OK. BTW, are C++ functors lke closures in other languages? Like
"frozen" functions?

Jul 3 '07 #5
On Jul 3, 12:16 am, Protoman <Protoman2...@gmail.comwrote:
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_to_o_func)(double);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar;
std::cout << "Value is " <<
(o.*ptr_to_o_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
<< endl;
o.*ptr_to_o_func and oo->*ptr_to_o_func are functors.
I'm not sure. Functors are normally objects, not expressions.
you can't assign "o.*ptr_to_o_func" to anything, for example.
Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions?
Again, not necessarily. The usual definition is that they are
objects which can be "called"; objects for which the () operator
is defined.
I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!
o.*ptr_to_o_func and oo->*ptr_to_o_func are member function calls,
No they're not. They're "pm-expression"s, according to the
standard. And (again, according to the standard) their type is
"an object or a function of the type specified by the second
operand", here, a function. The resulting expression has the
type "function (double) returning int". And since the
expression isn't an lvalue, you can't take its address. In a
sense, C++ has closures, but only as temporary "objects", like
here.
and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).
That's the type of the results of the () operator, once it's
applied to the above expression.
I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.
I think they have a type. The standard says that they have a
"function" type.
Yes, I know but this guy says they have no type;http://www.everything2.com/index.pl?...bjects%20with%...
He's wrong. He just doesn't understand C++ typing as it applies
to functions. (But I'll admit that it's not always evident.)

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

Jul 3 '07 #6
On Jul 3, 9:26 am, Protoman <Protoman2...@gmail.comwrote:
TW, are C++ functors lke closures in other languages? Like
"frozen" functions?
Sort of. You can make something that is very close to a closure (ahem)
with a functor. You can also use them to do something similar to
currying.

struct power {
power( float second ) m_pow( second ) {}
float operator()( float first ) const { return std::powf( first,
m_pow ); };
private:
float m_pow;
};

power now acts like a curried version of std::powf - although beware,
I'm binding the second argument not the first so in a language like
Haskell you'd need to use flip too.

power cube( 3 );
cube( 10 ); // Will return 1,000

Note that the implementation of power uses what could be seen as a
closure. In C++ you don't get automatic closure of arguments within
the same lexical scope. You have to put them into the closing
structure yourself.

All of this is much clearer using Boost.Function and Boost.Lambda
where you can play with functional idioms more easily.

If you want to see this sort of thing in action, consider a function
for executing some script and returning a result. The lambda that will
execute the script has this type:

boost::function< _variant_t ( wstring ) >

This means that it takes a wstring (the source) and produces a
_variant_t (a wrapper for the Windows COM type VARIANT) which is the
result of the script execution.

Now to store a context for dynamic execution of a script I have this:

class Translator { // Creates HTML from some internal format
// snip
boost::function< _variant_t ( wstring ) dynWriter;
};

I initialise this through a function that looks like this (abridged
slightly to make it clearer what is going on from some code for a
multi-threading JavaScript host):

boost::function< _variant_t ( wstring ) embed() {
boost::shared_ptr< Mahlee mahlee( new Mahlee );

return boost::lambda::bind(
&Mahlee::run,
boost::lambda::bind(
&boost::shared_ptr< FSLib::Process::Mahlee >::get,
mahlee ),
boost::lambda::_1 );
}

The first line creates a JavaScript interpreter and puts it into a
shared_ptr. For the lambdas it makes two. The inner one simply creates
a closure around the mahlee object and allows us to get the object
pointed to through the shared_ptr's get member.

The outer lambda then creates a functor object that embeds the
following:

mahlee->run( _1 )

This is stored in the dynWriter member earlier:

dynWriter = embed();

Now dynWriter has become a function that given some JavaScript
executes it and returns the result of the code into a _variant_t:

dynWriter( L"function() { return 'Hello world!'; }()" );

This will return a _variant_t with the string Hello world! in it.

The hardest part doing this in C++ is that you have to be very careful
to think through the full implications of object lifetimes. You can
see a more advanced use that shows the difference between generalised
partial application and currying on the second half of this page from
my web site: http://www.kirit.com/News:/1720702
K

Jul 3 '07 #7

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

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
14
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
28
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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
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...

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.