« first day (102 days earlier)      last day (2747 days later) » 

11:28 AM
What is the right way to write a function in C++ that : (a) share 98% of the code with an another function, while differ only in some details like the type of the returning value and (b) will be called so many times that it accounts for a unignorable amount of running time, and the program should be very efficient, and even a useless if or redundant work will increase the total workload proportionally
Can anyone give some suggestion?
 
@Thiner depending on the "some details" a template is fine
 
user1804599
@Thiner look up higher order functions
 
but a template won't help in this case, because I want one function to return only the bool that indicate whether the operation is invalid, and second function to return the backtrace which requires a (relatively) heavier work(allocate heap memory and log the data)
 
user1804599
I don't advocate that
 
that is one example for my question
but they share nearly 98% of the working code, with only a fractal of difference
 
11:33 AM
templates are efficient runtime wise
 
Factor the common part out then.
 
the common part intersects with the different part
 
then abstract the different part out
 
most of the cost of templates is at compile time and code size
 
I know my compiler is good at optimizing at templates
 
11:34 AM
at runtime each template use is basically just another function
 
what is 'abstract the different part out'?
do you mean, write my working function as a function that takes a lot of parameter of lambda expression, and write two wrapping function that input different lambda expression as argument to the working function?
that will be what a template will work
on
so speaking about lambda expression, is there any way to restrict the parameter type and the return type of the lambda argument?
 
nwp
@Thiner you just write the types like in any other function too?
you need a trailing return type, otherwise it is the same thing
 
So my question is: when I want to write a function that takes a lambda expression as an argument, how can I specify what types the lambda function takes as argument and returns?
 
nwp
11:53 AM
@Thiner If the lambdas are stateless use regular function pointers. You probably cannot afford to use std::function. You can use some ugly std::enable_ifs or some slightly less ugly static_assert + std::is_same. I would just not restrict it and work through the terrible compilation error when it happens.
maybe some day you can use concepts
 
std::result_of may be of help
 
@Thiner why did lambdas suddenly enter the picture?
auto some_interface(some_enum which_variant) { /* common part 1 */ if (which_variant == some_enum::first) uncommon1(); else uncommon2(); /* common part 2 */ }
 
template<boolean variant>
auto some_interface() {
/* common part 1 */
/*constexpr*/ if (variant)
    uncommon1();
else
    uncommon2();
/* common part 2 */
}
 
12:08 PM
@Thiner templates, let it all be resolved at compile time
but then, perhaps that results in bloat, which might be slower :D
it's almost like you have to try things out and test which is faster
but of course, those test only tell you what is faster for the platform you test on, different things, such as cache size might favour one version over another
 
12:38 PM
@Thiner Look at std::for_each. It's a function that shared 99% of the logic, and has a callback that actually does the varying part. en.cppreference.com/w/cpp/algorithm/for_each (look at "Possible implementation")
13 messages moved from Lounge<C++>
20 messages moved from Lounge<C++>
 
why though?
 
1 message moved from Lounge<C++>
 
 
3 hours later…
3:35 PM
Does it make sense to create an operator overload like:
A &&operator +(A &&a1, const A& a2){
    a1+=a2;
    return std::move(a1);
}
or will that delve into UB too easily?
 
4:05 PM
Hi Guys. If I have private field, which is unique_ptr and after some time I decide to implement public getter for those field, I assume getter should return shared_ptr, right? Should I convert unique_ptr to shared in getter, or just replace unique_ptr with shared for those field?
 
@silent_coder what do you want the ownership and lifetimes semantics to be?
but makeing a shared_ptr out of a unique_ptr will lead to dangling pointers and double deletes
never do that unless you know what you are doing there
 
Lifetime still should be related to main object I assume.
regarding ownership - this is currently opened question for me.
I should use shared_ptr only in case, when I expect that main object could be destroyed before the moment, that main class client will stop using getter's data, right?
ok, nevermind. looks like I found decent thread on stack
 
5:08 PM
@milleniumbug if I have a class called A and it's constructor take an int x and y, and if I then put the class type in a unique ptr like this unique_ptr<A> classA; , Would classA(std::make_unique<A>(1, 2)) be how you call the constructor in classA that takes in x and y?
 
5:28 PM
No, that's std::unique_ptr<A> classA(std::make_unique<A>(1, 2))
Or just auto classA = std::make_unique<A>(1, 2);
 
Hello. If I have static map<K, V> m{{"m1", 1}, {"m2", 2}, ...} which is the easiest way to convert it to map<V, K> with the same pairs, but now values goes to keys, and keys to values?
 
5:55 PM
I reasked as question there stackoverflow.com/q/40182573/1163230
 
user4651282
6:06 PM
hello @Natasha, look this.
 
6:22 PM
@milleniumbug what if you were initializing classA through initializer list i.e : classA(std::make_unique<A>(1, 2))? Would that work then?
 
hellro
I am trying to use access a file in my project, and I am not to sure about how to provide the relative path to the file
the working directory is ../project/sln/
however i need to access a file in ../project/src/res
 
so what's the problem
 
i am not sure of what the how the string should be formed to provide that particular path..
is that correct?
 
6:30 PM
the .. signifies a parent directory
hopefully you can manage from here
 
thanks for the help the correct way was ../src/res
.. is the parent of the working directory
does that work if i am trying to move two levels up?
such as ../../?
 
it's the parent to whatever directory your path refers
/home/user/whatever/../stuff -> /home/user/stuff
and relative paths get resolved using the working directory
 
 
1 hour later…
7:46 PM
Can anybody help with this quick issue? pastebin.com/uVnZ2B7c
 
so what's your problem
so far you've just dumped some code and what am I to do with it
 
It's described in the linked pastebin. But basically I have a callback that need an additional parameter
 
no "I and this method to have an additional parameter, a reference to std::vector<uint8_t> data" is not a problem description
 
writeCallback() needs access to data which is defined in serializeWrite()
 
what's the signature of Octree::traverse and can you change it
 
8:00 PM
void traverse(void(*callbackFunc)(uint32_t, uint8_t, bool));
 
don't take in a function pointer, use std::function or declare the function as a template that accepts the function
this way you can capture state along the callback
 
How would I go about templating this?
 
you'd write a template function that takes an argument of template parameter type
 
I have never worked with templates before, could your provide a quick snippet or is it a larger task?
 
13
A: Best practice for local variable scope in a C++ callback

Stuart GolodetzReplace your callback function with a functor - they can store state. An example functor: #include <iostream> #include <memory> class Functor { private: std::shared_ptr<int> m_count; public: Functor() : m_count(new int(0)) {} void operator()() { ++(*m_count)...

note that you can use lambda expressions instead of writing your own functor class
 
8:07 PM
Okay, thanks for your help
 
8:56 PM
nice functors!
 

« first day (102 days earlier)      last day (2747 days later) »