« first day (89 days earlier)      last day (4860 days later) » 

11:00 AM
@JohannesSchaublitb By they way, why don’t you think const-by-default is sane for locals? – Looking at my current code base, almost no variable ever gets modified after its declaration (not counting loop variables)
 
it feels most of my local nonparameters change
 
@KonradRudolph Loop variables could also be const, providing you get a fresh variable with every iteration which represents the current index :)
 
@litb: Turtle?!
 
try it as an experiment … in some project, make your locals all const an see how far it goes
 
@Konrad: That sounds odd.
 
11:03 AM
194
Q: How do I move the turtle in LOGO?

Joel SpolskyHow do I move the turtle in LOGO?

 
@Konrad: Do you have only constants, or something? What about your strings, streams, iterators, containers, loop counters, accumulators..?
 
@FredOverflow Conceptually this is right but it doesn’t work in C++ since increment mutates the variable.
 
@Konrad: What on earth sort of code are you writing?!
 
@TomalakGeretkal Not exclusively, but mainly, yes. I do use accumulators and containers which are not const. But in the latter case, this is mainly because C++ doesn’t have (yet) a good way to initialize them with all their values
 
11:05 AM
  boost::shared_ptr<int const> i(new const i(0));
  i = new const i(*i + 1);
etc :P
@Konrad: My feeling is that that is a very rare scenario.
 
in all fairness, we talked about const variables
 
I really like the way F# handles this:
let x = 42             // x is a constant
let mutable y = 4711   // b is a variable
 
I would prefer let and var (respectively) as keywords but yes, F# handles this classy
 
The entire purpose of a program is to take input and create output; without mutability, a program could do nothing. The usefulness is in the mutability of data. const seems to me to be just fine as something you bolt-on for added safety where you need it.
 
@KonradRudolph With an actual for loop it would work. C++'s for loop is just a sugared while loop.
 
11:06 AM
@litb: I knew you'd go there!
 
@TomalakGeretkal Oh but purely immutable programs do exist and work very well. In other languages (Java, C#), all my classes are immutable
 
sbi
@KonradRudolph Which is why I make most of my local variables const in C++. But I'm anally obsessed about constness anyway...
 
Anyway I think we talked about const variables only because too many people don't know the difference in terminology from const objects
 
@sbi sounds painful
 
11:07 AM
7
Q: C++ const keyword - use liberally?

JessicaIn the following C++ functions: void MyFunction(int age, House &purchased_house) { ... } void MyFunction(const int age, House &purchased_house) { ... } Which is better? In both, 'age' is passed by value. I am wondering if the 'const' keyword is necessary: It seems redundant ...

lol
 
@KonradRudolph Yes, but the variables pointing to the objects are still mutable, right?
@TomalakGeretkal In C++, variables and objects are almost exactly the same thing ;)
 
Not really
 
@FredOverflow In C#, yes because you cannot make locals readonly. In Java, no. Everything (!) is final
 
> A variable is introduced by the declaration of an object.
@TomalakGeretkal That is what the C++ standard says.
 
How in any way does that equate to them being "almost exactly the same thing"?
 
11:09 AM
By the way, as an example of my C++ code which only uses consts:
template <typename T>
T uniform_range(T from, T to) {
    unsigned int const seed = std::rand();
    double const udev = seed * (1.0 / (RAND_MAX + 1.0));
    return static_cast<T>(from + udev * (to - from));
}
 
@TomalakGeretkal In the way that every variable is also an object.
 
An object is -- in the program realm --, basically, a block of memory. A variable is -- in the realm of your C++ code -- your way to access and manipulate that object.
 
For example, if you say int i; then i denotes an int object.
 
@Konrad: I see. The const seems utterly pointless there and only serves to make your code hard to read, but I do see your argument for a function like that.
@Fred: Correct. However "i" itself is a variable.
 
a variable is a declared object
 
11:10 AM
@TomalakGeretkal I like that view.
 
"is" vs "denotes" is not equality or identity
 
so it sums up to "a variable is an object that has a name"
 
@TomalakGeretkal Turns out you're even more anal than me about terminology :) I bow my head.
 
I think of variables as more like handles.
@Fred: :P
 
@TomalakGeretkal It makes the code much easier to read and to maintain for me: const guarantees that the variable will never be mutated (I don’t use const_cast) which is a very nice guarantee to have. Granted, in a 3-line function this is not too useful but the longer the functions get, the better it is to ensure that your variables aren’t mis-appropriated later on
 
sbi
11:11 AM
@KonradRudolph Why? It's just six more chars to write, you do have to think about modifying locals anyway, and your decision can always be changed later.
 
though the spec uses it even in c++03 in the meaning of "references and objects that have a name", which is nearly c++0x definition
 
@sbi: that’s the point; my decision won’t change unless I have a serious design flaw in the code. The six characters more to write suck, which is why it would be nice if const were the default
 
@sbi Right, const is useless, because any programmer can later remove it from the source code :)
@JohannesSchaublitb Could C++ have done without references, or were they inevitable?
 
… and i rarely think about modifying locals. If I need a new value, I introduce a new variable for it.
 
@FredOverflow lol
 
11:13 AM
@Fred: operator overloading doesn’t work without references. So they were necessary
 
@KonradRudolph How about typedef const int cint? ;)
 
hmm seems like i was wrong. union { int a; }; <- the unnamed union object is a variable, right? since we declared it!? what about void f(int) { } <- the unnamed int object. is it a variable!?
 
@Fred: better yet: #define int const int
 
but then the standard seems to assume every variable has a name :)
 
@KonradRudolph I think using C++ reserved words as macro names is UB
 
11:14 AM
Konrad seems so sure of himself that his decision won't change. Hmm.
Operator overloading doesn't work without references? What?
@KonradRudolph That's UB.
 
@Fred that was a joke
 
Also, it's fugly. :)
Make better jokes!
 
i think it just implicitly assumes that the unnamed object in void f(int); does not introduce a variable, even though it contains a declaration of an object
 
@JohannesSchaublitb Show me one that doesn't
Exactly.
 
looks like a "declaration" ambiguity. because "declaration" actually can mean two things
 
11:15 AM
There is an object passed into the function f, but there is no variable there.
 
@TomalakGeretkal My experience has shown that this works very well for me. Of course, it’s a very special style of programming (and C++ doesn’t support it well). It’s trained like any other programming skill. Having worked a bit in functional languages has helped.
 
1) the introduction of a name into a translation unit 2) the syntactical construct
 
@Johannes I once asked a question about variables in C++ :)
5
Q: What exactly is a variable in C++?

FredOverflowThe standard says A variable is introduced by the declaration of an object. The variable's name denotes the object. But what does this definition actually mean? Does a variable give a name to an object, i.e. are variables just a naming mechanism for otherwise anonymous objects? Or is a v...

 
@Konrad: I don't see how anything that's led you to program against the grain of the language and have to write six extra characters on almost every line and lengthen every line.... is in any way helpful.
Working on your code must result in RSI!
@litb: Aye, seems so
@litb: Can't say I'm surprised :)
 
sbi
@FredOverflow By that definition, every code is useless.
 
11:19 AM
@sbi code is an uncountable noun
 
sbi
@KonradRudolph What's typing six chars to you? Compared to the time I need to think about my code, typing is a non-issue. (If it was, I would be a better typist.)
 
@sbi Do we need a mechanism to make source code const? ;)
 
sbi
@FredOverflow And modifiable on demand and with tracking who did what. It's called "VCS" and it's out there.
 
BTW "this" was added to the entity list of c++0x too
because they need implicit "this" capture for "used" entities in c++0x for lambdas :)
 
@TomalakGeretkal But this method does have advantages. It makes the code much more robust because it ensures that no variable is reused. The foremost use is that it makes the code more self-documenting. It informs the reader that each variable has a sole purpose and won’t be changed, which in turn makes flow analysis (for the reader but potentially also for the compiler) easier.
 
11:21 AM
@Johannes: I think we should go on a hunger strike until the "rvalues" vs. "temporaries" vagueness in the standard is resolved. Are you with me? ;)
 
@Fred i posted the issue to the committee, but nobody answered yet. let's hope they will discuss it on the madrid meeting :)
 
@sbi Typing six characters isn’t the problem – it’s the extra six characters cluttering my code. I hate clutter. With a fiery passion.
 
but i fear they won't. :)
 
@JohannesSchaublitb In that case, I will buy every copy of the standard, make a huge pile and set it on fire!
 
@Fred well you can support by complaining to WMM
 
11:24 AM
@KonradRudolph So you are effectively giving names to values (functional programming) instead of giving names to objects (imperative programming).
 
if more people than just me saying "some guys complained to me and i agree" do so, they are more likely to do something about it
 
@JohannesSchaublitb What is WMM? (First I read WWM and thought "Wer wird Millionär", the german version of "Who wants to be a millionaire"...)
 
@FredOverflow: yes. I prefer working with values. Much less to debug.
 
Günther Jauch, the german C++ expert? :)
 
Damn it, is everybody in her German or based in Germany? What a pest. ;-)
 
11:25 AM
no
 
@KonradRudolph Functional programming is awesome. Will C++ ever truely support it? :)
 
@FredOverflow WMM is William M. Miller, works at EDG and maintains the core language issues list
@FredOverflow and he also worked out the paper that introduced that wording into the draft, afaik
 
@KonradRudolph I'm not saying it doesn't have advantages. I see the advantages. They're pretty obvious. I just don't like the disadvantages and the fact that -- as is at the very root of your issue -- it doesn't fit the language. It goes against the grain.
 
so he's best qualified to send complaints to lol
 
@FredOverflow Forget it. Sooner will pigs fly. [In Burns’ voice: you didn’t see that]
 
11:26 AM
@JohannesSchaublitb So he is the culprit! Burn him at the stake!
 
@FredOverflow dunno :)
according to the issues list, he's at wmm@edg.com :)
 
@KonradRudolph Classic Simpsons :)
 
@TomalakGeretkal You’re right there. To be honest, I don’t practice this as religiously as I’ve made it seem here because ultimately it just doesn’t work in C++. C++ fundamentally relies on mutating values (+= and ++ versus +, assignment operator …) … but I try to.
And I model my code flows accordingly
Well, back to my code … Divide & conquer. Yum.
 
sbi
@KonradRudolph I agree that const should be the default. So should be explicit. And a few others. C++, however, is a compromise between a 40 year old past of C, 25+ years of C++ history, and trillions of lines of legacy code.
 
@KonradRudolph Traitor! ;)
@sbi I agree, explicit should be implicit, and implicit should be explicit :)
4
 
sbi
11:30 AM
@FredOverflow Pragmatist? Get as much as you can, instead of resorting to "I can't have it all anyway..."
@FredOverflow LOL! That's a good one.
 
@sbi I don't like compromises. I prefer pure thinking in my ivory tower to writing real world code.
 
sbi
@FredOverflow We all do. But compromises is what lets you take money home at the end of the day/week/month, so we all have to bend to them.
 
@sbi I like the end of the year best. You know, Christmas bonus ;)
 
@KonradRudolph :)
Do you people seriously keep clicking the reply button over and over? That icon is tiny.
 
@TomalakGeretkal Yes. As C++ programmers, we are used to pay attention to teeny tiny details ;)
2
 
11:36 AM
@FredOverflow Now that is dedication to StackOverflow.com chat.
 
sbi
@FredOverflow Never been in one of those companies. :(
 
@FredOverflow Of course. There are limits though.
 
@sbi Apparently, you get Christmas boni (what is the correct plural of bonus in english?) at universities.
 
teeny tiny details lol
 
sbi
@FredOverflow In <limits>. I know.
 
11:38 AM
@Fred: "Bonuses", I'm afraid.
 
@FredOverflow You don’t. (well, I didn’t) – not as a student assistant, at any rate
 
@TomalakGeretkal I prefer the latin plural :)
 
@FredOverflow You're not speaking Latin.
 
sbi
@FredOverflow If you work there? I must have done something wrong then. When I was studying, I tutored informally for free, and when I went back later as a lecturer, I got payed by the hour, excluding cooking up and reviewing homework and written tests.
 
we dont get bonusses in our FH
 
11:39 AM
@TomalakGeretkal "bonus" is Latin for "good", and generally, the plural of "-us" is "-i", right?
 
@sbi Sounds normal.
@FredOverflow (a) There is no such hard-and-fast rule, even in Latin. (b) "Bonus" here is borrowed from Latin, but you're using it as an English word. The plural in English is "bonuses". :)
 
sbi
@FredOverflow No, I is singular, us is plural.
 
@sbi Good one
 
wow you need to see tomalak in ##c++ he's really a english-grammar hero
 
@JohannesSchaublitb love you
 
11:40 AM
lol
 
@sbi I felt that joke coming the second I sent my comment :)
 
sbi
@JohannesSchaublitb Well, considering that we others discussing here are all Germans...
 
@JohannesSchaublitb That should be an english-grammar hero!
 
@FredOverflow ohh!
 
11:41 AM
crazy Germans.
 
sbi
@FredOverflow Yeah, sorry. I'm well-known for making the obvious joke.
 
@sbi So's your ... mum?
 
sbi
@TomalakGeretkal No. But she's very well-known to frown upon this habit.
 
@TomalakGeretkal Your mom doesn't follow the Rule of Three! And she uses naked pointers!
 
@FredOverflow she doesn’t have any friend s, but has #define private public
 
11:43 AM
template<int ... your> void f(your ... mum);
 
@FredOverflow I have checked, and "bonus" in Latin is the masculine form of a word in the first/second declension, which means that, yes, the plural would be "bonī" (when masculine). But we're still not speaking Latin. :)
 
@TomalakGeretkal Why is the "i" on "boni" looking funny? Pronunciation?
 
sbi
@TomalakGeretkal Of course, we all knew that, because "boni" is the proper plural for "bonus" in German.
 
@JohannesSchaublitb That code doesn’t compile
 
@FredOverflow She frowns upon dangling pointers and shared pointers, but doesn't mind employing the odd "smart pointer" implementation once in a while.
@KonradRudolph It doesn't?
 
11:44 AM
@KonradRudolph yes it does on gcc -std=c++0x xD
 
@JohannesSchaublitb Then your gcc is mighty broken. (hint, your your ... are ints, they can’t be used as parameter types)
 
i'm at fail
2
 
@Konrad: Lern2Semicolon!
 
template<class ... your> void f(your ... mum);
 
11:58 AM
Hurr hurr. Federal german agencies fail at Unicode: ix.de/-1168966
Better not have any diacritics in your name, else your ID will crash their software
 
 
4 hours later…
3:40 PM
hi
anyone know about MFC
 
no
nothing
 
thanks anyway...
 
a little bit
from years ago
 
3:59 PM
it's silly that the OP decides which answer is right. by the virtue of the fact that they did not know the answer (hence the question), they could not possibly be authoritative in the matter.
 
@TomalakGeretkal The rationale was that the OP knows the use-case best. And the “best” answer would be voted by the community. But I agree that this concept should be revisited. I’ve got the idea that a few of the original ideas just aren’t up for debate, no matter how they’ve turned out in practice.
 
@Tomalak: who should decide, then?
 
@John: the peers.
 
Via voting?
 
They already do … perhaps “accepted answer” isn’t such a useful feature after all
except of course for extra gratification
 
4:03 PM
Perhaps
 
@KonradRudolph I don't agree. Sometimes the most up-voted "answer" does not answer the question.
 
@DanielTrebbien Do you have an example of that? I can’t imagine a case except when the original question is so bad that “no answer” is actually the best answer.
 
@KonradRudolph Not off the top of my head.
It's rare, but I have seen it.
Also, when a large number of people up-vote the first answer(s), but the OP modifies his/her question to be more specific.
 
@Konrad: Well, the strncpy question may be a good example
0
Q: C++ Equivalent of strncpy

KayHi all, What is the equivalent of strncpy in C++? strncpy works in C, but fails in C++. This is the code I am attempting: string str1 = "hello"; string str2; strncpy (str2,str1,5);

In this case, the first answer (also the most upvoted & accepted answer, perchance mine) is wrong, and doesnt answer the question properly. All other answers are also wrong.
 
@JohnDibling if anything, that’s an example in my favour: that question sucks.
 
4:11 PM
@JohnDibling Good example
 
Not sure the question sucks. Its pretty clear.
 
I didn't know that the std::string(const char*, size_t) constructor was like strncpy in that it stops at the first NUL character '\0'.
Errr... Wait. The C++ standard for that constructor says that the effect is "[to construct] an object of class basic_string and determines its initial value from the array of charT of length n whose first element is designated by s".
It says nothing about stopping at the first NUL char.
 
@JohnDibling After it was extensively edited. The first version of the question was unintelligible.
 
@JohnDibling Nobody.
Votes should be enough.
 
@Tomolak: Interesting. You should post the idea to meta if you feel strongly about it
 
4:18 PM
@DanielTrebbien It's not.
@JohnDibling I don't really care. Was just commenting. :)
 
@TomalakGeretkal I misread one of John's comments. My mistake.
But the copy(charT*, size_type, size_type) member function does not stop at the first NUL char either.
 
@DanielTrebbien Nope. In general, wherever you provide a character count, assume that that is in lieu of null-termination.
It's "this size" not "at most this size".
 
@TomalakGeretkal Yes.
 
Which allows for "binary" input, but is still arguably a bit shite
 
Perhaps a good solution for that question is to write a "c-string iterator" and use the (iterator, iterator) constructor of std::string.
Somehow the end iterator could check for \0 in addition to making sure that no more than n chars are iterated over.
 
4:23 PM
heh
 
5:18 PM
hey guys
if delete takes a void*, does that mean that I can do like
delete (reinterpret_cast<void*>(new T());
assuming that I don't need T's destructor calling
 
@DeadMG no, delete does not take void*
 
No. You can't pass a void pointer to delete.
 
dang
I wanted to write a function that would deallocate on the correct heap (writing a program intended to be used as a dll)
 
2
Q: C++ delete static_cast<void*>(pointer) behavior

aaasuppose the code does the following: T *pointer = new T(); delete static_cast<void*>(pointer); what is result? Undefined, memory leak, memory is deleted? Thanks

 
call the destructor in the user side code, then delete the void* on the dll side
 
5:20 PM
don't reinterpret_cast pointers.
@DeadMG ew
 
what?
otherwise I'm gonna have to write a dozen deallocation functions
or have some kind of single-root type heirarchy
 
Yea god forbid you write sensible code!
 
quiet you
what I could do is
 
You can static_cast pointers fine; reinterpret_cast -ing them is almost always wrong. You use reinterpret_cast on [the] objects [that you're pointing to] themselves, if you really really really must, but never on the pointer. Pointers of different types may have different sizes.
 
new new(sizeof(T)) T()
 
5:21 PM
And as the guys said, delete will expect to call the right destructor; no way around that.
 
Are you linking against the dynamic runtime?
 
new new?
 
no, I'm linking it statically right now but could choose dynamic later
well
since I return unique_ptr's as a part of my interface, it's not like I can pretend that it's not completely compiler-specific
so why not just link against the CRT dynamically
 
I think it's time for malloc.
 
@DeadMG Why are you not dynamically linking against the runtime?
It's almost universally recommended to avoid statically linking against the runtime.
 
5:24 PM
because I want to redistribute my app in a single file
because it's currently under development and I don't want to ask a friend to view it, oh wait, now you need to also download a dozen DLLs
 
Well, if it's in a single file then you aren't dynamically linking anything? In that case, you only have one heap...
 
it's a single file right now
but I was thinking about separating them out
hence the original question
 
I often prefer statically linking the runtime too, tbh.
I get the benefits of modularity, but I like to just send someone my app and have it work without them frakking around on their machine with dependencies and whatnot.
Also, it's the free store, people.
 
heap
 
The CRT calls it the heap.
I think @sbi is silently watching the chat :-D
 
5:35 PM
@DeadMG Would consistently calling placement new work for your problem?
 
The CRT is not part of C++.
 
might do
 
Or, if it is, it does not call it the heap.
 
pretty sure that it is Tomalak
C++ Standard includes several CRT headers
 
sbi
@JamesMcNellis Actually I'm working, but once in a while I'm looking if something interesting has happened. :) How did you know?
 
5:36 PM
The term "heap" for it is definitely not mandated by any part of the C++ Standard.
 
<cstdlib>, <cstdio>
 
@sbi Ha. You commented on the question I linked to above.
 
It sounds as if you want to use a different allocator (not the C++ allocator), but have C++ construct into the memory regions that are allocated by that allocator.
 
And if it turns out that parts of the inherited C headers mention it, I'll argue that they're legacy and don't count. :)
 
sbi
@JamesMcNellis Ah, yeah, that would give me away! :)
 
sbi
I hadn't noted that it was an old question.
 
I'll just link against the crt dynamically
 
I have been thinking that my auto_malloc_ptr template could be generalized to allow any malloc-like allocator interface to be used. It could be adapted to HeapAlloc/HeapFree, for example.
 
@DeadMG It's much easier that way, and Visual C++ 2010 has much better support for distributing the runtime DLLs alongside your executable so that you don't have to use the SxS. (I think the recommended approach is still to use SxS, but I don't really know; I've never released a program built in VC10)
 
there's a load of stuff in the D3D SDK about app distribution
not that I ever read it
@Daniel: As long as you allow SCARY iteration
heh
man, that makes me laugh every time
 
5:42 PM
@DeadMG ??
 
ignore me
static_assert(std::is_same<std::set<int, std::greater<int>>::iterator, std::set<int, std::less<int>>::iterator>::value, "SCARY iteration disabled!");
I'm a child
 
reinterpret_cast<T*>(u) with u being a pointer won't actually try to read sizeof(u) bytes from &u
the majority of people think it will
 
of course it won't
all you did was cast it
 
or at least it looks like it
 
I'm glad u pointed that out!
 
5:51 PM
lol
 
har har
 
i tried to edit it. what a shame, too late!
but i forgot you could just follow xD
 
sbi
@Daniel Is there any reason you still have stackoverflow.com/questions/4681405/c-equivalent-of-strncpy/… down-voted?
 
why is std::string's assignment operator the equivalent of strncpy?
 
because it copies a string
you just don't have to deal with buffer sizes and null terminators
 
5:54 PM
it won't really, but its very nature
well, i would say the equivalent of strncpy would be
... tada ... strncpy!
HAHA
 
no
 
Nah
std::strncpy!
 
strncpy is used for copying of fixed length "strings" that don't necessarily need to be null terminated.
 
I'd say
the std::string begin/end constructor
 
Is anyone familiar with TR2?
 
5:56 PM
std::string(str, &str[length]);
there's a TR2?
 
I thought TR2 would be out a few years after C++0x
 
@jweyrich Heh. I like his optimism:
> The final technical vote is scheduled for March 26, 2011. I see no reason for that to fail.
4
 
@JohannesSchaublitb eh?
you mean the cast operation doesn't do any reading of the [pointer] object
similar to how sizeof doesn't evaluate its parameter?
 

« first day (89 days earlier)      last day (4860 days later) »