Reflection/ Introspection

L

Lars Schouw

Is there any plans to add reflection to the C++ compiler?
I understand it might slow down execition speed, but should be fast
when I don't use reflection.

Lars
 
M

Marcel Müller

Lars said:
Is there any plans to add reflection to the C++ compiler?

RTTI are reflections and are supported for many years. But most probably
you are talking about method->invoke or something like that.
I understand it might slow down execition speed, but should be fast
when I don't use reflection.

To be able to use reflections you must add tons of meta data about all
of your types, members and parameters to your object and executable
files (in fact what you currently call debug info). And you MUST have a
compiler as part of the runtime because you could invoke a template by
reflection for type parameters that have not yet been instantiated.

Code that requires reflection is almost always bad by design, except if
it is a debugger. And well, debuggers already exist and they use
reflections. It is just not part of the language.


Marcel
 
K

Kai-Uwe Bux

Juha said:
Compile-time reflection in template code could sometimes be useful.
For example, code which says something like "if the template type has a
member function named 'reserve', call it, else don't".

That degree of introspection is already possible (although not nice).

#include <cstddef>

namespace tpl {

template < typename T >
class has_reserve {
/*
stolen from Rani Sharoni, who attributes this to
Richard Smith and also Artem Livshits
*/

typedef char (&no) [1];
typedef char (&yes) [2];

template < typename S, void ( S::* ) ( std::size_t ) >
struct dummy {};

template < typename S >
static
yes check ( dummy< S, &S::reserve > * );

template < typename S >
static
no check ( ... );

public:

static bool const value = sizeof( check<T>(0) ) == sizeof( yes );

}; // has_reserve

template < typename T, bool b >
struct call_reserve_if;

template < typename T >
struct call_reserve_if< T, true > {
static
void call ( T & t, std::size_t arg ) {
t.reserve( arg );
}
};

template < typename T >
struct call_reserve_if< T, false > {
static
void call ( T & t, std::size_t arg ) {}
};

} // namespace tpl

template < typename T >
void call_reserve_if_possible ( T & t, std::size_t arg ) {
tpl::call_reserve_if<T, tpl::has_reserve<T>::value >::call( t, arg );
}


#include <vector>
#include <list>
#include <iostream>
#include <ostream>

int main ( void ) {
std::vector<int> a;
call_reserve_if_possible( a, 100 );
std::list<int> b;
call_reserve_if_possible( b, 100 );
std::cout << a.capacity() << "\n";
}

(What is currently missing but slated for C++0X is a test for the existence
of constructors.)


What one could really miss is a degree of compile time introspection that
would allow one to, e.g., write a generic serialization.


Best

Kai-Uwe Bux
 
I

Ian Collins

RTTI are reflections and are supported for many years. But most probably
you are talking about method->invoke or something like that.


To be able to use reflections you must add tons of meta data about all
of your types, members and parameters to your object and executable
files (in fact what you currently call debug info). And you MUST have a
compiler as part of the runtime because you could invoke a template by
reflection for type parameters that have not yet been instantiated.

Code that requires reflection is almost always bad by design, except if
it is a debugger. And well, debuggers already exist and they use
reflections. It is just not part of the language.

Serialisation is an obvious exception to that rule.
 
M

Marcel Müller

Ian said:
Serialisation is an obvious exception to that rule.

If you want it to become considerably slow to meet modern hardware
requirements, then you are right.

If you want fast serialization, the code to enumerate the members should
be generated at compile time rather than at run time. Of course, you
will not get this for free. You have to provide a list of members to
serialize for each class. But this has the advantage that you can easily
distinguish between original data and maybe some cached values that can
be reconstructed on demand.


Marcel
 
I

Ian Collins

If you want it to become considerably slow to meet modern hardware
requirements, then you are right.

If you want fast serialization, the code to enumerate the members should
be generated at compile time rather than at run time. Of course, you
will not get this for free. You have to provide a list of members to
serialize for each class. But this has the advantage that you can easily
distinguish between original data and maybe some cached values that can
be reconstructed on demand.

Well yes, generation at compile time would be in the spirit of C++
rather than the runtime model used by scripting languages.

I must admit I have used reflection in PHP, normally to work out what a
received object is and what it can do. In that environment, reflection
is a form of run time template specialisation. So I guess you could
argue that if reflection is almost always bad by design, so is template
specialisation!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top