vendredi 29 mai 2015

Unpack ts... to t0.a(), t0.b(), t1.a(), t1.b(),

I want to call one variadic function from another variadic function in the following manner:

template <typename ...Ts>
void f(Ts const & ...) { /* ... */ }

template <typename ...Args>
void g(Args const & ...args)
{
  // shall call f(arg0.a(), arg0.b(), arg1.a(), arg1.b(), ...)
}

I have done it the following way:

struct sentinel_t { };

template <typename Arg, typename ...Args>
void g_impl(Arg const & arg, Args const & ...args)
{
  g_impl(args..., arg.a(), arg.b());
}

template <typename ...Ts>
void g_impl(sentinel_t , Ts const & ...ts)
{
  f(ts...);
}

template <typename ...Args>
void g(Args const & ...args)
{
  g_impl(args..., sentinel_t{});
}

Is there another/better way to implement this pattern?

how to pass both function pointers and lambda's using one interface

I am trying to use function pointers and lambda's together using one interface. I decided to use std::function, but i quickly found out std::function cannot deal with overloaded functions by itself.

Example:

void foobar(double i_double ){std::cout << "double argument" << i_double << std::endl;}
void foobar(){std::cout << "no argument" << std::endl;}
void foobar(int){std::cout << "int argument" << std::endl;}

std::function<void(double)> func = static_cast<void(*)(double)>(&foobar);

Only by using a static_cast this code compiles.. Since at our company we have quite a lot of overloaded functions this is too much of a hassle. For now i decided to implement two interfaces.. one for std::function objects and another one for function pointers, although i would really like to just wrap the function pointers in a std::function object as well (without additional code on the calling side).

Any "nice" solution to this problem?

Run Time Error in a C++ code

I am getting Run-Time-Error in a C++ code. I am giving my source code. Need Help! Thanks in advance.

Source code:

#include <map>
#include <cstdio>
using namespace std;

class Pair{
    public:
    int x;
    int y;
};

map < Pair , int > mapper;

int main(){
    Pair a;
    a.x = 8;
    a.y = 9;
    mapper[a] = 1; // Here i get Run-Time-Error
    return 0;
}

Using Type Traits from Base Class

I am trying to understand the concept of type traits.

Say i have some templatized Class Hierachy like this and a client function:

template<typename T>
class Base
{
public:
//...
    virtual bool inline isSymmetric() const = 0;
};


template<typename T>
class ChildrenOperation:public Base<T>
{
public:
//...
    virtual bool inline isSymmetric() const override
    {
        return true;
    }
};

void clientFunction(const Base& operation)
{
  //do symmetry independent stuff...
  if(operation.isSymmetric())
  { //use operation the one way 
  } else { //use operation the other way
  }
}

Obviously, clientFunction is polymorphic and different children can have different implementations of isSymmetric. However, since isSymmetric seems to be constant and really more of a type information, i've read about type traits and i was wondering whether it is possible to rewrite the client function to not depend on isSymmetric on runtime, but rather compile time.

I've tried adding a trait like this. But i am not sure how to specialize it and use it in a polymorphic context.

template <typename T>
struct is_symmetric {
  static const bool value = false;
};

How efficient smart pointers are?

I know, that std::shared_ptr uses reference counting, so it has copy&move semantics, on the other hand std::unique_ptr (hence the name unique) only has move semantics, so trying to copy it is a compile error.

However, its not quite clear for me how big of a deal is that. Can I simply use std::shared_ptr over std::unique_ptr in most cases, or should I use std::unique_ptr whenever possible, as it's far more efficient because it doesn't have to take care of reference counting?

Also, I know that smart pointers are useful when dealing with, for example exception safety, but are they a mean to generally replace traditional T* pointers? Is it a good programming practice to use smart pointers over traditional T* pointers whenever possible?

Static array of lambda functions (C++)

I'd like to do something like this (inside a class):

static constexpr MyStruct ops[6] = {
    {'+', [&] (double a, double b) { return a+b; } },
    {'-', [&] (double a, double b) { return a-b; } },
    ...
};

Being MyStruct like:

typedef double (*binOp)(double, double);
struct MyStruct {
    char c;
    binOp fn;
};

I also tried:

std::function <double(double,double)> fn;

for the definition of fn, but no luck.

The error I get for the first case is "error: field initializer is not constant" which I don't really get. If I try with std::function it's worse since it says "cannot be initialized by a non-constant expression when being declared".

Why is the lambda function non-constant? Am I missing something?

Thanks!

shared pointer behavior when container object passed as (void*)

I have shared_ptr variable in my class object (ObjA). There is a requirement where this object is to be stored as (void*) entity of another Class' object (ObjB).

My doubt is, what will be the behavior of shared_ptr (will associated heap memory be freed?, what will happen to its reference count?)-

  1. when ObjA is converted to void*

  2. when void* entity of ObjB is cast back to (ClassA *)

Simplified Code:

Class AA{

    shared_ptr<int> aptr;

    public:
        AA(){
            aptr = make_shared<int>(100);
        }
        shared_ptr<int> get_aptr(){
            return aptr;
        }
};


Class BB{

    void *smpl;

    public:

        void setter(void* p){
            smpl = p;
        }

        void* getter(){
            return smpl;
        }
};

int main(){

    AA *objA = new AA();
    BB *objB = new BB();

    objB->setter(objA);
    //status of allocated int in class AA here?

    //some code later
    AA *objA_2 = (AA*)objB->getter();
    //status of allocated int in class AA here?

    shared_ptr<int> p2 = objA_2->get_aptr();
    //is this allowed

}