Archive for the ‘C++’ Category

C++ Using template type to make decision about how to extend class

June 4, 2013 Leave a comment

so the problem was a follow, I had a parent template class and as usual template child class extended from the parent class. Objective was depending upon what type of the Child class to select the type of the parent template.

Therefore went looking for how to pick up the  type of the variable depending upon the template. Found this really nice post.

Using information in it and little experimentation following code shows how to do it.

// Name        : forTestingAndTrying.cpp
// Author      : Mohsen Ali
// Version     :
// Copyright   : use freely on your own risk, but contribute
// Description : testing template
#include <iostream>
#include <type_traits>
using namespace std;

/*Parent Class*/
template<typename T>
struct parentMe{
T myData;
void print(void){
cout << "parentMe datasize " << sizeof(myData) <<endl;


template<bool B>
struct stUseless: parentMe<typename std::conditional<B, double, float>::type>{
typedef typename std::conditional<B, char, int>::type T;
typedef typename std::conditional<B, double, float>::type T2;

T dat;
void print(){
cout <<"Size is " <<  sizeof(dat) <<endl;
cout <<" --------------------  " <<  endl;

int main() {
stUseless<false> a;

stUseless<true> b;

cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;

and the output is below

Size is 4
parentMe datasize 4
Size is 1
parentMe datasize 8
!!!Hello World!!!

Categories: C++ Tags: , , ,

jsoncpp writing back the structures and arrays

May 24, 2013 Leave a comment

Well successfully read the json file of the matlab mat structure. Lots of things to care of, not as simple as it looks.

Now writing back, that is taking the C++ class, structure and basic data types and outputting a json structure. Since I have already written the reader to the writer should match the reader’s expectation.

This one is quite decent information and advice on how to design class for such activity.


Will follow the writing part now and later will try to implement the reader in much more structured way.

Categories: C++, code, jsoncpp Tags: , , , ,

parsing jason nested array using jsoncpp

May 22, 2013 Leave a comment

so first experiments with the json and json cpp.

the json string is as follow

{“id”: 1,”tags”: [ [800, 99.7], [], [76] ],”stock”: {“warehouse”: 300,”retail”: 20}}

Wanted to read the nested arrays of “tags” as you can see such an array could be e.g. cell datatype in the matlab or could be represented as the array of vectors

int main (){</pre>
unsigned int i,j;
char fileName[] ="abc.json";

ifstream infile;
infile.exceptions (ios::failbit|ios::badbit);
{ (fileName);
catch (ifstream::failure& e)
cerr<<"\n Exception opening file "<<fileName<<": "<<e.what ();
return 0;

string input;
Json::Reader reader;
Json::Value root;
bool parsingSuccessful;

while (getline (infile,input,'\n'))
if (false == (parsingSuccessful = reader.parse (input,root)))
cerr<<"\nFailed to parse configuration:"
<<reader.getFormatedErrorMessages ();
return 0;

int verbName = root.get ("id","").asInt ();
Json::Value data = root.get ("tags",0);
cout << "size is " << data.size() <<endl;

//vector<float> freq (mWidth * mHeight);
for (int i = 0; i < data.size(); i++){
int sizeint = data[i].size();
cout <<endl <<"---------------" <<endl;
cout << "Size of individual is " <<sizeint << endl;
Json::Value dat1 = data[i];

for(int j=0; j< dat1.size(); j++){
cout << dat1[j].asDouble() << " ,";

catch (ifstream::failure& e)


return 0;
Categories: C++, code, jsoncpp, matlab Tags: , , , , , ,

Running Makefile on the Windows

August 1, 2012 Leave a comment

There are many ways to run make program on the Windows, many of them will ask you to run in cygwin or similar shells. I was interested in running the makefile in the Windows so that the application generated could be used by the other Windows programs.
So the solution is to use the Visual Studio Command Prompt, run it, cd to the directory where the makefile is and then use the following

nmake -f Makefile

If the Makefile is using g++ or gcc in it you have to change it to “cl”, this the compiler being used by the VC. You might have to make little changes but then it should work fine.


Have a look at following


Categories: C++, code Tags: , , , ,

Dynamic Multi-dimensional memory allocation C/C++

October 15, 2010 Leave a comment

An excellent library for the multi dimensional memory allocation.

some other code snippets are

Recovering 2D motion between the frames of video

June 28, 2010 Leave a comment

Motion2D is C/C++ library by Irsia/INRIA to estimate 2D parametric motion in between the frames of video.  Works very good. Have a good example code.

Only headache is I am having problem in compiling with the VC. They don’t have any dsp file for compilation of code in Windows, so don’t know which libaries to create. Have to use cygwin.

Block commenting in Visual Studio

June 26, 2010 Leave a comment

Having worked in the Matlab, you get habit of selecting multiple lines and commenting them just using short keys.  So nowadays when I have to work in Visual Studio, I tried finding short keys for this.

Comment: Ctrl+K+C

Un-Comment: Ctrl+K+U

Categories: C++ Tags: ,