• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Koren

Member
So you love the way it hurts?
I think he meant he likes that most people wouldn't believe it works or wouldn't think of it.

And I agree.

At the same time, I really enjoy C++ *except* virtually anything related to templates... or at least user-built ones (STL is fine), I feel it's such a weak and bad solution to bring polymorphism to C.

So I may not be the best one to ask.
 
So you love the way it hurts?

I never said that, just explaining what problems you would face if you tried to remove :: from C++. My point about the template disambiguator is that it is a dark corner of C++ that should remain in the dark, because it's actually shit. And that anything that would make it needed more often would probably not be worth the perceived gains because of how shitty the template disambiguator is.

In any case, I'm not saying that if C++ had been designed from the beginning without :: in mind the problems would have been unsolvable. Just explaining why you :: is needed in C++ today and why you couldn't just replace it (ignoring all the code it would break, obviously)

I will say though, that Java and C# don't have the notion of global variables. Everything is inside of a class. So what I pointed out as the most difficult part of removing :: from C++ (global disambiguation) are not an issue those languages have to deal with.

So if Rust had wanted to use one operator instead of two, it would still need to address that problem. Maybe the solution would be to still have a :: for global resolution and . for everything else, but it's not immediately clear that's the best solution. Also known as: even the slightest changes in language semantics can have a butterfly effect on the way future language features are designed / implemented. So you really have to be careful, it's not as easy as jsut saying "yea this language does it, it would totally work!" People spend years thinking about and designing language proposals.

I think he meant he likes that most people wouldn't believe it works or wouldn't think of it.

And I agree.

At the same time, I really enjoy C++ *except* virtually anything related to templates... or at least user-built ones (STL is fine), I feel it's such a weak and bad solution to bring polymorphism to C.

So I may not be the best one to ask.

I loooooooove templates. I don't use them often, and I don't think people should use them often. But compare that to operator overloading, where I both hate them and think you should be fired for using them ever. But I think the STL is a testament to how powerful they really are. There is no other language that has a generic programming model as powerful and efficient as C++.
 

Skinpop

Member
I loooooooove templates. I don't use them often, and I don't think people should use them often. But compare that to operator overloading, where I both hate them and think you should be fired for using them ever. But I think the STL is a testament to how powerful they really are. There is no other language that has a generic programming model as powerful and efficient as C++.

even with vector math data structures? I've used/written libraries without operator overloading and I hated using them. I think that's one exception where it's totally fine to use overloading.

as for templates, tanking compile times and obnoxious compiler errors make them pretty useless to me. I'd rather generate the code myself.
 

Koren

Member
even with vector math data structures? I've used/written libraries without operator overloading and I hated using them. I think that's one exception where it's totally fine to use overloading.
We've discussed thisa couple weeks ago... It's a perfectly valid reason to use overloading there (besides, Stroutstrup played a lot with those, and who am I to discuss his work? ^_^) but it would be better if only responsible people would use overloading on this, and not anyone finding it funny to play with operator()...

For example, overloading operator* on matrices is probably good, but to implement dot product on 1D vectors, it's dangerous.

(V*V has no meaning. Both tV*V and V*tV have, why should you choose one over the other?)

One of the main gripes I have with numpy in Python is how broadcasting and other tricks "to simplify" expressions can drive you directly into insane and dark bugs...
 

Skinpop

Member
(besides, Stroutstrup played a lot with those, and who am I to discuss his work? ^_^)

his work? has he ever written a program that is used by anyone? to me bjarne is the biggest clown in the industry and I don't take anything he says seriously. the only thing he is concerned about is c++ adoption and ivory tower academic ideals - not programming.

For example, overloading operator* on matrices is probably good, but to implement dot product on 1D vectors, it's dangerous.
I agree. for dot I use a function call. I meant overloading for +-*. I don't think I've ever used it outside of vector math though.
 

Koren

Member
I loooooooove templates. I don't use them often, and I don't think people should use them often. But compare that to operator overloading, where I both hate them and think you should be fired for using them ever. But I think the STL is a testament to how powerful they really are. There is no other language that has a generic programming model as powerful and efficient as C++.
STL is written by sane people...

I don't have issues with the concept of templates.

I have issues with how they were implemented (although there probably weren't better solutions). Mainly
- how the syntax become sometimes a mindfuck because of those
- the fact that headers contains code (nearly all the code sometimes), which is heresy to me
- error messages are just awful, even for a misplaced bracket
- plently of warnings won't trigger... compare signed and unsigned in C, you'll get a warning, but use unsigned in min<int> and watch the compiler believe it's a predicate and stay silent
- optimization will be awful because the compiler don't know what you're after, and performance is a huge reason of using C
etc.

They're nicer in D, and polymorphism can be great in ML, but I feel like sometimes playing with lisp macros where I shouldn't

You use them because they're useful, but...


And STL is really nice, but it's API isn't that great...


Also, I wonder if I really needed turing-completeness at compilation time ^_^
 

Somnid

Member
I loooooooove templates. I don't use them often, and I don't think people should use them often. But compare that to operator overloading, where I both hate them and think you should be fired for using them ever. But I think the STL is a testament to how powerful they really are. There is no other language that has a generic programming model as powerful and efficient as C++.

There's lot of languages with good generics that are easier to understand as well as ones where it's not super necessary either. You'd probably love Rust macros though. Also, such things seem difficult to test though I'm sure people must have some way of doing it. I get why people might like them but it seems to be a can of worms to me, I guess I generally like to do things in the most readable way possible and have trust issues with code from others. Maybe some day I'll warm up to meta-programming.
 
even with vector math data structures? I've used/written libraries without operator overloading and I hated using them. I think that's one exception where it's totally fine to use overloading.

as for templates, tanking compile times and obnoxious compiler errors make them pretty useless to me. I'd rather generate the code myself.

So you'd rather have:

IntVector
FloatVector
VectorOfVectorOfInts
StringVector

and re-implement all the code each time? Or have all your methods accept and return void*s?

Templates are the only thing that make generic libraries even possible.

obnoxious errors are almost a solved problem through the use of static_assert, and will be even more solved if concepts ever make it into the language.


Operator overloading is shit because you can't ever figure out where the code for a particular operator lives. Looking at code that says a += b? Good luck finding that method so you can put a breakpoint on it. Is it a global operator? Is it a class member of a? Is it a class member of a base class of a? What if there's a global and a class operator that could both work?

The syntax with using methods is more verbose, but at least you can find the code that backs the operation without pulling your hair out.

I can probably be convinced that certain use cases of operator overloading are ok (such as the index operator on a map), but I'm *way* more likely to push back on the introductino of an overloaded operator than I am on a template. At least a template is trying to solve a real problme (most of the time) as opposed to a perceived problem.
 

Koren

Member
So you'd rather have:

IntVector
FloatVector
VectorOfVectorOfInts
StringVector

and re-implement all the code each time? Or have all your methods accept and return void*s?

Templates are the only thing that make generic libraries even possible.
Only because you needed to build this over C...

ML polymorphism works better, I'd say, for example.

And I'll grant you vectors, but that's in the standard, the same question you rise about overloading could be rised... who should write templlates?
 

Koren

Member
his work? has he ever written a program that is used by anyone? to me bjarne is the biggest clown in the industry and I don't take anything he says seriously. the only thing he is concerned about is c++ adoption and ivory tower academic ideals - not programming.
As one of the people behind C++, I'd say he has some rights to express his feelings about what you can do with the language...

In this case, I remember reading code from him where he overloaded operators for matrices.


Not to say I'll agree with everything (I don't agree often with Guido von Rossum, but I'll still read him)


As for his work, I'm not sure, but I'm not convinced that you need to be a developper to design a langage.

Is my memory wrong, though, or did he helped Microsoft a lot into making Visual a decent compiler some time ago?


I agree. for dot I use a function call. I meant overloading for +-*. I don't think I've ever used it outside of vector math though.
I remember using it for sets and CSG at least.

I think each time your object is mathematically a group or a ring with clearly defined operators, it's not a big issue...
 
Only because you needed to build this over C...

ML polymorphism works better, I'd say, for example.
Well obviously. But we did need to build it over C, so what are you gonna do right? Gotta play the hand you're dealt. People need and will continue to need generic programming support in a language that is close to the metal.

I love ML (actually i love F# better, but same thing) but it's not like you could fit ML or even Haskell's type system into C++ and at the end of the day people need to use C++.

And I'll grant you vectors, but that's in the standard, the same question you rise about overloading could be rised... who should write templlates?

Sure vector is in the standard, but what about sparse_matrix, or vector in the mathematical / computer graphics sense? There's a million real world examples where you need to apply the same algorithm or structure to different types, and templates are the only practical solution.
 

Skinpop

Member
and re-implement all the code each time? Or have all your methods accept and return void*s?

Templates are the only thing that make generic libraries even possible.
It depends on what I use them for. If I need to generate code, then well, I'll generate it myself. Then there's macros. It's uncommon that I need generic libraries.
here is an example of a c++ style vector in c:

I can probably be convinced that certain use cases of operator overloading are ok (such as the index operator on a map), but I'm *way* more likely to push back on the introductino of an overloaded operator than I am on a template. At least a template is trying to solve a real problme (most of the time) as opposed to a perceived problem.
I generally agree, but I think it's fine when limited to vector math or similar things.

As one of the people behind C++, I'd say he has some rights to express his feelings about what you can do with the language...
sure he does, but I don't have to agree with what he is saying :)
 

injurai

Banned
Well obviously. But we did need to build it over C, so what are you gonna do right? Gotta play the hand you're dealt. People need and will continue to need generic programming support in a language that is close to the metal.

I love ML (actually i love F# better, but same thing) but it's not like you could fit ML or even Haskell's type system into C++ and at the end of the day people need to use C++.

Isn't this what Rust achieves though? It leverages an ML type system for generic programming.
 
Isn't this what Rust achieves though? It leverages an ML type system for generic programming.

Well yea but it's not C++. No matter how good it is, it's not going to satisfy the people who have billions of lines of c++ sitting around that they need to improve. So I'm just saying that there are lots of people who need to use -- specifically -- C++. And if you're one of those people, you need templates.

From a theoretical perspective I'm not arguing templates are the best possible implementation of generic programming, just that they're a verrrrrrrrrrrrrry good model in c++ and if you are using c++, it would be a mistake to ignore them

It depends on what I use them for. If I need to generate code, then well, I'll generate it myself. Then there's macros. It's uncommon that I need generic libraries.
here is an example of a c++ style vector in c:
But why would you want a C++ style vector in C when you're already using C++? Just so you don't have to use a template? That doesn't make sense.
 

Skinpop

Member
But why would you want a C++ style vector in C when you're already using C++? Just so you don't have to use a template? That doesn't make sense.
that's just an example of how c++ style vectors can be implemented in c. not using templates in c++ makes a lot of sense to me for various reasons.
 

Koren

Member
sure he does, but I don't have to agree with what he is saying :)
Never said you should (I can't count the number of times I disagreed with GvR or Linus Toswald)

Well obviously. But we did need to build it over C, so what are you gonna do right?
I agree, I just don't like them.


Sure vector is in the standard, but what about sparse_matrix, or vector in the mathematical / computer graphics sense?
Not sure that's the absolute best example since it'll be limited to numeric types... If you really needed it, you could still probably define everithing using a numeric_type and simply using a

typedef double numeric_type

at the beginning of the file and changing it if you need. Granted, it's a weaker template mechanism.


I love defining sparse matrix objects, but I'm not sure anyone should in real world, though. We're back to who should be allowed to use it...
 

Skinpop

Member
Are you interested in sharing the reasons?

compile times, debugging/error messages, code readability.
I'm not against templates if their impact on the above is minimal and I do use a few templated libs, I just find that most of the time I don't need them or rather that most of the time they don't provide enough of a benefit for me. i'd be happy to use templates more if they worked better in cpp.
 
compile times, debugging/error messages, code readability.
I'm not against templates if their impact on the above is minimal and I do use a few templated libs, I just find that most of the time I don't need them or rather that most of the time they don't provide enough of a benefit for me. i'd be happy to use templates more if they worked better in cpp.

That's a good reason to be careful with them, but not a good reason to not use them. I mean, I agree it's easy to use them incorrectly. 6 or 7 years ago I worked at a company where we had a product that was probably about 60k lines of C++ code that made heavy use of templates. It took 15 minutes to compile. Now I work on a codebase that's 5-6 million lines of code, and it still takes 15 minutes to compile. We still use templates.

I do agree that most of the time you don't need them, but if you're sitting there hand-generating code, then that's a pretty good sign that you need them.

One good reason I can think of not to use them is that you don't feel confident enough with them, but the solution to that IMO is to go learn more about them and get more practice experimenting with them so that you can build that confidence.

Used correctly they can greatly improve code readability and maintenance because it forces you to think about the ways in which two algorithms or data structures are the same or different, and it encourages those abstractions where you break off a piece of functionality into something that can be shared by many different things with one piece of code.

The difficulty of course is that when you try to go learn about them, you see all this theoretical mental masturbation exercises about template metaprogramming and you do a table flip because it all seems so excessive and over the top. But that stuff just exists not to encourage you to go use all that stuff in your own code, but rather to help you become comfortable with them and to think inside of that mental model. So that when *actual* useful situations arise where you can use them, you will at least recognize the opportunity.
 

Mr.Mike

Member
I don't really get why people worry about compilation times. (Constant time operation eh?). Of course I haven't actually worked as a Software Engineer yet, so maybe I'm just ignorant of how inconvenient it is.

For debugging you have Makefiles and separate compilation so it shouldn't take too long to compile and test code after making a change. And who cares how long it takes to compile a release build from scratch with optimizations on ( how often are you building release version?).
 

Somnid

Member
I don't really get why people worry about compilation times. (Constant time operation eh?). Of course I haven't actually worked as a Software Engineer yet, so maybe I'm just ignorant of how inconvenient it is.

For debugging you have Makefiles and separate compilation so it shouldn't take too long to compile and test code after making a change. And who cares how long it takes to compile a release build from scratch with optimizations on ( how often are you building release version?).

When it takes on the order of minutes to compile a big bloated codebase it matters because you make one change and sit around for a while browsing NeoGaf. Highly inefficient.
 

injurai

Banned
When it takes on the order of minutes to compile a big bloated codebase it matters because you make one change and sit around for a while browsing NeoGaf. Highly inefficient.

The poster is right, if things are correctly setup with a nice build tool you should be able to do partial compilations. Also if you engineer you're software with that in mind.

But if none of that is done, you really can't turn the clocks back and you're stuck with the massive compilation times. Which is more than likely when the project is being developed by countless devs and you find yourself in dependency hell.

But 15 minute compilations are better than interpreted projects that take hours to run through QA and unit test sessions. Fuck all of that noise.
 

Somnid

Member
The poster is right, if things are correctly setup with a nice build tool you should be able to do partial compilations. Also if you engineer you're software with that in mind.

But if none of that is done, you really can't turn the clocks back and you're stuck with the massive compilation times. Which is more than likely when the project is being developed by countless devs and you find yourself in dependency hell.

But 15 minute compilations are better than interpreted projects that take hours to run through QA and unit test sessions. Fuck all of that noise.

And then I'd take hours of unit-tests/acceptance tests over weeks of manual regression testing which is frequently how it works in many enterprises.
 
The poster is right, if things are correctly setup with a nice build tool you should be able to do partial compilations. Also if you engineer you're software with that in mind.[/code]
Sometimes you have to change something in a core header file.

Sometimes you sync to tip of trunk and hundreds (or potentially thousands) of other CLs have gone in since then.

Sometimes you switch to a different branch to work on something different, and the changes are different enough that it requires a huge incremental recompile.

idle time spent waiting for a build to finish is one of the biggest sources of inefficiencies in the developer workflow that exists. Some large companies have invested hundreds of thousands of man hours building their own distributed build systems that build stuff in parallel on remote machines and send you back the binaries, because it's faster than building locally.


Then there's the issue of continuous build infrastructure. Every non-trivial project is using continuous build infrastructure that is compiling on every submitted CL. They need to have a fast turnaround time otherwise they get backlogged and can't keep up with the rate of changes.
 

injurai

Banned
I think we are reaching a point where sufficiently sized software is solely limited by the bus factor. Because build systems like that are routinely, in my experience, a point of failure that is always failing. It's like the project is always on fire, and it only matters that you're not in the repo that is.
 

Koren

Member
That's a good reason to be careful with them, but not a good reason to not use them. I mean, I agree it's easy to use them incorrectly.
Not a good reason to not use them, but a good one to get pissed because of them...

6 or 7 years ago I worked at a company where we had a product that was probably about 60k lines of C++ code that made heavy use of templates. It took 15 minutes to compile. Now I work on a codebase that's 5-6 million lines of code, and it still takes 15 minutes to compile. We still use templates.
Technically, 7 years is 30-40x in flops, throw in slightly better compilers and more cores, I'm not convinced it says much about usage...

On the subject, ten years ago, I was working in a lab that had installed a massively paralel compiler, your code was distributed over all the computers in the lab, it was blistfully fast. I wish I could still use it.
 
Actually I'm not convinced I actually have a memory leak issue.

I think the check for memory leaks is occurring before my destructor is ever called.
 

Skinpop

Member
I don't really get why people worry about compilation times. (Constant time operation eh?). Of course I haven't actually worked as a Software Engineer yet, so maybe I'm just ignorant of how inconvenient it is.

For debugging you have Makefiles and separate compilation so it shouldn't take too long to compile and test code after making a change. And who cares how long it takes to compile a release build from scratch with optimizations on ( how often are you building release version?).
most of the stuff I do is graphics and simulation related so my workflow involves lots of small iterations. even just adding a few seconds to the compilation time is extremely annoying, especially when I am live coding.
 

Slavik81

Member
I don't really get why people worry about compilation times. (Constant time operation eh?). Of course I haven't actually worked as a Software Engineer yet, so maybe I'm just ignorant of how inconvenient it is.

For debugging you have Makefiles and separate compilation so it shouldn't take too long to compile and test code after making a change. And who cares how long it takes to compile a release build from scratch with optimizations on ( how often are you building release version?).
Compiling is the longest part of running a test. If the turnaround time for 'does my code work?' is instant, I'm doing great. If the turnaround time is 2 seconds, I'm wasting minutes each day. If the turnaround time is 20 seconds, I get bored and end up browsing reddit for at least a minute each time.
The poster is right, if things are correctly setup with a nice build tool you should be able to do partial compilations. Also if you engineer you're software with that in mind.
Worth noting that on large projects, Make may take a very long time just to figure out what (if anything) to rebuild.
 
Also worth mentioning c++ modules again since they speed up compiles by an order of magnitude. Msvc and clang are both shipping with partial support for modules as of right now
 

irongear

Neo Member
Guys i have i have an assignment that is driving me crazy in java, this is the assignment:

Write a program ChatBot.java, where the user inputs a one-line sentence and the computer responds accordingly.
Specification:
The computer should continuously ask and respond to questions according to the following rules:

If the sentence ends with a question mark (?) and has an even number of spaces in the sentence, then respond with "yes" (note: 0 is an even number)
If the sentence ends with a question mark (?) and has an odd number of spaces in the sentence, then respond with "no".
If the sentence ends with an exclamation mark, then respond with "Wow!"
If the user enters "quit", then the program exits
In any other case, respond with:
You always say "<user input>"
Make sure the print out includes the quotation marks

example dialog:

Hello! Say something to me!
Are you the greatest?
no

Hello! Say something to me!
Are you the greatest robot?
yes

Hello! Say something to me!
yay!
Wow!

Hello! Say something to me!
I can't find my burrito
You always say "I can't find my burrito"
____________________

i will appreciate any help and hints (and what type of methods i will need etc), thanks!!
 

injurai

Banned
Databases feel like a completely foreign language compared to other programming languages. It's like I'm speaking corporate lingo.
 

Mr.Mike

Member
Databases feel like a completely foreign language compared to other programming languages. It's like I'm speaking corporate lingo.

I read somewhere that SQL is basically a functional language, which has really affected the way I think about it, and now I think it's pretty cool.
 
Top Bottom