« first day (443 days earlier)      last day (4495 days later) » 

Xeo
4:00 AM
4 mins ago, by R. Martinho Fernandes
> WARNING!, we've fixed an earlier bug!
It's really exactly that.
 
Somehow I'm not surprised.
 
@RMartinhoFernandes And there exist things which cannot be type checked without running the metaprogram, such as a::value * b::value, as value could be a pointer type
 
Xeo
Now I have to #pragma warning(disable:4345) a warning that's basically a commit message.
 
@Pubby Hmm, I see that kind of errors as parallel to rejecting user input.
 
@Xeo Meh, could have been worse.
 
4:05 AM
The way I see it, the first lookup phase is TMP compilation and the second phase is (during) TMP execution.
 
Xeo
The first is during the first encounter with the template declaration, the second is when the template is odr-used.
 
The input to a metaprogram is what types (and/or compile-time values) you feed into the template (in std::vector<int> int is input to the metaprogram). Obviously, you can't validate input without running a program.
Type checking of the result of the metaprogram is a part of what some metaprograms do when they run. In fact, we have meta-programs that do nothing but type-checking: type traits.
I think I should write an answer.
 
4:26 AM
I added user comment to Microsoft's doc of that warning.
 
Xeo
Hi @Ben
 
Hi @Xeo
 
Xeo
You can edit messages for up to 2 minutes by hitting the up-arrow-key
 
So, I see two possibilities. One is that the compiler uses a single flag no matter how many static local variables there are. The other is that there's a separate flag for each.
 
Xeo
static char enter = (EnterCriticalSection(&cs), 0);
static Class instance;
static char leave = (LeaveCriticalSection(&cs), 0);
Just to get the code in here
 
4:37 AM
Yay, ugly workarounds.
 
Both cases are broken. Which do you want me to explain?
 
@Pubby Ah, dammit, the first lookup phase does not check as much as I thought :(. I have now realized it's got too much of both sides for me to pick a side now. I'll wait for concepts :P
 
Xeo
Like I said in my comment, execution will never reach the declaration of instance concurrently. It may concurrently try to enter the critical section, but once it's in there, only one thread will have the first pass through the declaration. All others will have the second pass and as such not initialize instance.
 
Ok, sounds like you're assuming a separate flag for each of the three statics. Fine.
 
I can write a lot of nonsense into a class template member function (like say, int x = nullptr;) and it won't reject it :(
 
Xeo
4:40 AM
@RMartinhoFernandes Err, all non-dependant stuff should be resolved in the first pass. Or do you write that inside a member function of the template?
 
Threads A, B, and C all enter the function simultaneously. enter has not yet been initialized, so all three execute its initializer. One of these gains the critical section, while the other two wait. Agreed so far?
 
Xeo
Because those are only instantiated when they are actually used
@BenVoigt Yes
 
@Xeo Oops, I meant member function :)
 
Xeo
Heh, too much meta programming today, eh?
 
Ok, the thread in the critical section initializes instance, then leave, which unblocks the other two threads.
 
4:41 AM
@Xeo But it complains if you do bar b; b.x = 0; when bar doesn't have an x. It's silly to check this and not other stuff.
 
Xeo
@RMartinhoFernandes Huh?
 
The second thread returns from EnterCriticalSection. instance has already been intialized, as has leave, so it returns with the critical section still held.
The third thread is still waiting inside EnterCriticalSection.
 
Xeo
@Ben: AHH! I see now
 
Forever.
 
Xeo
Damn, that sucks.
Thanks for enlightening me
 
4:43 AM
Glad to be of service.
You need a conditional inside the critical section. aka test-and-test-and-set.
 
Xeo
Hm. Well, we can always make leave nonstatic...
 
so now you're going to release a critical section you don't own?
 
Xeo
What does LeaveCriticalSection do if you're not actually inside a critical section?
 
Bad things, I'm sure.
 
Xeo
4:44 AM
:(
 
"If a thread calls LeaveCriticalSection when it does not have ownership of the specified critical section object, an error occurs that may cause another thread using EnterCriticalSection to wait indefinitely."
 
Xeo
Can't that just be a no-op like delete nullptr;? -.-
Damn, thanks
Hmmm
 
@Xeo I'd really rather not.
I don't want my locking mistakes to go by unnoticed. A big boom is really the best of things.
 
Xeo
Is there something like bool IsInsideCriticalSection(CRITICAL_SECTION*);?
 
@Xeo: No, critical sections are lightweight. Only kernel synchronization objects know what thread is holding them.
 
Xeo
4:46 AM
Hm.
 
Not that I'd go with that solution anyway.
 
Xeo
I'll try to think of something...
 
Xeo
Other than that it currently leaks
As I just noticed
 
but I guess you really want to avoid taking the critical section after initialization completes?
 
Xeo
4:48 AM
@BenVoigt That's what I had in mind, along with avoiding dynamic allocation
 
How does it leak, pray tell?
I think you could make test-and-test-and-set work.
 
Xeo
@BenVoigt When will it delete instance;?
4 space indention
Or hit the "fixed font" button
 
more problems than that
can't insert lines with enter, wasn't done
 
Xeo
Shift enter
And all lines need to start with the indention
 
    Class& get_class_instance() {
        static unique_ptr<Class> c; //by default, global and static variables are assigned default values. Hence c will be NULL when the program starts.

        if (!c.get()) {
            EnterCriticalSection(&cs);

            if(!c.get())
                c.reset(new Class(data));

            LeaveCriticalSection(&cs);
        }

        return *c;
    }
Four spaces didn't help :( fixed font button did.
 
Xeo
4:53 AM
@BenVoigt Why the double-test though?
 
This improves Chethan's answer in two ways. First, unique_ptr means the destructor will be called at the end of the program. Second, adds a test to prevent unnecessary use of the critical section.
 
Xeo
Ah, wait
Nevermind, I understand why double check
Yeah, that does the job.
One way to aviod dynamic allocation may be boost::optional btw
Same semantics, no allocation
 
will optional call the destructor appropriately?
 
Xeo
Sure
 
I don't use Boost much, so I don't know.
 
Xeo
4:55 AM
boost::optional<T> is basically a T, just that it will not default initialize upon construction
 
It's awesome.
 
ok, there you go. Even better than unique_ptr.
 
Xeo
Internally, it uses a correctly sized char buffer and placement new + destructor call
 
And you get to keep the same syntax! Almost.
 
yeah, I guessed as much
 
Xeo
4:56 AM
2 mins ago, by Xeo
Same semantics, no allocation
:)
Woops
That one
@RMartinhoFernandes Oh, yeah "almost" because you don't need new
 
@Xeo "correctly sized and aligned char buffer" I feel the need to be picky, because I'm angry at C++.
 
Just adjust the !c.get() to whatever test a boost::optional uses.
 
Xeo
@BenVoigt It uses the same
Or that
It offers a pointer interface
Since pointer naturally allow that "optional" usage
 
c.get() gets you the value, so you can't do !c.get() :)
 
4:58 AM
oh ok. But there must be a different interface to cause construction.
 
Xeo
I think it offers reset too
Just without the new of course
 
It's not moving yet :(
 
@RMartinhoFernandes Says who? unique_ptr::get() returns the pointer.
 
Xeo
@BenVoigt optional::get doesn't
 
@BenVoigt I was talking about optional :)
 
5:00 AM
Ok, getting confused. So it really isn't the same semantics.
And unfortunately just happens to also have a member named get(), that works differently.
 
&*c or &c.get() gets you the pointer.
 
Xeo
T const& get() const ;
T&       get() ;
// (deprecated)
void reset ( T const& ) ;
Interesting
Anyways, we can just use this:
 
Use assignment.
 
Xeo
bool operator!() const ;
Yeah, I was just surprised about the deprecation
 
5:02 AM
Then you're stuck with an extra copy-constructor call, that unique_ptr didn't have?
 
Xeo
Unfortunately, yes.
Since it doesn't support move semantics yet
 
3 mins ago, by R. Martinho Fernandes
It's not moving yet :(
 
Xeo
It also doesn't support emplace construction
 
Hopefully this will get fixed in the next release...
 
Xeo
T const* get_ptr() const ;
 
5:03 AM
Yeah, emplace is more what I was wanting there.
 
Xeo
I just noticed there is this
 
I like if (!c) just fine. I think that might work with unique_ptr as well.
 
Xeo
Well, you can just wrap T inside a moving or emplace constructing wrapper
 
@BenVoigt Actually, yes.
@Xeo I think it supports the boost typed factory thingies.
 
Moving, maybe. But emplace construction won't work, since optional is still going to call the copy-constructor of your wrapper.
 
5:04 AM
That's what they used before emplace.
 
Xeo
@RMartinhoFernandes Ahh, right
 
@BenVoigt It can provide a construct template member function that just perfectly forwards to placement new.
 
Ok. C++11 makes all of this so much happier.
 
Xeo
@RMartinhoFernandes I think he meant emplace construction through the wrapper
 
Ah, sorry.
 
Xeo
5:06 AM
Okay, so we got a solution that does only a single boolean check at every call.
It looks nice and easy, and doesn't have dynamic allocation
Now the only thing I want to get rid of is the boolean check. xD
 
@Xeo It's a pattern, make a template!
 
Xeo
lol
threadsafe_singleton or what? Hell no, I'm kinda happy the question wasn't already bashed to death for the singleton
 
What you laughing at? I'm serious dude. lazy_init.
 
Xeo
@RMartinhoFernandes Huh? You meant the boost::optional stuff?
 
@Xeo Oh, it's a singleton? Didn't notice.
 
Xeo
5:10 AM
lawl
 
But lazy_init<T> can be useful in other places too.
 
Xeo
@RMartinhoFernandes template<class T> using lazy_init = boost::optional<T>;
 
Not only for singletonness.
 
Xeo
> the point of singleton is that you don't delete instance, its allocated once and forever. If you expect exiting your application and still use singletons - then you put the delete code there (at the portion that handles exiting the application, that is). – littleadv 3 mins ago
I don't know what to say.
 
@Xeo Not quite.
I meant something that constructs when you call operator* or operator-> on it, for example.
 
Xeo
5:12 AM
@RMartinhoFernandes Ah, I see.
So @Ben, what are we gonna do about an anser? Do you want to put it up?
 
So you can do static lazy_init<T> x; x->foo(); and not worry about all those checking shenanigans.
 
So many exams this month. I hate this month.
 
nah, you got the boost:optional idea, so go with it.
 
Xeo
Alright.
 
Just leave a comment saying I had a good point so you fixed the race condition.
 
Xeo
5:14 AM
I'll link the discussion :)
(If you don't mind.)
 
Go with it.
 
@Xeo Well, I bit.
 
@Xeo On the other hand, freeing individual objects when the OS is about to reclaim your memory space en-masse is often just a waste of effort. Depends on what the destructor actually does.
 
Xeo
@BenVoigt Yep, that's why I specifically said it's leaking resources, not only memory.
 
Delete temporary files, close network connections, all those things.
 
5:27 AM
The OS probably reclaims about 95% of resource correctly as well, if you just leave well enough alone.
Network connections will be closed. Temporary files will be deleted (if you opened them with the right flags).
Nothing will go wrong that wouldn't go wrong if your process abended.
 
Xeo
named mutex (files), logs, database connections...
 
those all get flushed and closed as well.
 
Arrgh, write a footer on a file.
Beat that.
 
Xeo
atexit(&write_footer_to_file) ? xD
 
that's approximately what the compiler does, when you construct a static object.
 
5:29 AM
@Xeo But it needs an object!
 
Xeo
mimimi
 
You can't pass capturing lambdas to atexit.
That really sucks.
You need to build your own list of type erased function objects to workaround that crap.
 
No, but you can make your own vector of std::function pointers
Problem with that, is that the whole list will be invoked contiguously.
Whereas the desired behavior is inverse order of construction, intermingled with all other atexit tasks.
 
Xeo
Hm. Are mutable types of a const objects actually const and just not handled as such, or are they non-const?
 
... just noticed, I need to gain 70-ish rep stat. So it won't show up as 666.
 
5:32 AM
decltype to the rescue!
 
Xeo, example please? But I think they aren't const.
 
Xeo
    template<class T>
    struct move_wrapper{
      mutable T value;

      move_wrapper(T&& other)
        : value(std::move(other)) {}

      move_wrapper(move_wrapper&& other)
        : value(std::move(other.value)) {}

      move_wrapper(move_wrapper const& other)
        : value(std::move(other.value)) {}
    };
To work around the non-move awareness of Boost.Optional
Specifically the copy ctor
 
so are you talking about move_wrapper const&, or when T is const U?
 
Xeo
the former
(Inside Boost.Optional's assignment operator.)
 
It's not const. That's precisely what mutable is for.
To allow a member to be changed even when this is a pointer-to-const.
 
Xeo
5:34 AM
Okay, I just thought it was maybe just handled like if it was non-const
 
Not const.
I wrote my own move-aware optional some time ago. I never used it though.
 
Umm, does the contract for optional::operator= say that it copies the object exactly once?
 
@BenVoigt It uses assignment sometimes.
 
Because extra temporary copies could screw things up rather badly.
Just like in that question on why auto_ptr can't be stored in containers.
copy-constructors-that-actually-move are a bad idea, learned that from auto_ptr.
And Xeo, why don't you just drop the const from the reference, then you won't need mutable either?
 
Xeo
@BenVoigt Because it actually is const inside optional's assignment operator
 
5:40 AM
@Luc, I meant when the optional was previously empty.
 
Xeo
> Notes: If *this was initialized, T's assignment operator is used, otherwise, its copy-constructor is used.
 
@Xeo: ah, ok.
 
Xeo
Doesn't say anything about the number of copies though
 
I think the contract is in terms of 'as-if' and they could very well use any number of copies to get the job done.
 
Xeo
Oh well, I'm just gonna drop the move_wrapper example
Hm...
 
5:42 AM
Xeo, I see you haven't fixed your answer yet?
 
Just implement move semantics into the existing code and submit it!
I would, if I could navigate through portable code.
Portable code is ugly.
 
Xeo
static char instance_buf[sizeof(Class)]; // not currently aligned
std::unique_ptr<Class> p;
// ...

p.reset(new (&instance_buf[0]) Class(data));
 
I suspect boost will reject any patches that don't still compile with C++03.
 
Xeo
Now I just need a custom deleter
 
looking for an opinion about an api i'm writing, for a database access.
 
5:43 AM
@BenVoigt They have Boost.Move now, which emulates it for C++03.
 
Xeo
@BenVoigt I'm at it, currently being stuck with the wrapper :P
 
And many libraries have C++11 support #ifdefed. It just happens optional isn't one of them :(
 
ic. You need static on that unique_ptr though.
 
@Xeo Aren't char arrays guaranteed aligned for all types of at most the same size?
Or is it only dynamically allocated ones?
 
typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type if you ever need to hand-roll your own optional.
@RMartinhoFernandes Dynamic only.
 
5:46 AM
Ah I see.
 
Well, automatic aren't.
The question is about static duration.
 
Oh, you fixed it.
alignof is neater :P
 
And don't ever forget that std::aligned_storage is a metafunction :<
 
is all this available pre-C++11? I know alignof(T) isn't.
 
Xeo
5:48 AM
Boost can emulate much of it
 
@BenVoigt There's a Boost version which might differ a bit in the syntax.
 
Aren't aligned_storage and alignment_of from TR1 or something?
 
Xeo
I guess that alignment stuff resides in <memory>?
 
@Xeo It's <type_traits>.
 
Xeo
ah
Both?
 
5:48 AM
what do you think of this, for an API I'm writing for a database access?
da::database db("./db");
da::transaction(db)
    << "INSERT INTO creatures (name) VALUES (?);"
    << da::bind("Hamster")
    << "UPDATE creatures SET name=? WHERE name=?;"
    << da::bind("Salmon") << da::bind("Hamster");
 
@Xeo Yes. Even though only alignment_of qualifies as a type trait in my view.
 
Xeo
aye
 
do you think it's intuitive? maybe operator()() instead of operator<<()? or no buildup at all?
 
For some reason aligned_storage takes the size, not the type.
 
wilhelm, I think I'd prefer a helper object that implicitly uses bind on parameters.
 
5:50 AM
@BenVoigt what do you mean? can you show me a usage example?
 
da::transaction(db).execute("UPDATE creatures SET name=? WHERE name=?;") << "Salmon" << "Hamster";
 
Xeo
    #include <memory> // std::unique_ptr
    #include <type_traits> // alignment stuff

    template<class T>
    struct destructor{
      void operator(T* p) const{
        if(p) // don't destruct a null pointer
          p->~T();
      }
    };

    Class& get_class_instance() {
        typedef std::aligned_storage<sizeof(Class),
            std::alignment_of<Class>::value>::type storage_type;
        static storage_type buf;
        static std::unique_ptr<Class, destructor> p;

        if (!p){
looks about right?
 
@Xeo Remember that Seth is on a pre-C++11 world.
Boostify it.
 
Xeo
FUCK! You're right.
Noo, I don't want to D:
 
@Xeo, I think you can lose the (void*) cast, that's implicit.
 
5:52 AM
@BenVoigt k. and throw for missing bindings?
 
7 hours ago, by StackedCrooked
@Xeo Kids these days. Can't live without C++11 already...
@Xeo I know, I feel the same.
 
Xeo
Fuck this shit with unique_ptr, I'll just make a single in-place factory example..
@BenVoigt I know, but overloaded new could get in your way. Atleast I read something like that somewhere.
 
I thought this was about optional anyway.
 
Xeo
@LucDanton Alternatives since Boost.Optional doesn't support Boost.Move yet
 
@Luc, but optional doesn't support emplace (yet)
 
5:53 AM
@Xeo Force the use of the built-in new if you're afraid of that.
 
@Xeo You can't overload the standard placement new.
 
Xeo
ah, right. ::new
 
@Xeo There are no moves in C++03!
 
Xeo
better? :P
 
Where am I looking for the better?
 
Xeo
5:55 AM
Seriously, fuck C++03. When I look at C++11, C++03 looks like some little kid who doesn't know anything. :(
 
Let's just use scoped_ptr and dynamic allocation, then point out reasons C++11 is better.
 
I don't know how you guys survived before C++11.
 
Cause it just works. One static, compiler handles the critical section, done.
 
Xeo
Right
@RMartinhoFernandes We didn't know better
But now that we do, we don't want to go back
 
@RMartinhoFernandes Boost, really.
 
5:57 AM
Don't use it, actually.
 
@Xeo How could you not? I already know better than C++11: C++11 with polymorphic lambdas for example.
 
Xeo
Heh
 
Quick question: fixed, float, or quotients for rational meta type?
 
std::ratio?
 
TIL std::aligned_storage, thanks
 
5:58 AM
integral numerator and denominator, for rationals I believe
 
std::ratio exists?
 
C++11.
It's there for <chrono> stuff, which is there for threading stuff.
 

« first day (443 days earlier)      last day (4495 days later) »