samedi 30 décembre 2017

What really is constant? Definition

I'm getting a little confused about constant. It says here in Programming Principles and Practice Using C++ book that you cannot give a new value to constant.

constexpr int max = 17;
int val = 19;
max+2

but this code is confusing me. max+2, that means the max will have a new value, 19, so what really is constant? how come you can add 2 to a constant?

Boost variant get underlying value

Is there a way to make the following work avoiding inheritance?

A and B share the same API, all I want to do is to obtain the underlying value and call on that the common member function.

#include <iostream>
#include <cstdlib>
#include <boost/variant.hpp>

struct A {
    void foo(){
        std::cout << "A" << std::endl;
    }
};
struct B {
    void foo(){
            std::cout << "B" << std::endl;
    }
};

template <typename Ret>
Ret getUnderlying(auto t) {
    switch (t.which()){
        case 0: return boost::get<A>(t);
        case 1: return boost::get<B>(t);
    }   
}

int main()
{
    using AB = boost::variant<A,B>;
    AB var;
    var = A();
    getUnderlying(var).foo();

}

http://ift.tt/2CcxLKk

vendredi 29 décembre 2017

error: use of undeclared identifier 'tolower' compiling code

Error:

clang++  -I.  -MMD -I/usr/local/Cellar/verilator/3.900/share/verilator/include -I/usr/local/Cellar/verilator/3.900/share/verilator/include/vltstd -DVL_PRINTF=printf -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=0 -Wno-char-subscripts -Wno-parentheses-equality -Wno-sign-compare -Wno-uninitialized -Wno-unused-parameter -Wno-unused-variable -fbracket-depth=4096 -Qunused-arguments     -std=c++11 -I../../../../common -lncurses   -c -o blinky.o ../../../simulator/blinky.cpp
../../../simulator/blinky.cpp:24:12: error: use of undeclared identifier
      'tolower'
    switch(tolower(userCh)) {
           ^

My CFLAGS are -std=c++11 -I../../../../common -lncurses

What am I doing wrong?

nodejs native addon : how to modify the value of c++ object's member contained in another native addon

First some context, i got two nodejs native addon. The first one contains a static c++ object "Conn" exposed using an v8 object internal field as described in the embedder's guide

NAN_METHOD(cobject) {
    auto isolate = Isolate::GetCurrent();
    Conn* p = &ConnHolder::connection;
    Local<ObjectTemplate> conn_templ = ObjectTemplate::New(isolate);
    conn_templ->SetInternalFieldCount(1); 
    Local<Object> obj = conn_templ->NewInstance();
    obj->SetInternalField(0, External::New(isolate, p));
    info.GetReturnValue().Set(obj);
}

In my other native addon, i'm loading the first one using c++ code and i expose a function called test containing two calls on the Conn object "callToDummyFunction()" and "callToFunctionWithMemberAccess()"

// persistent handle for the main addon
static Persistent<Object> node_main;


void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    // get `require` function
    Local<Function> require = module->Get(String::NewFromUtf8(isolate, "require")).As<Function>();
    Local<Value> args[] = { String::NewFromUtf8(isolate, "path_to_my_addon\\addon.node") };

    Local<Object> main = require->Call(module, 1, args).As<Object>();
    node_main.Reset(isolate, main); 
    NAN_EXPORT(exports, test);

}

NAN_METHOD(test) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    // get local handle from persistent
    Local<Object> main = Local<Object>::New(isolate, node_main);

    // get `cobject` function to get pointer from internal field
    Local<Function> createdMain = main->Get(String::NewFromUtf8(isolate,     "cobject")).As<Function>();

    Local<Object> callResult = createdMain->Call(main, 0, nullptr).As<Object>();
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    void* ptr = wrap->Value();

    // from there i get a pointer to my Conn object
    Conn* con = static_cast<Conn*>(ptr);

    conn->callToDummyFunction();
    conn->callToFunctionWithMemberAccess();

    info.GetReturnValue().Set(10);

}

Then i'm launching a nodejs session using "node", i load the first and second addon using two require calls and finally i'm calling the method test on the second addon.

The method test is executed, the call to "callToDummyFunction" is executed successfully but the call to "callToFunctionWithMemberAccess" crash and also kill the node session.

Ok, so what is the difference between "callToDummyFunction" and "callToFunctionWithMemberAccess" ?

bool Conn::callToDummyFunction()
{
    cout << "callToDummyFunction" << endl;
    return true;
}

bool Conn::callToFunctionWithMemberAccess()
{
    cout << "callToFunctionWithMemberAccess " << someIntMember << endl;
    return true;
}

So, it seems accessing a member of the Conn object generate an error and crash the node session. The node session does not output any message before crashing.

Can someone tell me why?

And/Or

How to get an error message ?

Wrap pattern std::begin; return begin(c); into a function

Is there a way to wrap the pattern into a general, template function?

template <typename C>
auto Begin(C&& c) -> ??? {
  using std::begin;
  return begin(std::forward<C>(c));
}

The question here is how to write the return type of the function here?

Why does g++ take much longer to compile

g++ version is 5.3.0.

#include <algorithm>
int main() {
    return 0;
}

test$ time g++ test.cpp

real    0m0.203s
user    0m0.073s
sys     0m0.031s

test$ time g++ test.cpp --std=c++11

real    0m0.761s
user    0m0.554s
sys     0m0.130s

Similar results over multiple attempts.

Initializing arrays, potentially unforseen consequences?

I've recently observed that by initializing an array, the garbage values are all replaced by null terminators. This would appear to be inconsequential, but in a large program I don't think it would be preferable to spend resources on cleaning out an array. Now to be clear, I don't program large programs, that's why I'm asking, I'm just a beginner. However, when used properly, it would stand to reason that garbage values would be perfectly fine as long as there is always an explicitly placed terminator.

So then, should I take it as bible to always initialize my arrays, or should I consider whether I can get away with leaving the garbage values untouched?