POV-Ray : Newsgroups : povray.off-topic : Undirected ramblings on C++ : Undirected ramblings on C++ Server Time
28 Jul 2024 12:25:26 EDT (-0400)
  Undirected ramblings on C++  
From: Orchid Win7 v1
Date: 27 Feb 2014 14:23:11
Message: <530f909f$1@news.povray.org>
One of the most unexpected outcomes of my current employment is the 
amount of time I spend writing C++ code.

I remember I did spend a while dabbling with C++ years ago. (I think I 
even got Warp to install Haskell round about the same time...) But I 
never managed to write much beyond Hello World with it. But now, I find 
myself writing highly non-trivial code in C++. And that's kind of 
alarming, given I know nothing about C++...

I'm always immensely surprised when I compile the code, and it actually 
*works*. It seems so unlikely that my naively cobbled-together nonsense 
could ever function correctly. But somehow, it does. (Sometimes!)

I lose count of the number of times I've spent an hour or so writing 
some C++ code in Visual Studio, I send my work to source control, and 
GCC chokes on it and refuses to compile it. I honestly can't figure out 
whether this is a defect of GCC or VS.

For example, yesterday I wrote the following code:

   auto iter = inputs.begin();

VS happily accepted this declaration, and the code performed the way I 
had intended it to. But GCC emitted some gabled message to the effect 
that "iter" hasn't been declared or some such... Replacing the "auto" 
keyword with an explicit type signature fixed the problem. But I didn't 
think this was an especially new language feature...

In a similar vein, today I tried giving a variable the same name as a 
class. VS didn't care; it compiled and ran my code just fine. GCC gave 
me an utterly baffling error message which took me some considerable 
while to figure out. Renaming the variable made it happy again.

I can't figure out whether VS is being too permissive or GCC is being 
too restrictive, but the two frequently differ on what is and isn't 
permissible code. (And then there's the preprocessor macros - but we 
*know* those are evil...)

Not that VS is perfect, of course. Yesterday it gave me the dreaded 
C2512 error: "no appropriate default constructor available". I searched 
the Internet, and EVERY SINGLE QUESTION about this involved a class 
where somebody had *clearly* defined a custom constructor, thereby 
disabling the automatic creation of a default constructor. My class, by 
contrast, had NO METHODS AT ALL, let alone a constructor. WTF?

As best as I can tell, this is somehow due to it being impossible to 
auto-initialise reference fields? Not that any error message *told* me 
this or anything like that...

Speaking of which... I think I may be fundamentally misunderstanding 
what references actually *do*. I had thought they were like pointers, 
just that you can't make them null. But some sources claim they're like 
pointers who's value can never change. So what, pray tell, does the 
following do?

   std::vectpr<int> foo;
   std::vector<int> bar;
   std::vector<int> & baz = foo;
   baz = bar;

I had imagined that the final line changes baz from pointing to foo and 
to instead point to bar. But... if a reference can never change... then 
what the hell am I changing?? O_O

It's times like these that make me want to step away from the keyboard 
and go for a walk outside for a while. This stuff is just too 
complicated to be left to mere mortals. It should only be touched by 
people with a PhD in the official language spec.



If there's one thing that's more astonishing than writing complex C++ 
code and having it *work*, it's calling that code from a real 
programming language and having *that* work!

I still can't quite believe that I wrote a complicated regular 
expression parsing and matching engine in C++, and built a Windows DLL 
out of it, and called it from C#. Literally, our application has a C# 
class in it. It looks for all the world like a perfectly ordinary C# 
class. But every time you invoke a method, it's actually running C++ 
code. Dynamically loaded from a DLL at runtime. And it freaking works! 8-O

Of course, if it doesn't work, good luck ever figuring out what the 
problem is! :-P

Hell, today I even wrote a Haskell wrapper around the DLL. So I'm 
sitting there writing all this ultra-high-level code that tranposes 
linked lists and stuff, and somewhere deep in the computer's innards 
it's actually running clunky old C++ code. Weird, but true.

(Actually, I recently added a new feature to the regular expression 
engine. It was so hard, I wrote it in Haskell first, so I could figure 
out the algorithm. And then I spent an entire day trying to get C++ to 
do it. It's almost shocking how tiny the Haskell implementation is... 
This is no trivial algorithm!)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.