Wednesday, March 17, 2004

Re: C++ generics

this has to do a lot with the design of the languages.

Yup you're right, it does have to do with how the different platforms are designed. Java and .NET are much more dynamic than C++. This affects how they've implemented generics.

i can definitely not comment on generics in c++. but i am very relieved with the constraints part in java/c#.

It's funny because when I first read about constraints I was kind of disappointed. I think a lot of people were. In comparison to C++ templates they put more limitations on its use. So looking at what was there before, I guess people expected more rather than less. But when you think about it, it makes sense.

one part in generics that was really confusing me was that how can i know what "type" the generic class will have to work with so that some important/basic common property can be worked on. in other words, since i can constraint the generic to IComparable ( i guess == Comparable in java) it is much simpler to understand how a sorting algorithm can be created.

Exactly. Those checks would have to be done at runtime since the actual generic class instantiated object is generated at runtime by the JITer. That would be horrible for performance. One of the main goals of generics is performance so it would pretty much defeat the purpose. With constraints all the compiler has to make sure is that the types implement the specified Interface or derive from a Base Class. And if you don't specify any constraints you can only use methods defined in object. Those methods are guaranteed to work cause every type derives implicitly from object.

how do such things work in c++? do you just hope that the class used overloads some operator or something. else generate a compile error? say for the same sorting algorithm, is there some basic comparison method/operator the class is to define. is there no way to "constraint" that?

I dunno how it works exactly - Need a compiler book/class for that ;-) The basic idea is that for every template class instantiation, it creates a new class with the appropriate type. It then checks that the type supports whatever methods are called on it. If not, you get a compiler error. It works without constraints because all the instantiations are done at compile (or maybe link?) time.

The build times are super fast for Java/C# compared to C++ because of their dynamic nature. Another consequence of "static" compilation of C++ is code bloat. When it comes time to instantiate the template classes with actual types, C++ will generate a new class for EVERY type it encounters in the code regardless of whether the application goes down that branch or not. With Java and C# this won't happen. New types are generated only if they are encountered and as needed dynamically.

why do i even need to bother with bruce, when i have mohn here!!

Let's see...

Bruce Eckel - author of Thinking in Java, Thinking in C++, C++ Inside & Out. Published over 150 articles in numerous magazines. Founding member of the ANSI/ISO C++ committee. Provides public and private seminars & design consulting in C++ and Java.

Mohnish Rao - contributor to codeWord.

Eckel's got NOTHING on me.

so i guess i goofed up again.!! :) . so metadata means adding more type info other than the general class stuff.

When was the first time you goofed up?

You had the idea. I think it's more a case of naming that's causing confusion. Everything is being labeled "metadata"... especially in .NET. I just think of everything that will be used by the runtime and JITer (aside from IL) as metadata.

i am coming to questions on metadata/attributes now. how do they work?

Attributes allow you to "tag" your code (a specific class/method etc...). When compiling, the compiler sees these tags and injects it's own code. Like the example you gave - @Remote - the compiler probably injected some "infrastructure" code to expose it as a method that can be called remotely (is that what @Remote does? I'm guessing here). There is an attribute in .NET called "WebMethod". You can use that on any public method and it can be used as a web service. That's all you have to do. All the actual SOAP stuff is handled by the CLR. Another is "Serializable".

also if you ever get any info on how generics have been implemented in java pls let me know.

Doesn't Sun have some Whitepaper published on their dev site?

I just wanted to say that everthing I'm writing about this stuff is how I THINK it works... how I understand it from what I've read. It could very well be that I'm making a huge arse out of myself and writing utter bullshit.

The point of this blog is to discuss this stuff and learn from each other. In other words... to try and become less of an arse.

No comments: