« first day (332 days earlier)      last day (4614 days later) » 

11:00 AM
Both designs work, and Boost.Wave does it the other way around, but they really work out similarly in the end.
It's a consequence of using output iterators vs input iterators. I think output is more elegant.
 
Oh, it's push vs pull. I get it.
 
Yep.
Usually when you're compiling something, you know more about what you're starting with than what you want to have. So it makes more sense to throw a bunch of data at the compiler, than to ask the user to request exactly as much data as there is, or worry about an end-of-iterator value…
I'm finding this an interesting problem, since I tried to design a compiler framework back in high school. So it's a bit of perspective on my problem-solving skill development.
It helps to have a language which allows you to arbitrarily specify a member object and perfectly forward its constructor arguments, too ;v)
 
template<typename T> struct no_boilerplate_please: private T { using T::T; };
 
What's that?
 
Yeah, that bugs the hell out of me.
 
user53670
11:10 AM
Does anyone know how to use regex in python?
 
@RMartinhoFernandes Huh? What's what?
 
You should just be able to declare typename inherited_member; and be through with it.
 
@LucDanton This.
 
Private inheritance and inherited constructors.
no_boilerplace_please(args...); will be valid iff T(args...) is, and constructs T with those args.
 
11:13 AM
Oh, constructors. So is that example valid or not?
 
template<typename T>
class now_with_boilerplate {
public:
    template<typename... U>
    explicit
    now_with_boilerplate(U&&... u)
        : member(std::forward<U>(u))
    {}
private:
    T member;
};
 
Neat. I didn't know that. Is this inherited constructor thing C++11?
 
@RMartinhoFernandes Yes, not implemented so far.
 
Does it actually do anything that boilerplate perfect forwarding doesn't?
 
@Potatoswatter I assume so, but I haven't checked. Usually the examples involve public inheritance.
@Potatoswatter I assume it handles those cases that perfect forwarding doesn't.
now_with_boilerplate(an_overloaded_function_name); won't ever compile for instance.
Heh I actually forgot what other things perfect forwarding can't handle, let me dig them up.
 
11:17 AM
It's almost-but-not-quite-perfect-forwarding.
 
Since I'm always passing overloaded function names as arguments… LOL
 
Forwarding 0 to a constructor accepting a pointer type (hence why we have std::nullptr_t.
And list initialization is a big no-no
 
Ah, there's a useful case.
 
now_with_boilerplate<std::pair<int, double>> { 0, 0. }; // nope
Haha getting more esoteric.
Bitfields are problematic as well it appears.
 
Bitfields don't exist outside of structs. You can't pass a bitfield in the first place. ??
 
11:20 AM
struct { int bitfield : 1 } b; f(b.bitfield);?
 
void f(int); will accept the bitfield lvalue using lvalue to rvalue conversion.
However, a perfect reference won't bind.
 
That's taking the rvalue of the bitfield, which is simply int.
 
@Potatoswatter And the perfect forwarding wrapper can't deal with the bitfield.
 
I'm surprised if passing a bitfield name to perfect forwarding doesn't pass an rvalue, i.e. bind to rvalue reference.
 
> error: cannot bind bitfield 'x.X::bitfield' to 'int&'
The bitfield is an lvalue so I'm not sure how and where it should be special cased to enable the conversion.
Heh, probably next to functions?
— If the initializer expression
— is an xvalue, class prvalue, array prvalue or function lvalue and “cv1 T1” is reference-
compatible with “cv2 T2”, or
— has a class type [...]
I guess 'bitfield lvalue' could be slipped in there
Also, notice how array prvalues are part of the languages!
 
11:26 AM
You can't bind a reference to a bitfield, for good reasons.
 
@Potatoswatter Latest quote is about rvalue refs.
 
I would expect lvalue-to-rvalue conversion to apply, and continue with the rvalue. But now i see how that's messy, too.
Yeah. I wouldn't want to bind an rvalue ref directly to the bitfield object, but to a temporary holding its value.
 
References to arrays of unknown bounds can't be perfectly forwarded to a function taking T*.
 
@LucDanton what's a bitfield?
 
Anyway, that's from c.s.c++ early 2010.
 
11:30 AM
is that like a packed struct?
 
@TonyTheLion struct X { int bitfield: 4; }; has a bitfield of size 4 bits.
 
It's a struct member with a size in bits.
 
@LucDanton oh I see...
 
@TonyTheLion Close to that notion, yes.
 
@LucDanton It's possible to have a reference to array of unknown bound? ideone.com/kFiLx ideone.com/Os4xQ
 
11:32 AM
@Potatoswatter ideone.com/ycAzs
 
Ohhh, like extern int a[]; f( a );
 
The fact that you can't put it in a parameter list is probably tied to the specific perfect forwarding failure.
wrapper(ref); won't work.
 
So, there's a type that you can't use as a parameter but can as a local variable?
 
Yeah, arrays. (Assuming parameter here, not local variable.)
 
Guess I'm kinda on the fence on that. It would bug me if I wanted that feature (which is very possible), but the language is definitely cleaner without that decay mode.
 
11:35 AM
Duh.
 
Als
ohla
 
Hi.
I'm glad that you weren't serious with that "no more answers" thing :)
I wonder if copy ellision applies to catch statements.
 
It applies to throw. It can't apply to catch because then rethrow would expose a modified object.
(You should always catch a reference, anyway, since normally you do want to rethrow the modified object.)
 
11:51 AM
@Potatoswatter Doesn't that last part depend on the copy constructor though? Copy elision doesn't have to modify the source.
 
@Potatoswatter Yes, I know, it's not my code.
0
Q: Catch by reference when exception variable is not defined

Christopher HowlinWhen catching an exception the standard guidance is to throw by value, catch by reference. As I understand it, this is for two reasons: If the exception was thrown due to an out of memory exception, we won't call a copy constructor which could potentially terminate the program. If the exception...

 
Sounds more like the as-if rule than 'true' copy elision come to think of it.
 
@LucDanton I mean, if you catch by value and then modify the object, you need to be modifying a copy.
 
Do you think I should move my comment to a real Question?
 
@Potatoswatter Oh yeah, definitive as-if rule there, thanks for the explanation.
 
11:53 AM
@LucDanton Yes, but copy elision is an exception to the as-if rule.
 
What is copy ellision?
 
@RMartinhoFernandes Different speculative as-if here.
 
Think I have heard of it before..not sure
Oh..an optimisation thing yeah?
The compiler may decide not to copy temp objects..
 
@RMartinhoFernandes That is to say, 'as-if' as applied to copy elision itself ;) although your comment/question stands actually.
 
Read the question now :v) … that's a good question. Do any compilers optimize local catch/throw at all, though?
 
11:58 AM
Well, catch by value should be just as safe as the copy constructor. And since throwing uses copy-init, the copy constructor of an exception better be quite safe...
 
It would be a very special case, since an object thrown from the same function would be subject to copy elision, but an object from up the call stack would seem to need to go through the whole rigmarole.
 
Since in fact the catch clause will be further down than the throw statement, the throw should really be the risky business, not the catch.
 
Yeah, even all within the same function you can still restore the original by rethrow. So I think copy elision (by the as-if rule) would be a very hard optimization to apply.
You have to make sure that no functions called within the catch block can rethrow… which is essentially the same as everything being noexcept.
 
Haha
No idea how conforming GCC is but
 
12:01 PM
the copy constructor exception escapes the catch, no std::terminate here.
It's weird because for functions parameter copying is caller side.
 
std::terminate results from exceptions after the exception object is constructed.
An exception during exception object construction is just a normal exception.
 
What's normal about an exception escaping just in between a try clause and a catch clause?
 
What do you mean, escaping? An exception is thrown but ignored?
 
Well, I'm going to enclose with a further try-catch block then.
Mmmh I'm confused right now.
 
ideone?
 
12:08 PM
ideone.com/nMnQ8 but won't load for me right now :|
 
terminate called after throwing an instance of 'int'
 
So that's the message when an exception escapes main.
But I can't catch it, a telltale sign of UB.
Mmh. Quite not sure this is accurate.
terminate called without an active exception is the message when calling std::terminate 'by hand'.
And that's what I get when calling it in the copy constructor instead of throwing.
Any idea?
 
terminate is getting called because of a throw during stack unwinding… since the copy constructor in question isn't constructing the exception object, what I said before doesn't apply.
The throw apparently has set the exception object to int before terminate gets called, so you see that error message.
Probably just a QOI issue in the terminate error message printer, but I don't think it's UB.
 
ideone.com/IukY5 substantiates that hypothesis.
 
12:21 PM
So, copy elision for rvalues only then?
The 'stored' exception seems to be a weird polymorphic rvalue. How peculiar.
I can't find where it's specified how the parameter of a catch clause is initialized.
From what more precisely. The GCC code behaves as if passed an lvalue though.
> The temporary is an lvalue and is used to initialize the variable named in the matching handler (15.3).
No weird rvalue then.
 
12:43 PM
Huh, C++03 allows an inline friend definition of a function template within a class template. I thought that was just C++11. Add that to the list of pathological ADL cases.
 
Speaking of pathological, it seems the consensus on the question is:
- the active exception is a temporary object and an lvalue (?? it's not even an expression)
- it is explicitly mentioned that copy elision is allowed
- the meaning of the program must not change if elision occurs
wat
 
@LucDanton 15.3/16: If the exception-declaration specifies a name, it declares a variable which is copy-initialized (8.5) from the exception object.
 
If there's a name, copy initialization. If there's no name, copy initialization. What am I missing?
 
The exception object is defined as a temporary, because copy elision is defined in terms of temporaries.
 
@Potatoswatter I clarified that what I wanted to know what it was initialized from (which is an lvalue).
@Potatoswatter I'm referring to the text of 15.3/16 (that you've pointed me to) which applies to the object in the handler, not the active exception.
> If the exception-declaration denotes an object type but does not specify a name, a temporary (12.2) is copy-initialized (8.5) from the exception object.
So
Does that mean that there is copy elision elision in C++?
'Elide copy elision if it would otherwise mean changing the semantics of an exception handler!'
 
12:52 PM
It would seem that copy elision doesn't apply here. 15.3/16 says the temporary lives as long as the handler.
 
Why does the lifetime affect copy elision?
 
Copy elision applies to initializing the exception object, which is a temporary, but which the language essentially const_casts for you into an lvalue.
 
@Potatoswatter I'm at the handler level, not the throw level. For the whole of the discussion.
 
The language explicitly says when the temporary is created and destroyed. There's no wiggle room for the as-if rule.
(Outside the as-if rule, this isn't one of the cases explicitly defined to allow copy elision, so it certainly can't be "true" copy elision.)
 
Are we really talking about the same thing?
 
12:55 PM
Talking about eliding the construction (and destruction) of the unnamed object declared in the catch clause.
 
Oh. I didn't consider that. Can it be elided at all then?
 
No. What were you talking about?
 
Ah I see.
The paragraph makes two cases not for the copy initialization, but to make one of the case a temporary.
That would actually help with the other question actually: pass by value can be optimized to nothing assuming non-pathological constructors/destructors.
 
You mean, trivial constructor and destructor.
 
@Potatoswatter Can the active exception be copy elided into the handler parameter, even if rethrowing?
@Potatoswatter QoI, this is specified in terms of side-effects.
 
12:59 PM
Rethrow doesn't copy anything. It reactivates the existing exception object.
 
@Potatoswatter No, you're correct, no such provision here. I'm not sure then.
@Potatoswatter Copy elision would have happened on entering the handler.
i.e. the very first tihng we discussed when the question was opened.
 
But 15.3/16 forbids copy elision for the handler's object… I'm not sure what the question is.
 
It's allowed (with caveats), see the updated answers.
 
Updated answers where? "Catch by reference when exception variable is not defined" hasn't changed.
 
1:11 PM
Interesting. C++11 has expanded a lot on this.
 
It has!
 
They probably should have introduced more subclauses… 32 paragraphs in one clause is not good.
Not sure where it could be broken up though.
 
sbi
Happy Programmer's Day! It's the 256th day of the year. 8 bits FTW. (See, for example, http://ow.ly/6swoR) #DevDay
 
So it's New Year's?
 
sbi
@Potatoswatter 365 != 256
 
1:17 PM
But it's the 0'th day.
 
No, that's tomorrow. '256th' is the ordinal, 255 is the offset.
 
ordinal?
 
(Although I should check which day is today.)
@TonyTheLion 42 is a cardinal number while 42nd is an ordinal number.
'Ordinal' as in 'order', i.e. counting things.
 
oh I see
 
What if you're counting cards?
 
1:20 PM
For instance 2011 in 'Year 2011' is also an ordinal number because it's counting the years.
It's the 2011th year, but it took 2010 years to get to that.
 
Ordinal numbers refer to members of sets. Cardinal numbers express the size of a set.
 
Mmmh I said counting things, but enumerating is possibly a better word for that.
A set of size 3 has a first element, a second element and a third element (Am I using the axiom of choice here?). This is enumerating.
 
Ordinals can refer to members of uncountable sets.
 
1:37 PM
How do you clear a stringstream? Nothing bloody works!
I tried ss.str("") ; and ss.str(std::string()) *nothing!*
I had to make it a local variable :S for it to then work
 
What do you mean, clear?
 
@Potatoswatter reset it, empty the stream
 
To flush whatever values it has away..
What I am doing is creating a new game object from the commandline
 
Perhaps you should try an istringstream or an ostringstream instead of a stringstream.
 
but it kept using the same bloody position
 
1:39 PM
@LewsTherin easiest answer is to just recreate it
 
is istringstream ostringstream not cin and cout ?
@jalf, ok that's what I did..for now I'll leave it
Also, C++ has a way to clear output stream but not input
It's weird how they overlook it :S
 
cin and cout are plain istream and ostream objects.
Their buffers are usually, but not guaranteed to be, filebuf.
 
@LewsTherin ss.str(""); ss.clear();
 
so are cin and cout wrappers around istream and ostream or derived or what?
 
@LucDanton will try it now
 
1:42 PM
@TonyTheLion They are objects of type std::istream and std::ostream.
 
IOstreams just isn't a very well designed library, at least when seen with modern eyes
the sooner you accept that, the happier you'll be ;)
 
namespace std {
    extern istream cin;
    extern ostream cout;
 
@LucDanton so technically, could you get them to write to a file instead of the console?
 
^ 27.4.1
 
@TonyTheLion yep
 
1:44 PM
@LucDanton it works...nice one man :)
 
oh wow
 
@TonyTheLion IIRC you have to restore the old buffer or whatever before the program shutdowns but apart from that caveat the rdbuf member is available to do that.
 
You can also retarget them to go to a std::string, or a network link, or text on a window…
 
That sounds dangerous
 
@LewsTherin That would be a correct assessment :)
 
1:45 PM
@LucDanton yea looking at ostream's members, that's what I thought
 
Not a practice I recommend. But some folks do it.
 
oh, what's dangerous about it?
 
It's kind of "hijacking" a standard global variable for your own purpose. It's greedy.
Similar to using a thread-unsafe API on the assumption that nobody else will.
 
Like strtok. Except less insane.
 
So, don't defy the expectations of everyone else.
 
1:46 PM
oh I see, so better of creating a new ostream object if you wish to output elsewhere
 
Just declare your own damn safe global.
 
Why is strtok bad?
 
cause it's C
lol
 
I used it before changing to stringstream
 
@TonyTheLion No, not really.
 
1:47 PM
and I don't think it's type safe
 
The C is not the bad part of it.
Nor the type safety.
 
It's the crazy side-effects mostly.
 
It's like close( STDIN_FILENO ); open( my_input, O_RDONLY ).
 
1:48 PM
Which are?
 
yesterday, by R. Martinho Fernandes
I completely forgot how strtok works, other than "It calls forth Shub-Niggurath, The Black Goat of the Woods with a Thousand Young, Wife of the Not-to-Be-Named One, and devours your children."
 
huh?
 
It's a singleton state machine.
You call it first with the string and the delimiter and you get the first piece. And then you call it with NULL instead of the string, and it gives you the next piece.
State abounds.
 
> they work by modifying the contents of the character array and the address of the start of the array.
 
1:50 PM
@TonyTheLion did you get that?
 
@TonyTheLion And that.
 
When someone says they are going to optimize code by rewriting from C++ to C, that's what they want to do.
 
now how is that bad? I've lots of functions that alter the contents of some object or other
 
That can be acceptable.
 
when is it then considered a side effect?
 
1:51 PM
What is the result of strtok(NULL, ")")?
 
Yeah, that would be a B***h
 
thanks, I was getting confused.
 
If you're thinking the result of that is UB or segfault or NULL, or something, you're wrong.
 
It's the next piece.
 
1:53 PM
so strtok uses a static buffer to hold data between function calls
so that I can see is a bad thing
 
Right. Probably just a pointer or something.
 
but I don't understand the side effect bits?
and how modifying the input is a bad thing?
 
Oh, maybe I shouldn't have used the word "side-effect". I was using it to mean "holds state".
 
oh, that's just because it's static right?
and it's not thread safe
 
1:56 PM
thread safe?
 
so when do functions that hold state become bad?
 
Since like, ever? :)
 
@LewsTherin as it can't be used safely across multiple threads
@RMartinhoFernandes I don't know, I mean is there some rules or something that define when "holding state" becomes a bad thing?
 
strtok does the kind of thing that looks and can be done without keeping state.
 
oh I see
 
1:57 PM
In C++ we call them "functors" and they are wonderful.
 
Hi
 
Martinho just said it was bad
lol
 
yea I was thinking about functors, being stateful functions
 
@Potatoswatter But a functor is not a singleton!
strtok is.
 
Functors don't have to be global though.
 
1:58 PM
@TonyTheLion The term for that is "reentrant".
 
But functors aren't C functions or "first-class" function types, they're just any object implementing operator().
 
If you want to call a functor without interfering you create another instance and you're done.
With a global function, you're screwed.
 
'zackly.
 
@Potatoswatter Unless it's a surrogate!
 

« first day (332 days earlier)      last day (4614 days later) »