« first day (399 days earlier)      last day (4534 days later) » 

3:21 PM
@AlfPSteinbach The last question I saw you ask was about the swap semantics for ios_base or something. It was something I would have probably never noticed in the standard, but the question and answers were really instructive. Most of the questions on here seem like garbage, and most answers seem trivial. It's kind of depressing really, but there is some really good stuff on SO.
 
@StackedCrooked if sound and video goes out of sync, lemme know
 
How do you usually do it when you implement a Template class?
Do you put everything in a .h file, or definition in the .h and the declaration in the .cpp file and the include the cpp file into the h file, or include the h file into the cpp and instead include the cpp where the template class should be used?
 
never ever include cpp files, that's just asking for trouble
 
Yeah the last one is a bit strange, I was just wondering what's your general implementation
 
why would you create a cpp file at all? What would you put in it?
 
3:29 PM
To only see the declaration in the h-file and then the actual definition in the cpp file
Just as regular classes
 
but I have to include the definition in order to instantiate the template
 
yes but like this:
.h file
{
declaration

#include .cpp
}
then you could have the definition in a separate file
 
but I still hace to include both in order to use the template
you could spread it across 18 different files too
 
I wonder if this problem can be solved with an abstract factory and a wrapper.
 
you're just creating more work for users
 
3:30 PM
but you include the .cpp inside the .h file
 
so what have you gained?
you'll confuse your build system because you now have a .cpp file which should not be compiled
 
Abstraction
as with regular classes
 
abstraction?
how does it provide abstraction?
 
Yeah you don't have to see how the functions are implemented
 
In my latest project I've gone to .h for "simple" headers, .hpp for headers with deep implementation, and one .cpp for the "driver."
 
3:32 PM
@ManofOneWay That doesn't make sense. You don't have any functions.
All you have are templates.
 
@ManofOneWay but I still see it! I include a file which includes the definition
you haven't hidden anything
and even if you had, it'd still be of dubious value
 
I think it looks a bit cleaner to have the declarations apart from the definitions
 
C#, Java and practically any other language in the world tells you to define your classes inline, because it's just not very intuitive to have it split into two halves like we traditionally do in C++
in C++, we do it to work around a broken compilation model
that doesn't mean it's a wonderful idea
 
26
Q: Why can templates only be implemented in the header file?

MainIDQuote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this?

 
for regular classes, that is. There are other mechanisms for providing abstraction
 
3:34 PM
There are numerous extensions recognized as C++ header files. Just choose one, but .cpp will confuse everyone.
 
So in the accepted answer he shows a common (?) way of separating the declarations from the definitions
That's why I'm asking how you guys do it =)
 
templates have to be visible to use them. So wherever you put them, they have to be included. You can put them in one, two or fourteen different files, it makes no difference, I have to include them all in order to use the template
@ManofOneWay I just write my class templates inline
 
Sure, I'm thinking more about the fact that I don't have to see the definition, I can just see what declarations there are
Therefore it might be cleaner and easier to grasp
 
The whole "include a separate implementation file" thing is used sometimes for really large class templates
but for the most part, I just don't see the point, and I don't see it done very often
 
Okey =)
 
3:37 PM
@ManofOneWay But the class still contain a bunch of other implementation details. The header still shows all the private members and base classes and whatnot. It's not a good abstraction, and it was never intended to be an abstraction
headers are a workaround for a broken compilation model, nothing more. They're certainly not a tool for providing abstraction
 
Abstraction is the wrong word then, I was more thinking of the readability.
Anyway, I will not use it then =)
 
Fact: The sensation of bugs crawling on your skin is called Formication. (sci-ence.org/neosporin)
 
@ManofOneWay well, I personally think it's more readable if I only have to look in one place to see the code I'm using
 
about how the "min" template is broken
too bad neither he nor Dave gave an elaborated explanation for why it fails
Dave just said "Unfortunately, as Daveed points out in his comment, it’s even worse than that: because decltype is operating on an lvalue, the version above returns a reference to a temporary! So we’d actually need this:"
 
3:40 PM
the concepts vs polymorphic lambdas really really bugs me
 
but that's not quite true. it doesn't return a reference to a temporary, and it doesn't explain what's going on
 
@JohannesSchaublitb What's broken?
 
it's a pretty clear example of how broken concepts are, if they make it impossible to implement language features that we want
 
i wonder whether I should make a SO question
since this is an interesting flaw of the min template presented
 
as long as concepts can't describe the C++ code we want to use, they're obviously not going to fly
 
3:41 PM
@jalf IMO that's not true
it's lambdas that are at fault
 
how so?
 
they need to be specified to work with concepts
@KerrekSB the problem is that under common situations he returns a reference to a parameter of the min template. not to a temporary
 
I thought concepts were strictly optional. What needs to be made conceptual?
 
@jalf because they were totally unconstrained and syntax wasn't there to add constraints
 
@JohannesSchaublitb so?
 
3:43 PM
which is flawed
 
@JohannesSchaublitb only if you look at it back to front, and start with the assumption that "we have to have concepts, regardless of how they constrain us"
 
so why is the good type checking at fault, when the non-type checked lambdas cannot work with the good type checking?
 
The objective with concepts was to better describe the code we're writing, so the compiler can better understand what we're doing, and provide better error messages. It was never meant to constrain us or block us from doing useful things. So any time concepts constrain us from doing useful things, they've failed
@JohannesSchaublitb I don't follow. How would polymorphic lambas be non-type checked?
 
@JohannesSchaublitb Doesn't the standard require this? 25.4.7?
 
yes [](a, b) { return a + b; } is invalid
because a has no operator+
and is not copyable
 
3:45 PM
they correspond basically to a class with a templated operator(). I don't see why that wouldn't be type checked
 
@jalf well the above lambda adds two values that it doesn't know whether they have operator+
if it would type check at definition time, it would give an error
 
why couldn't it be implemented as `struct f { template <typename T, typename U> auto operator()(T a, U b) -> decltype(a+b) { return a+b; } };?
then the type checking is done when the operator() is instantiated
as with any other template
 
@JohannesSchaublitb you already memorized whole c++ standard pdf ? i think soon you'll come up with c+++ or might be ---z :)
 
@jalf but type checking at definition type will be completely gone
 
cpx
hmm, i'm still wondering if i c++ guarantees that a pointer to one past the end of object or array is valid?
 
3:50 PM
which is bad
and fixed by one corner stone of the concepts proposal
 
Ha, I just found an old answer from @Johannes that has currently 24 upvotes. Badge coming in 3, 2, 1...
 
@FredOverflow ohh i'm soo glad!
 
@JohannesSchaublitb so? How is that different from regular templates? Why is it worse?
 
thanks dude
@jalf it is as bad as "regular templates"
all templates currently being written suffer from the problem of missing type checking
 
@cpx Yes, it is and has always been guaranteed. It is called the "one past the end" pointer. Beware though, you are not allowed to dereference it.
 
3:52 PM
it is equivalent of writing a c++ program where at compile time no type checking occurs, and where at runtime your program crashes and you are stumped
 
@JohannesSchaublitb And, as shown by the polymorphic lambdas case, that makes them useful
a proposal that removes something useful just in order to provide better error messages seems pretty flawed to me
 
@jalf IMO that makes them more useless
 
@JohannesSchaublitb I don't see why
 
templated lambdas are useful just as much as templated regular functions
 
the type checking occurs later during the compilation, and yields a less readable error message, and so what? It still gets type checked, and it allows me to write more expressive and concise code
It's not much of an exaggeration to say that that's why I use C++ and not C#
 
3:57 PM
What we really need is a better error formatter… a dedicated browser, really.
 
that's equivalent to saying "the checking for errors occurs later during execution of the program and yields a less reliable error message, and so what?"
 
anyone here ever needed to wrote their own buffer class for streams?
 
@user411102 yes
 
@jalf you will miss much error checking at instantiation time
 
@JohannesSchaublitb And so what? Python manages pretty well with errors caught at runtime, doesn't it?
 
3:58 PM
@user411102 Not really needed, and not from scratch, but I have some experience with that.
 
nothing necessarily checks that the iterator you are using is really a forward iterator. perhaps it is actually an input iterator and you just ended up in UB land?
 
moreover, no, I don't think that's equivalent at all. I think there's a world of difference between errors caught late during compilation, and errors caught at runtime
 
cpx
@FredOverflow I believe so, can you point me to the standard :)?
 
@AlfPSteinbach what was need to write one?
 
@JohannesSchaublitb So? Nothing checks that I don't write past the end of an array either. UB isn't some strange new thing.
 
4:00 PM
@user411102 You subclass basic_streambuf and override the needed virtual methods. Which methods and how, refer to the Standard.
 
@cpx sure things, just a few seconds...
 
class DirectOutputBuffer
        : public basic_streambuf< wchar_t >
    {
    protected:
        virtual streamsize xsputn( wchar_t const* const s, streamsize const n )
        {
            static HANDLE const     outputHandle    = GetStdHandle( STD_OUTPUT_HANDLE );

            DWORD nCharactersWritten    = 0;
            bool writeSucceeded         = !!WriteConsole(
                outputHandle, s, static_cast< DWORD >( n ), &nCharactersWritten, 0
                );
            return (writeSucceeded? static_cast< streamsize >( nCharactersWritten ) : 0);
 
@jalf yes. checking that you don't write past the end of an array is not checked anywhere
 
@Potatoswatter that i know but ..
 
equivalently, you are not required to write semantic concepts for each and every requirement
 
4:01 PM
Markdown - Alf: 1:0
 
even with concepts, some type checks may be missing. sometimes for the worse and sometimes for the better
 
I'd have nothing against [](a, b) { return a + b; }
 
Well certainly they can only help if used properly. Already we have iterator tags which can be checked.
 
@JohannesSchaublitb but if concepts make it impossible to implement polymorphic lambdas, then that is an extremely high price to pay for a bit more error checking
 
@cpx 5.7 §5
 
4:03 PM
And only so much can go wrong in a one-liner function, knock on wood.
 
I'd rather polymorphic lambdas than concepts
 
@jalf they make it not impossible to implement polymorphic lambas
 
I actually don't have much of a problem with the current error message system
 
If I wanted everything to be checked for me, I'd use Java. I use C++ because I'm willing to give up some error checking in order to write more expressive code
 
they just make it harder to specify and implement lambdas
 
4:04 PM
Polymorphic sounds like virtual. Can't we call them deduced lambdas or something?
 
syntax and rules need to be invented
and parsers and type checkers be written
 
@Potatoswatter No, that's just you not knowing what polymorphic really means :D
 
@JohannesSchaublitb well, it prevented polymorphic lambdas from going in C++11 ;)
 
4
Q: Is accessing data outside an array incorrect if you don't use it?

static_rttiIn an algorithm I'm writing, I can have the following (simplified of course) int a[3] = {1,2,3}; int b = a[3]; when the index used to fill b overflows, I never use the value of b. Is the code still incorrect? Do I have to make an explicit boundary check?

7
Q: Is storing an invalid pointer automatically undefined behavior?

Channel72Obviously, dereferencing an invalid pointer causes undefined behavior. But what about simply storing an invalid memory address in a pointer variable? Consider the following code: const char* str = "abcdef"; const char* begin = str; if (begin - 1 < str) { /* ... do something ... */ } The e...

3
Q: C++ size_t or ptrdiff_t

gsieranskiIf you have the following code where p is a pointer: p = p + strlen(p) + size_t(1); Since strlen() and size_t are both size_t, should I cast the code to ptrdiff_t ? p = p + (ptrdiff_t)(strlen(p) + size_t(1)); If so why? Thanks, Greg

10
Q: Why is comparing against "end()" iterator legal?

sharptoothAccording to C++ standard (3.7.3.2/4) using (not only dereferencing, but also copying, casting, whatever else) an invalid pointer is undefined behavior (in case of doubt also see this question). Now the typical code to traverse an STL containter looks like this: std::vector<int> toTraverse...

@cpx Those questions all deal with "one past the end" pointers.
@Potatoswatter virtual is just one kind of polymorphism, namely "subtype polymorphism".
 
in fact int a[2]; a + 2; is already UB
as was reported recently on usenet
you are only allowed to say a + 1 + 1. I believe it was Richard who found that hole in the spec
 
4:08 PM
@JohannesSchaublitb brilliant
 
@FredOverflow In the Standard, polymorphism is used to refer to subtype polymorphism. dynamic_cast does something "if the object has polymorphic type", etc. So I think it's reasonable to restrict the meaning from the pure computer science term.
 
@Potatoswatter Not really. Templates are often referred to as "type polymorphism", which is indeed exactly what they are.
 
@Potatoswatter but static polymorphism is applicable to C++ too, even if the standard never uses the term
 
or generally a + (N-1) + 1
 
@JohannesSchaublitb What? Why?
 
4:10 PM
Still, there are plenty of terms to choose from. "Templated lambdas," "deduced lambdas," would simply be more specific.
 
deduced lambdas would imply that the lambda itself is deduced, which it isn't
 
@Potatoswatter but "polymorphic lambda" is the established term for this
 
templated lambda isn't very accurate either, you can't pass it explicit arguments, for example
polymorphic lambda is the best approximation and well accepted
 
Bah, I say. Humbug.
 
@FredOverflow because the rule is "if P points to the last element, P+1 points to after the last element.". There is no rule "if P points to array element N of an array of M elements, P + (M - N) + 1 points to after the last element"
 
4:12 PM
uh
addition is left associative
so actually, yes, that is exactly implied
(P + (M - N)) yields a P which points to the last element, therefore + 1 on the end of that does yield the last element
 
@DeadMG The inability to pass explicit arguments results from the lack of a suitable template-name more than anything else. It's certainly possible to define a canonical order of implicit template parameters.
 
@Potatoswatter But you'd be using the term "template" to refer to something that's not a full template.
 
@DeadMG I put a "d" at the end. Template*d* lambdas, not template lambdas.
 
nobody cares why you can't pass explicit template arguments unless you want to try to fix it
 
P + 1 + 1 is different from P + 2
 
4:14 PM
I capitulate, anyway.
 
the fact is, they aren't templates on par with class or function, and don't really deserve the name
 
if addition where right associative, it would be the same
@DeadMG so your complaint is invalid.
 
uh, no
the only difference between P + 1 and P + (M - N) + 1 is that you obtained the pointer to the last element in an intermediate expression
 
@JohannesSchaublitb So you're saying that N + P would be OK if the standard said 1 + P?
 
i think i was wrong hold on
 
4:16 PM
and in addition
 
There is no rule "if P points to array element N of an array of M elements, P + X points to after the last element if X is M-N+1"
 
if P + 1 + 1 is well defined, then by extension, P + 2 is well defined, as addition is commutative and associative
 
nono
addition on pointers is not associative
 
and it is perfectly legitimate to say (P + 1) + 1 = P + (1 + 1) = P + 2
 
4:18 PM
@JohannesSchaublitb But addition among one pointer and several integers is commutative, no?
 
i was wrong with the above and i corrected above, but now you are wrong with saying that addition on pointers is associative.
@Potatoswatter yes i think it is commutative but not associative
 
it is associative
 
int a[1]; (a + 100000) - 99999; is UB but a + (100000 - 99999) is not
 
there is no way that P + 1 + 1 could possibly yield a different result to P + 2
 
@DeadMG yes there is
 
4:20 PM
uh, no
 
as i showed, one may be UB but the other not
 
also no
 
Okay I think I get it, the standard says that you can get one past the end by adding 1 to the last "normal" pointer.
 
what you showed was the creation of a completely invalid pointer
and then put it back
that's not what's under discussion here
the discussion is between P + 2 and P + 1 + 1
no minus
 
@JohannesSchaublitb Oops, you could add integral overflow from left-associativity in ( i + j ) + p, so no, and p + i + j is therefore preferable.
 
4:22 PM
and in addition
 
@DeadMG i don't follow you
 
pointer addition with integers must be associative
because else, you could have something like
 
i have given an example that for pointers the addition is not associative
why then discuss P + 2 and P + 1 + 1 ?
 
because
in your example, the array was way under the sizes involved
in the original example, it is not
 
the rules in the spec for past the end production assume associativity. since that is not, there is no rule that defines what P+2 yields
@DeadMG yes in the original example it is too
int a[2]; a + 2; the array is under the size of 3 - the smallest size that a+2 could be well defined for
 
4:23 PM
no, no, I give up
 
Can we all agree that int a[2]; a + 1000 - 998; is UB? So associativity breaks.
 
you are simply ignoring my valid points
 
you can't read
and don't have any valid points
 
lol
it doesn't matter whether you go 10000 beyond the end or 1 beyond the end
beyond is beyond
 
it's UB to even create a pointer that's 10000 beyond the end
not true for 1 beyond the end
 
4:25 PM
But anyway, I thought this was all defined in terms of addresses, and one-past-end is fine no matter how you get there. It's a property of the pointer value, not the pointer expression.
 
thus, it completely and absolutely matters
 
@DeadMG no, the matter at hand is how you create the 1 beyond end pointer
the way a + 2 is invalid, a + 1 + 1 is valid
 
for int a[2]; then a + 2 is valid exactly as much as a + 1 + 1
but
 
because only the latter is defined to produce a defined value
for the former, no behavior is defined
 
(a + 10000) - 9999 is not as valid as a + 1
 
4:26 PM
so the former is just as invalid as a + 1000
 
so your earlier example was pure horse shit
now
 
@DeadMG you still need to prove that "then a + 2 is valid exactly as much as a + 1 + 1" you have not given such proof
lol
 
a + 2 must be exactly equal to a + 1 + 1, because the behaviour of both are clearly defined
 
no, the behavior of a + 2 is not defined anywhere
 
move one element, and then move one element again, is clearly exactly equal to move two elements
 
4:28 PM
only if "move" is associative
as we found out earlier, it is not
 
no
it completely is, and indeed, must be
 
yes a + 2 must be defined. but the spec does not define it.
which is why it is defective
you keep refusing this fact lol
 
the fact that something is a logical consequence of the spec is exactly equal to the fact that the spec defines it
if you implement something according to the spec, then your system must have this property
 
@DeadMG you need to prove that a + 2 is a "logical consequence of the spec". i see no such thing
 
a + 2 is a logical consequence of the spec
do you think that a + 1 - 1 is not defined to be equal to a?
 
4:31 PM
that's your assertement, but it is false.
 
it is completely true
 
@DeadMG yes I think so
for example
int *x = new int[0]; then x + 1 - 1 is UB, but x is not
 
§5.7/5: "If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow." — so even if the behavior is undefined, the obvious failure mode is excluded.
 
uh
for a start, it's not UB because you only went 1 past the end
 
i went two past the end
 
4:32 PM
but even if you went more
 
x is already past the end
lol
 
then it's still not UB because in the example it was clearly within the bounds
 
yes if a is int a[2] then it is not ub
 
right
 
and then a + 1 - 1 has the same effect as a + 0
 
4:34 PM
but a + 1 - 1 is defined to equal a
if pointer and integer addition is not associative, how come?
 
that is not true
example: extern struct A a[2]; then a is valid, but a + 1 - 1 is invalid
 
@DeadMG: how is that gin coming along?
 
and that example is valid for any other a and any other series of additions or subtractions that produce a pointer in the range a to one past the end
 
@DeadMG you are asking "1/1/1 is defined to be equivalent to 1. if division is not associative, how come?"
 
no, because you only ever did one operation, so how could associativity possibly come into it?
 
4:37 PM
updated
 
that's still not representative, because I also said that it was valid for any a and any other series of additions
 
ur lost in math rules dude xD
 
not just a + 1 - 1
 
@DeadMG as we showed it is not valid "for any a and any series of addition".
 
you didn't show anything
 
4:39 PM
a + 100 - 99 is invalid for int a[2]
 
Is there a common word that generalizes "increment" and "decrement"?
"crement"?
 
lern2read
> that produce a pointer in the range a to one past the end
 
and int *x = new int[0] then x + 1 - 1 is UB but x is not
likewise int *x = 0; x + 1 - 1 is UB but x is not
aaahaha
 
> that produce a pointer in the range a to one past the end
 
@KerrekSB Crement would be cromulent but I don't think so. "Advance" maybe.
 
4:40 PM
@DeadMG i have no idea what you are saying aaahaha
 
@KerrekSB Excrement
@JohannesSchaublitb I've noticed that.
 
3 hours ago, by DeadMG
sleeeeeeeeeeeeeeeeeeeeepy
 
@DeadMG you may enjoy telling Richard about your insights groups.google.com/group/comp.std.c++/browse_thread/thread/…
 
for any int a[5]; then a + 3 is defined to point to the same element as a + 1 + 1 + 1
and, in addition, the compiler cannot treat them differently for one-past-the-end, because in the case of dynamic arrays or just arrays not allocated in that TU, it cannot know when it's at the end
 
4:43 PM
"[expr.add]/5 also only allows a one-past-the-end pointer to be formed by
adding 1 to a pointer"
this observation of Richard is totally correct, but you keep refusing it
 
that's because it's wrong
 
Johannes has a linguistic point, but some things you're just better off not knowing.
For what it's worth, as I said, the behavior is not defined but overflow is specifically prohibited.
 
it is like this. the spec says "if a number X is 5, then X+1 makes you win".then you come and claim "my X is 10. BUT! X-4. I Win!!"
 
the compiler can't know when it's at the end of an array
 
4:46 PM
Eh, strike that actually, since P+N doesn't really point one past the end the last sentence doesn't apply.
 
so addition over it is as defined as any other element
 
@Potatoswatter if you hit UB, any rules are inactive
the compiler doesn't have to care about whether or not overflow may occur
 
@JohannesSchaublitb But we're not talking about explicit UB, we're talking about a hole in the standard where they forgot to define anything.
 
Hey, what do you think about contributing to my compiler project sometime, if it starts going somewhere?
The framework is teh neat-o.
 
4:50 PM
xD
@Potatoswatter im currently in the phase of defining the syntax of my lexical anaylzer generator/parser generator
it should be able to generate GLR parsers
 
@JohannesSchaublitb Cool! I know next to nothing about formal grammars.
 
when it hits a conflict, it copies the state of the parser and lexer, and does work in parallel, until it hits a point where all other paths die except one
that's the plan. the user declares all the state of the lexer and parser, so that lexical tie in works automagical
 
@JohannesSchaublitb That's pretty much the architecture I've been planning on to tackle C++.
 
@Potatoswatter im on teh noob side on grammars too xD
 
However, I'd minimize the state that gets copied.
 
4:53 PM
yeah. perhaps even applying reference counting
 
you don't need that
 
Reference counting is not performant. GC is the way to go.
 
or that
you don't need any heap-based structures for pure parsing
and as for the state of the parser, you can copy a single iterator
 
I'm procrastinating from generalizing my GC-ish string class, adding preprocessor pragmas and optimizations and such.
 
oh btw, @Potatoswatter
you could always help me with my compiler project :P
 
4:56 PM
Heh, if you want to give my framework a spin, everyone would benefit :v)
 
don't require a framework
 
You don't want modularity, or you have your own?
 
uh
modularity is class Parser { ... };
 
what other modularity could I possibly want?
 
4:57 PM
But the lexer is separate from the parser…
 
yes
and?
 
and the parser needs to plug into some kind of output processing…
 
@Potatoswatter i thought yours is just a PP ?
are there some compiler bits too?
 
also and?
I have both of those things
 
@JohannesSchaublitb Not yet. I started writing phases 5-6, but that required too much target machine description, so I went back for polishing and adding features.
 
4:59 PM
ohh i see
i plan to make the parser generator so that a C++ parser can be put into its "samples/" directory xD
 
Like this custom string class, which is the groundwork for the GC/streaming architecture that should handle everything else.
 

« first day (399 days earlier)      last day (4534 days later) »