« first day (431 days earlier)      last day (2428 days later) » 

8:26 AM
Hey guys, was wondering, is there no other way than this to support having a distinction between rvalue and lvalue arguments when passing into a function? It looks like I'd have to support all permutations of rvalue and lvalue which could be big if a function has a lot of inputs:
 
nwp
@OneRaynyDay That's a known issue. There is no really good solution. You can use forward references instead, but that widens the function which might be problematic.
Another solution is that if you don't need to make a copy then take a reference, otherwise take a copy and then move it to where it belongs.
The copy-constructor is then supposed to handle temporaries correctly.
 
@nwp I see. Well then is there a way to know when a value has been constructed implicitly?
like void f(int x, int y){}; int x = 2; f(x, 3); // f knows y was constructed implicitly
I know that using the explicit keyword you can disallow passing in like f(x,3).
 
nwp
That's a temporary, you can tell because it binds to a int &&.
 
mmm sorry, bad example, consider this:
class foo{ foo(int x); }
void f(foo x, foo y){};
foo x = 2;
f(x, 3);
now it calls the constructor of foo.
 
nwp
8:46 AM
You can only tell that it is a temporary which binds to a non-const &&. But you can't tell the difference to f(std::move(some_foo)).
What is the problem you actually want to solve?
 
Sure, so it's still the autodifferentiation library I'm writing, and I got everything to work, i.e. calculates everything and build passes, but I'm optimizing
I'm optimizing specifically the operator+ and etc.
The operator+ right now takes the follow: (var lhs, var rhs), and the var variable is simply a wrapper that holds a pImpl reference(its only member is a shared_ptr to the actual implementation)
The user would ever really want to get the gradient from a variable they defined, consider the following:
var x(10);
var y = ((x + 10) + 20) * 3;
...
derivatives(sigmoid, {x}); // something like this
It would return the derivative with respect to x.
the optimization comes in this form: If any rvalues are created temporarily in the middle, then we want to implicitly create a var class with a flag set.
 
nwp
Are you profiling your code properly?
 
Noone wants the derivative of the variable implicitly created by 10 in x+10, right? So we shouldn't calculate it
 
nwp
Normally copies are cheap and branches on flags not so much.
 
@nwp I didn't profile, but this is a definite win for when the objects are large.
The win is more obvious when you see:
var y = x * matrix(1000,1000); // matrix multiply
dx/dy would be needed
but d(matrix(1000,1000))/dy is not necessary
but we'd be doing a gradient calculation there, which is absolutely terrible in terms of speed
 
8:57 AM
the copy construction from a temporary to a parameter will be elided anyway: coliru.stacked-crooked.com/a/97468f8c4e46a109
 
nwp
I don't really know what exactly all that does and why a simple assignment would calculate derivatives of every term.
I recommend you spend some time trying to get profiling to work. Compilers tend to be good at optimizing out obvious inefficiencies, for a strange definition of obvious.
 
Ehm, I tried to abstract away all the explanation, but the moment the explanations are needed I felt like I needed to explain everything
Just trust me when I say that, according to my API, there will be a case where arbitrarily large data structures will allow this to give massive speedups
No profiler is needed for this specific scenario, in my mind it seems obvious
 
you should still take a look at what copies will be elided in what cases
then you know to focus on only certain types of uses
 
@ratchetfreak I don't think it got elluded, if that's what you mean? :o I see your coliru
The only reason that the code isn't marked as ambiguous by the compiler is because you have 2 args there
I'm considering changing from the signature foo(T, T) to foo(const T&, T&&), foo(T&&, const T&), foo(T&&, T&&), foo(const T&, const T&)
 
I gave them separate names to avoid any overloading and to illustrate that copies get elided
 
9:08 AM
I see what you mean, but I'll give enough flexibility so that it doesn't
 
nwp
@OneRaynyDay What are you doing with the Ts? Reading, writing, storing?
 
@nwp Using the Ts to create a new T
Actually, here's an example that might make everything easier:
var x = 10; // ctor implicitly called here, then = operator
var y(10); // ctor explicitly called

assert(x.flag == true);
assert(y.flag == false);
I want the implicit calling of the ctor to be known
 
you can't, and you shouldn't
 
I've broken many "C++ should/shouldnots" in making this API already :P the user is probably not a c++ guru. But damn @ can't
 
nwp
@OneRaynyDay I don't think that's what is happening. Those should both call var(int). Compare to this.
 
9:15 AM
woah. __PRETTY_FUNCTION__ is pretty neat O_o
 
copy elision at work ^^
 
nwp
This is probably better, I forgot operator =(int).
 
what about: coliru.stacked-crooked.com/a/b4fd8c54854fe96e allows no implicit construction.
 
nwp
@OneRaynyDay Unfortunately an extension. Doesn't work on VS, but they have a similar one.
 
nice. Will use this often when debugging, thanks :)
 
nwp
9:21 AM
@OneRaynyDay Doesn't seem to help you because it disallows var x = 10;.
 
It doesn't directly, but it's a step in the right direction. It disallows var x = 10;, but I wish there was a way to allow me to also have the second function available. Then as priority explicit will be called when var(10) is called, and the implicit will be called during var x = 10; which will allow me to separate the two successfully
 
nwp
Why do you want to distinguish var x = 10; and var x(10); again?
(btw you should use var x{10}; instead)
 
@nwp in an equation y = 10 + x, you want to get the derivative of x, right? Who on earth would want to get the derivative of "10"? It's implicitly a variable in my library, and you CAN right now. However, you can disallow this and the program will run faster
less flexibility -> better performance is a pretty common tradeoff, so trust me that it does have significant optimization wins when you talk asymptotics
 
then make variables in a different way than the constants
 
@ratchetfreak they are different in that they share a different member, say x.flag
 
nwp
9:28 AM
@OneRaynyDay Does it really compute the derivative of every single term on a simple assignment? Don't you have to request the derivative somehow where you must specify the term to derive by?
 
@nwp you request it on y, and you CAN get back dx or "d10"
this is a common method called reverse mode differentiation, it's used in tensorflow fyi
I have no idea what their optimizations are cause their code makes my eyes bleed, but im sure they incorporated this somehow
 
@OneRaynyDay I mean different actual different types
 
nwp
@OneRaynyDay So you express "give me the derivative of y in respect to some unknown variable"?
 
@ratchetfreak that means I need to allow for operator+(var, const), operator+(const, var), operator+(var, var), operator(const, const)
which is no better
@nwp yes, pretty much
 
var x{10}; //no implicit construct from int
constant c = 20; //implicit construct from int
expression e = x+c;
but var and constant can implicitly convert to expression
 
nwp
9:32 AM
@OneRaynyDay That seems like a design flaw. People should pass in the variable explicitly.
 
so you only have to deal with operator+(expression, expression)
 
@ratchetfreak then you need to make it references, or pointers. You run into object slicing like that
 
I'm not talking inheritance
 
@nwp it's not a design flaw if it makes the correct interface easier to implement, and the incorrect interface harder to implement
 
but instead operator expression or expression::expression(const var&) and expression::expression(const constant&)
 
9:33 AM
@ratchetfreak you mean expression(var), expression(constant) should be exposed?
hmmmm
 
user creates the leaves (constants and variables) explicitly and then constructs expression trees from those
 
nwp
@OneRaynyDay The original version says something like "Make interfaces easy to use correctly and hard to use incorrectly". It doesn't talk about "easier to implement" because usage usually trumps implementation difficulty. Your library has no clue what to do and the user has no clue what the library is doing. Having an easy implementation is no excuse for that.
 
@nwp I meant implementation for the user, i.e. use
The user knows the top-level idea, which I have explained in the docs. This is by far the annoying implementation(for me)-wise project I've had to face when coding in C++
 
welcome to library design
 
nwp
@OneRaynyDay That's because you are trying to use C++ as a DSL. Also people don't read the docs.
 
9:38 AM
@nwp I felt it was a necessary evil when I have to deal with relatively fast performance
@ratchetfreak I think your idea works :o, but one thing I'm having trouble with is considering the ease of "flipping the switch" between const and nonconst
In machine learning, people like to stop gradient flow arbitrary and train different parts of the neural net, for example.
I had that idea in mind when I was thinking to make a flag toggle.
It might just be a "suck it up you can't do it" thing if I can't use the flag and fallback to expression like you said
 
 
2 hours later…
11:21 AM
@nwp Is it bad that I think this formatting is objectively 100% better: coliru.stacked-crooked.com/a/67547bef35861e38
@OneRaynyDay Pro tip: In c++ community, use words that are closely connected to the meaning you wish to convey. Compilers are unforgiving, c++ programmers even less so
 
@sehe I see your formatting and raise you another: coliru.stacked-crooked.com/a/661867b10da3b0a0
 
@OneRaynyDay You... have to "deal with relatively fast performance"? That's a complaint not often encountered
 
nwp
@sehe Depends on your criteria. My criteria is "whatever the project's .clang-format says". I don't think it is even possible to express your formatting in a .clang-format file, because it really doesn't like multiple expressions in 1 line.
 
@ratchetfreak Somehow that's less legible to me, but yeah in this very specific, artificial, case there's an argument to be had in favour if that
@nwp I did this in vim: :!clang-format % and then ViB:Align { }<CR>
Oh, of course I then inserted noexcept for no particular reason.
@nwp Oh I lied. But there's an obvious fix coliru.stacked-crooked.com/a/fc9db6e594c6b237 :) trololol
 
nwp
But I don't want to be left out, so here is the ultimate superior formatting.
(I think I'll stick to clang-format)
 
11:29 AM
@nwp you forgot to align the param in the first operator=
 
nwp
Oops. fixed
maybe we can align the = in main with the << above
 
will bool x{}; initialize to false?
Yay, markdown again! (unlike irc)
 
nwp
@kim366 Yes, it's called "value initialization".
 
Okay, that's what I've thought. The guys at the sfml chat say otherwise, though
 
nwp
11:45 AM
@kim366 You can point them to the documentation. Should be 4) and the explanation of "The effects of value initialization are:" eventually lead to "otherwise, the object is zero-initialized".
 
Awesom, thanks!
 
nwp
Does anyone know how to install a current-ish binutils on travis' ubuntu 14.04? Their ld is too much out of date.
I have a local VM ubuntu to test with, but none of the PPAs seem to have more current binutils.
 
nwp
12:03 PM
@nwp Install binutils-2.26 which seems to be already provided and you just need to replace ld using sudo update-alternatives --install /usr/bin/ld ld /usr/bin/ld-2.26 90.
 
 
1 hour later…
1:25 PM
I find it interesting how "dead" this chat is compared to the sfml chat.
 
it depends on how many people with problems come here
 
nwp
I prefer to call the state "all problems solved" instead of "dead".
In my mind there is a rule to ask here and talk in the lounge. I would consider people lounging here a bug.
 
Ron
2:20 PM
While working with some legacy code I often saw const static string s = "something" and pretty much every other const static T var = value variation. What's the point of having both const and static for some helper variables in cpp file?
Or is that some nasty C style bad practice?
In separate namespace in source file, that is.
 
static on a global means that it should not be visible to other translation units
const means it's read-only
those are orthogonal
 
Ron
I see. Appreciate it.
 
 
1 hour later…
3:52 PM
Hi room! Someone here?
I have two memory management questions. So I have a singleton class and I get the singleton like so:
auto manager = Manager::getInstance()
Now my question is: Do i have to release the memory for the instance if it's in a function for example?
void Test::test(){
auto manager = Manager::getInstance()
}
 
singleton by definition are unique and don't need to be released unless by program exit
also you can use shift+enter to enter a multi line message.
Then a button with fixed font will appear to let you format code and preserve whitespace
 
Ok. So the instance of the manager variable is going to be released at the end of the function
Ok thanks
@ratchetfreak so my statment above is correct right?
 
if it's an actual singleton then it doesn't matter.
if getInstance actually creates a Manager then it should return by value or smart pointer and it will happen automatically
 
@StephanHofmann If you need to release anything, you're doing it wrong. (see: RAII)
 
This is the Manager implementation:
Manager* Manager::instance = nullptr;

Manager* Manager::getInstance(){
    if (instance == nullptr){
        instance = new Manager();
    }

    return instance;
}

Manager::Manager(){
    instance = nullptr;
}
Ok then it should be fine
Next question is the following code:
 
nwp
4:06 PM
How does that compile? Manager::getInstance is not static.
 
It is static
 
@StephanHofmann Normally, your getInstance should be static, and the constructor should be private so only the designated static member function(s) can access it.
 
Yeahh that was the .cpp file not h
 
@StephanHofmann You're using new so it's wrong (see: RAII)
 
H:
—

class Manager{

    static Manager* instance;

    Manager();

public:

	static Manager* getInstance();

}


Cpp:
—

Manager* Manager::instance = nullptr;

Manager* Manager::getInstance(){
    if (instance == nullptr){
        instance = new Manager();
    }

    return instance;
}

Manager::Manager(){
    instance = nullptr;
}
Is this incorrect?
 
nwp
4:11 PM
You are still using new, so no. static Manager* instance; should be static Manager instance;.
 
return a reference to a local-static Manager
not class-static
 
Manager Manager::instance {};

Manager* Manager::getInstance(){
    return &instance;
}
 
I'm just wondering. I took it from a example code: gist.github.com/pazdera/1098119
 
nwp
That example code is not very good.
 
lazy initialization is very often not actually needed
 
4:16 PM
Manager* Manager::getInstance(){ static Manager m; return &m; }
a non-lazy way risks SIOF issues
 
Ok thanks I changed it accordingly
 
That said, don't use singletons and you'll live a happier life
 
This line "return &instance" only works cause of the static keyword right
Yeah I think I my case it makes sense
Ok after that my real question
Next question is the following code:

class Abstract{

public:
	virtual void test() = 0;
}

class Test : public Abstract{

public:
	virtual void test();

}


class Assembly{

…

public:

	Abstract * resolveAbstract(){
		return new Test();
	}

}
 
@StephanHofmann no the & unary operator means address-of
 
nwp
@StephanHofmann You are still leaking memory by default.
 
4:21 PM
@StephanHofmann I doubt it, but vOv
 
So I have an abstract class. The Test-Class inherit from it. The Assembly-Class is like an resolver for the instance.
 
though in the case of Millenium's code if you didn't have static you would return a pointer to a local variable and UB ensues
 
nwp
Inside Abstract it should be void test() override;.
 
So my question is when I return the "new Test()". How do I have to release the memory for this?
 
You use a std::unique_ptr and std::make_unique instead
 
4:23 PM
don't return it as pointer but wrapped in a std::unique_ptr
 
19 mins ago, by milleniumbug
@StephanHofmann If you need to release anything, you're doing it wrong. (see: RAII)
 
Ok like so:
std::unique_ptr<Abstract> Assembly::resolveAbstract(){
    return std::unique_ptr<Abstract>(new Test());
}
?
 
related:
 
return std::make_unique<Test>();
 
7
Q: Industrial-strength n-ton base class template

fredoverflowI am working on an n-ton base class template. I don't worry about laziness yet, so the Intent is: Ensure a class has only n instances, and provide a global point of access to them. Here is my code so far: template<typename Derived, size_t n = 1> class n_ton_base // Singleto...

 
4:25 PM
std::unique_ptr<Abstract> Assembly::resolveAbstract(){
    return std::make_unique<Test>();
}
 
I don't have make_unique in the std namespace. Any idea?
 
it's C++14
-std=c++14
 
Ah ok. so "std::unique_ptr<Abstract>(new Test())" does not work?
 
it's a last-resort alternative to std::make_unique
Prefer std::make_unique over that code
 
Ok. Then I leave it like above
And the instance will be released safe after exiting a function for example
 
4:31 PM
yes, once it goes out of scope (RAII)
 
Ok. Just for my understanding this correctly. I could also use this:
Abstract Assembly::resolveAbstract(){
    return Test();
}
And this would be also safe in point of releasing memory right? Only if the object is complex it would be not good for performance
Or yeah realtive performance
 
no, it would fail to compile because it would slice over an abstract class
 
Ah I see. Thank so far. Just one more. The singleton code from above. Is it also c++14? I can't compile it
h:
—

class Manager{

    static Manager instance;

    Manager();

public:

..

}


Cpp:
—

Manager Manager::instance {};

Manager* Manager::getInstance(){
    return &instance;
}
Changed it to this
 
it's C++11 because of brace-initialization
also I don't see the declaration of getInstance
 
Ah yeah sorry. But I have it:
 
4:42 PM
omg "I can't compile" - where is your error message
also, I wonder what kind of ancient compiler you have that doesn't support C++14
 
Sorry my pc is literlly dying
Undefined symbols for architecture x86_64:
"Manager::Manager()", referenced from:
___cxx_global_var_init.3 in Manager.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Mmhh Xcode in newest mac version
 
your constructor doesn't have a definition
Either define it or do Manager() = default; instead of your declaration
@StephanHofmann clang is your compiler, Xcode is your IDE
 
Nice thanks. Your guys are great
Maybe you can answer me one simple question. Its related with the Assembly thing. Lets say I have an instance of a class which is like really deep in the hierachy of objects. Like Object A has Object B has Object C has our instance. This instance use an interface like the Abstract class in my example. Whats the best way to instantiate it? Also in regards if I have other Classes (instances of this class) which use the Abstract class. Should I pass it down the objects (from A -> B -> C -> I)
Or should I make in the constructor of the instance itself, or a singleton class?
 
5:00 PM
Read this question 4 times and I'm still not quite sure what are you asking
you can pass instances to the constructor, or create them inside
 
Ok mom. I write a better example
 
"Ok mom" I'm not your mom :P
 
Lets say I have an abstract class Settings and a XMLSettings class which inherit from it. I use the abstract Settings class in many other classes. Now my question is how can I instantiate the abstract class with XMLSettings the best way globally?
I could instantiate the XMLSettings in all class constructors which use the Settings class but this seems error-prone. Or what if have to "resolve" it in a normal function so it's not declared as a member variable.
Do you understand what I mean? :p
Sorry
 
nwp
@StephanHofmann Not sure about the best way, but Settings &get_current_settings(); would be a start.
Also why does XMLSettings exist? You should have a non-static Settings that has member functions load_from_xml, load_from_registry and load_from_json.
 
5:19 PM
I want an interface of Settings which uses functions like save or getBoolX regardless how they are saved in the background.
Or handled in the background
 
nwp
You would use save_as_xml or save_in_registry for that.
I don't know what getBoolX is supposed to do.
 
That was just an example of one function. Lets say isSoundEnabled() and setSoundEnabled(bool enabled)
 
nwp
Those would just wrap a bool inside Settings.
Inheritance is just one tool in your toolbox and comes with significant downsides. You should try to avoid using it as long as you can.
 
@StephanHofmann Not to quibble over irrelevant details, but I generally advise against Boolean parameters. I'd much rather you used an enumeration, so that was setSound(enabled); or setSound(disabled); --and possibly better still, just enableSound() and disableSound().
 
Ok. I keep that in mind ;) Thank to all! espascailly to @milleniumbug
 

« first day (431 days earlier)      last day (2428 days later) »