d

D using emplace


I am trying to use emplace in this piece of code:

class motionFactory{
        public:
            this(double velocity, double gravity, double angle){
                this.velocity = velocity;
                this.gravity = gravity;
                this.angle = angle;
            }
            motion getMotion(double step) const{
                auto ptr = malloc(motion.sizeof);
                return emplace!motion(ptr, velocity, gravity, angle, step);
            }
        private:
            double velocity;
            double gravity;
            double angle;
    }

    class motion : motionFactory{
        public:
            this(double velocity, double gravity, double angle, double step){
                super(velocity, gravity, angle);
                this.current = 0;
                this.step = step;
                this.range = getCurrentRange();
            }
        private:
            double current;
            const double step;
            const double range;

    }

And I keep getting this singular compilation error.

error: template std.conv.emplace cannot deduce function from argument types !(motion)(motion, const(double), const(double), const(double), double), candidates are:
  102 |                 return emplace!motion(ptr, velocity, gravity, angle, step);

After looking at the documentation for too long with no avail, I'm at a loss. Please help


Solution

  • I found the solution by tinkering with the examples on the doc page for emplace.

    size_t size = __traits(classInstanceSize, motion);
    void[] ptr = malloc(size)[0..size];
    return emplace!motion(ptr, velocity, gravity, angle, step);
    

    This compiles and works just fine for me. Fascinatingly enough keeping the cast there meant the D allocator would get called somewhere which would opens the door to allocator mixing.