Live data from Hacker News

Popular Myths about C++, Part 1

isocpp.org

121–130 of 144 posts

Re: Popular Myths about C++, Part 1

#121
post #118

Earlier quoted context omitted.

would it be fair to say you have just described references, or do I have to go and lookup this algol/mesa stuff?

Some examples using Modula-2, a Mesa's successor. How it allows for all the C pointer features, while adding a little bit more safety. MODULE examples; IMPORT Terminal, SYSTEM; (* procedure with reference parameter, no need for pointers *) PROCEDURE ChangeParam (VAR changeMe: CARDINAL); BEGIN changeMe := 25; END ChangeParam; (* procedure with a slice, bounds given by HIGH(changeMe) and LOW(changeMe). also a string in…

very interesting, reminds me of this:

http://stackoverflow.com/questions/6456253/why-is-reference-...

it's a great shame it's defined in such an alien to use way in C++.

Re: Popular Myths about C++, Part 1

#122
post #33

Earlier quoted context omitted.

Right. Also, the C version would be cleaner using the printf family of functions. Idiomatic C would use snprintf and accept a buffer. If you insist on returning a heap-allocated object, you could use asprintf, which is not universal but is common. Either way it's a one-liner.

Yeah, but as soon as you use snprintf you're throwing performance to the wind.The idiomatic C++ solution should beat it hands down. ( gulp )

Have you benchmarked that? Last I tried some not-so-rigorous microbenchmarks, *printf beat C++ << streams by a factor of 3 on msvc 11 (VS2012).

Re: Popular Myths about C++, Part 1

#123

Earlier quoted context omitted.

https://github.com/torvalds/subsurface pretty interesting! the .c's heavily outnumber the .cpp's. there's nothing like having your prejudices confirmed to harden them, so I question whether he is more welcoming.

There's a video about it https://www.youtube.com/watch?v=ON0A1dsQOV0 Apparently, Qt takes away most of the problems they have with C++

So the guy who took over from Linus doesn't mind Qt. The guy reiterates how much Linus does not like C++. I want my 12 minutes back!

Re: Popular Myths about C++, Part 1

#124
post #40

A major problem with C++ is a lack of consistency, which you run into if you start trying to generalize the ideas presented in this article. The article demonstrates string concatenation using +. That is great! Except it's inconsistent. The article's example works fine, of course: return name+'@'+domain; I'll skip over the weird use of '' instead of "". Now let's say I want to prepend mailto: as well: return "mailto:…

Not that this is necessarily any cleaner, but in C++14 one would write:

    return "mailto"s + ":"s + name + "@"s + domain;
and it would compile fine. (s being a literal for std::string)

Re: Popular Myths about C++, Part 1

#125
post #91
post #40

A major problem with C++ is a lack of consistency, which you run into if you start trying to generalize the ideas presented in this article. The article demonstrates string concatenation using +. That is great! Except it's inconsistent. The article's example works fine, of course: return name+'@'+domain; I'll skip over the weird use of '' instead of "". Now let's say I want to prepend mailto: as well: return "mailto:…

>>The next example demonstrates initializing a vector: >> vector v = {1,2,3,5,8,13}; >>This is great, of course. The example after that introduces "auto" so you don't have to write the type of a variable. Well heck, let's combine the two! >> auto v = {1,2,3,5,8,13}; The auto example creates an std::initializer_list which then can be converted into a vector or a set std::vector newvectorfromauto(v); std::set newsetfro…

What he has in mind is that it is logical and reasonable for C++ to behave in this way, but completely incomprehensible for beginners who has just learned the basics of programming.

Re: Popular Myths about C++, Part 1

#126
The list initialization syntax touted by Bjarne Stroustrup himself as beginner friendly is not even friendly to advanced users. Tell me how to debug or teach students the difference between

    std::vector{10}
    std::vector{10}

Re: Popular Myths about C++, Part 1

#127

"Finally, which version is likely to be the most efficient? Yes, the C++ version, because it does not have to count the argument characters and does not use the free store (dynamic memory) for short argument strings." I just compiled the C code and the C++ code using: gcc -std=c99 -Wall -Wextra test.c -o test g++ -std=c++11 -Wall -Wextra test.cpp -o test According to valgrind: C: total heap usage: 1 allocs, 1 frees,…

This is the archetypal problem with benchmarks: not only are they hard to design, but people will incorrectly nit-pick the results. You count the number of allocations, without looking at the big picture: this is a toy example. It provides the hard-coded input to the function. This results in the C version not having to allocate memory for the hard-coded literals and the function problably being inlined and optimized…

C++14's "meow"s is syntactic sugar for string("meow"), with identical efficiency.

Re: Popular Myths about C++, Part 1

#128

Earlier quoted context omitted.

> The answer in all cases is C. Starting to sound like a broken record. No, the answer in all cases can also be found in C++, and understood in C++ terms alone. C++ is a SUPERSET of C, not just an arbitrary language + C.

cheers for the broken record warning. i will now abstain from further conversation - i will leave you with this - i don't care to read your answer in case i have to respond and you might have to deal with some more repetition: using the language of sets, the set of C++ minus the set of C, with what is now left: how can you explain the things i have listed?

[deleted]

Re: Popular Myths about C++, Part 1

#129

Earlier quoted context omitted.

> The answer in all cases is C. Starting to sound like a broken record. No, the answer in all cases can also be found in C++, and understood in C++ terms alone. C++ is a SUPERSET of C, not just an arbitrary language + C.

cheers for the broken record warning. i will now abstain from further conversation - i will leave you with this - i don't care to read your answer in case i have to respond and you might have to deal with some more repetition: using the language of sets, the set of C++ minus the set of C, with what is now left: how can you explain the things i have listed?

>how can you explain the things i have listed?

As evolution within the same language. Inconsistent ideas ironed out for more uniform and safer handling.

A lot of used languages have similar historical warts...

How do you explain "var" in JS, when "let" exists?

Re: Popular Myths about C++, Part 1

#130
post #110

Earlier quoted context omitted.

> why do structs exist and why are their members public? why don't you just use classes with public? To be able to reuse existing C code in a C++ compiler, while allowing them to participate in class hierarchies. This is the real reason, but you don't need to explain it like that to new C++ devs. Use struts when you just need a data container. Use classes when you need to add behaviour. Simple rule, no need to talk a…

people keep using this "new C++ devs" proviso, it's a red herring. to remind you: > What exactly do you need to know about C, that is not part of C++, to understand C++? why can't you memcpy classes but you can structs? it's a confederation of languages, i think coldtea's idea of representing it as a superset is excellent, you cannot understand C++ by taking out its C subset. if that's your attitude where it's not th…

> why can't you memcpy classes but you can structs?

Since when?! Try this on a struct and it will go boom!

The only difference between structs and classes is the default access specifier.

> C++ is full of this. you only get to "why" with C.

No, you get the why by knowing Assembly and how computers work.

Any system programming language since FORTRAN exposes the same set of issues.

We never teached C at our university (during the 90's), first year students had Pascal followed by straight C++.

If they ever need to use C, it was based on what they learned from C++. There were no C lectures, nor teaching what is C or C++.

I never saw anyone speaking about learning BCPL to use K&R C, or having to learn Pascal to use Ada or Modula-2.

Post reply on HN