-
Need to See Template Instantiations
I think I've asked this about g++ (which doesn't seem to do what I want). Does someone know of a compiler or similar tool that will generate the instantiated templates that are then compiled?
I'm trying to work through some hairy templates (learning more generic programming) and it would really help if I could see what the compilable code looks like once the templates are instantiated.
-
you can look at the objects in visual studio and see the type of the members etc of each object, but not the created class as if it were typed out in code. If that helps.
-
I'm not too sure that that would help you much to be honest. The easiest way to "see" the whole instantiation is to create a compile error (by say using a const object where a non-const object is required). The compiler will spit out lines and lines of gobbledigook which you can then C&P in your favourite editor and dissect. It's not pretty...
take for example string which is a typedef of basic_string which is a template. Produce an error where you pass a const string to a template that requires a non const string and try to understand the error message. This is what you are up against.
This is a bit of a destructive approach of reverse eneneering in that you break a thing in order to understand the inner workings, but there you go.
When I write template(-libraries) I do it usually in following manner: First create a non-template version for one (ideally typedef'd) type. make it work, test all the methods, confirm there are no leaks and other memory issues, and then replace the typedef'd type with a template. Specify the template with the type you had previously typedef'd and confirm all of the above. Then try other types/classes and see where it breaks - and it will break. When it breaks then you have to decide whether the break is acceptable (say you never will want to use your template with a non-numeric type, then there is no need to provide for strings), or if you intend to use it with the type, then you write a template specialisation for the type or if you're lucky then you can extend the original template to fix the problem.
Last edited by drkybelk; 09-01-2010 at 04:11 AM.
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
Here's a derived question: I'm trying to catch up on tr1 and the new C++0x, but--so far--I can't penetrate the following typedef. I can use it, but I don't understand it. Can someone layout what exactly this template instantiates?
Code:
typedef functional<void()> Message.
It's from a Herb Sutter article in Dr Dobb's. One can create a nice collection:
and Queue up a several functions and functors like so:
Code:
q.push_back(a_function);
q.push_back(bind(&A_Class:Member_function, instance));
q.push_back(bind(A_function, a_parameter));
Then you can execute them like so:
Code:
Message m = q.pop_front();
m();
Again, I can use it, but I don't quite understand it.
-
as far as I can tell its making a typedef for
<functional> void()
MSDN has this on functionals
http://msdn.microsoft.com/en-us/libr...b0(VS.71).aspx
but I don't really understand it either. Its as if its a way to make a constant value into the result of a bogus function to fool certain interfaces into thinking there is a function to invoke when there is not really one. I have no idea why this would be necessary.
Your example is a neat trick but I don't see why this could not be done with straight up function pointers --- thats where I got lost.
-
The technique is too fresh with me to justify it one way or the other, but it comes from an interesting article by Herb Sutter on Dr Dobbs:
http://www.drdobbs.com/go-parallel/a...leID=225700095
I recommend the read. You might have to register to read it.
-
I'm not aware of any compiler that generates the C++ code for template specializations. In practice, compilers that generate the code use an intermediary language (pseudo code, or even assembly). Besides, even if there were such tools I doubt that you can learn much from generated code. It looks very different from handwritten code.
The technique you presented is perhaps too advanced and complex. You don't have to use it as is. Start with simpler constructs such as using std::function as a binder for functions and see how the code works.
Message m = q.pop_front();
m();
pop_front() pops the front object from the queue. m gets a copy of that object. Then m(); invokes the member function you previously stored in that element's position using the arguments, if any, passed to it.
My advice: start dissecting std::function and learn how it works. The rest will be much easier.
Danny Kalev
-
> I think I've asked this about g++ (which doesn't seem to do what I want).
> Does someone know of a compiler or similar tool that will generate
> the instantiated templates that are then compiled?
g++ with -frepo perhaps? http://gcc.gnu.org/onlinedocs/gcc-4....-Instantiation
> I can't penetrate the following typedef. I can use it, but I don't understand it.
std::function<> is a polymorphic call wrapper which figures out what it is wrapping using template meta-programming. The template parameter is a function type that describes the return type and the argument types of a callable object. function<> can hold any target callable object that can be called with the argument types (with conversions) and returns a type that can be converted to the object's return type. It allows us to specify the type of a callable object based on a loose knowledge of just its argument and return types.
Here's a simple example to get you started:
Code:
#include <functional>
#include <iostream>
struct A
{
int member_function( int arg ) const
{ std::cout << "A::member_function\n" ; return arg ; }
};
long free_function( A& a, double arg )
{ std::cout << "free_function\n" ; return arg ; }
struct function_object : std::binary_function< A, short, short >
{
short operator() ( const A& a, short arg ) const
{ std::cout << "function_object::operator()\n" ; return arg ; }
};
int main()
{
// polymorphic call wrapper
typedef std::function< int( A&, float ) > function_t ;
// wraps any callable object that can be called with any two compatible
// arguments and returns a value that can be converted to an int
function_t functions[3] =
{
&A::member_function, // pointer to member function int (A::*) (int)
free_function, // pointer to free function long(*)(const A&,double)
function_object() // object with function call operator
} ;
A a ;
for( int i=0 ; i<3 ; ++i )
{
auto& fn = functions[i] ;
std::cout << fn( a, 2.3f ) << '\n' ;
}
}
-
-
View Template instantializations
What I do in MS Dev Studio: Go to debug mode, set breakpoints where template is called or in template member function, and debug. You will send into instantialized template function, whether it is specialization, partial specialization, etc.
-
Thanks for the suggestions.
For anyone who wants to try and understand "what lies beneath", I bought "Modern C++ Design: Generic Programming and Design Patterns Applied", by Andrei Alexandrescu.
He develops the infrastructure behind something very similar to what becomes std::function in C++0x.
As you might guess, it is pretty involved, but elegant at the same time.
Similar Threads
-
By Peter_APIIT in forum C++
Replies: 7
Last Post: 12-09-2008, 12:32 AM
-
By greenmile in forum C++
Replies: 2
Last Post: 10-16-2008, 10:09 PM
-
By anitha2324 in forum AJAX
Replies: 0
Last Post: 06-18-2008, 04:22 AM
-
By bmac in forum VB Classic
Replies: 0
Last Post: 12-28-2005, 04:01 PM
-
Replies: 2
Last Post: 07-26-2005, 07:41 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks