Tuesday, October 15, 2013

Macro Expansion

Newsgroup: comp.lang.c++

Subject: Macro Expansion

From: Ryan <code@...>

Date: Tue, 15 Oct 2013 15:38:38 -0700 (PDT)



Below I have a series of defined macros. When ISVALID_TEST is called the compiler is complaining the macro doesn't have enough actual parameters. This means NOINPUT isn't expanding. The definition of ISVALID_TEST uses the UnitTest++ framework. This isn't an issue but unless you have it it won't compile. It is shown here to explain the macro signature.



How can I get NOINPUT to take the place of Kpos, Kvel and Kacc in the macro call instead of all being stuffed into Kpos?



Ryan





#define P99_PROTECT(...) __VA_ARGS__



#define NOINPUT P99PROTECT(false, false, false)



#define ISVALID_TEST(TestName, Kpos, Kvel, Kacc) \

TEST(TestName) \

{ \

point = std::make_shared<KinematicState>(); \

if (Kpos) point->set(position); \

}



//This line complains the macro doesn't have enough actual parameters

ISVALID_TEST(NoInput, NOINPUT);



//Works fine.

ISVALID_TEST(NoInput2, false, false, false);







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?108740-Macro-Expansion&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

String and interfacing with functions using char*

Newsgroup: comp.lang.c++

Subject: String and interfacing with functions using char*

From: DSF <notavalid@...>

Date: Mon, 14 Oct 2013 15:07:19 -0400



Hello,



This may be a question with an obvious answer, but I'm trying to

find out how to use a String object when it needs to be filled by a

function that uses char pointers. To make things simple, I'll use

strcpy as an example. It's properties are well known and should serve

better to explain than an obscure API call.



String cppstr = "Hello";

char cstr[20];



strcpy(cstr, cppstr.c_str());

...do whatever with cstr



I have no problem when the String is on the source side, but what if

it's on the destination side, too? Is something like this the only

recourse?



String cppstr1 = "Hello";

String cppstr2;

char *tempstr;



tempstr = new char["estimated size needed"];

strcpy(tempstr, cppstr.c_str());

cppstr2 = tempstr;

delete[] tempstr;

...do whatever with cppstr2



Or is there a way to get a C-type string directly into a String when

it's returned through a pointer as a parameter of a function? (And

no, unlike strcpy, the API call does not return the output C string

like strcpy does.)



DSF







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?107591-String-and-interfacing-with-functions-using-char*&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Saturday, October 12, 2013

RAII object in constructor

Newsgroup: comp.lang.c++

Subject: RAII object in constructor

From: =?ISO-8859-1?Q?Marcel_M=FCller?= <news.5.maazl@...>

Date: Wed, 09 Oct 2013 22:48:50 +0200



I have a mutex lock that is implemented as RAII. I need to acquire the

mutex as long as the constructor runs. But for the /entire/ constructor

run not just the construction of the derived class.





Example:



#include <stdio.h>



// some mutex class

class Mutex

{

public:

// hold the Mutex (RAII)

struct Lock

{ Lock(Mutex& mtx)

{ puts("Lock()\n");

//...

}

~Lock()

{ puts("~Lock()\n");

//...

}

};

//...

};



// set of configuration parameters

struct Parameters

{ //...

};



class WorkerBase

{ //...

protected:

WorkerBase(const Parameters& params)

{ // do something with params...

}

};



class Worker : public WorkerBase

{

private:

static Parameters GlobalParams;

static Mutex ParamMtx; // protect the above



public:

// synchronized public access to the parameters

struct AccessParams : Mutex::Lock

{ AccessParams() : Mutex::Lock(ParamMtx) {}

operator Parameters&() { return GlobalParams; }

};



Worker() : WorkerBase(AccessParams())

{ AccessParams params; // !!! Mutex acquired twice!

// do something with params...

}

};



Parameters Worker::GlobalParams;

Mutex Worker::ParamMtx;



int main()

{ Worker worker;

// do something with worker...

}





As expected the mutex is acquired twice. But this is bad, since the idea

is to sample a consistent set of parameters and not to use one parameter

set for the base class and some other one for the derived class.



Any ideas how to solve this?





Marcel







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?104289-RAII-object-in-constructor&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Tuesday, October 8, 2013

Copying part of a vector element to a string variable

Newsgroup: comp.lang.c++

Subject: Copying part of a vector element to a string variable

From: "Steph W." <swilks06@...>

Date: Mon, 7 Oct 2013 21:01:42 -0700 (PDT)



I know you can copy a vector to a string by using:



std::string t( v.begin(), v.end() );



But is it possible just to copy apart of a vector element to a string?



For instance if you have a vector that has the following:



S R A

F B 30

M A 49

F M 34



Each line would be an element and each char would be an element inside that element. So could take vector[1][4:5], which be the "30", and just put that into a string variable?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?102960-Copying-part-of-a-vector-element-to-a-string-variable&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Integer arithmetic when overflow exists

Newsgroup: comp.lang.c++

Subject: Integer arithmetic when overflow exists

From: junyangzou <zoujyjs@...>

Date: Mon, 7 Oct 2013 20:39:59 -0700 (PDT)



Tow 32 bit integer values A and B, are processed to give the 32 bit integers C and D as per the following rules. Which of the rule(s) is(are) reversible? i.e. is it possible to obtain A and B given c and D in all condition?



A. C = (int32)(A+B), D = (int32)(A-B)



B. C = (int32)(A+B), D= (int32)((A-B)>>1)



C. C = (int32)(A+B), D = B



D. C = (int32)(A+B), D = (int32)(A+2*B)



E. C = (int32)(A*B), D = (int32)(A/B)



A few questions about the integer arithmetic. Modular addition forms amathematical structure known as an abelian group. How about signed addition? It's also commutative (that?s where the ?abelian? part comes in) and associative, is this forms a n an abelian group?



Given that integer addition is commutative and associative, C is apparently true, because we can retrieve A by (A+(B-B)). What about D? Can we assume that 2 * B = B + B st. B = A+B+B-(A+B)?



And multiplication is more complicated, but I know that it can not be retrieve A if there is an overflow.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?102926-Integer-arithmetic-when-overflow-exists&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Monday, October 7, 2013

An Objective-C to C++ Translator

Newsgroup: comp.lang.c++

Subject: Re: An Objective-C to C++ Translator

From: lipingyang2010@...

Date: Mon, 7 Oct 2013 16:01:56 -0700 (PDT)



On Thursday, July 28, 1988 11:19:01 AM UTC-7, Jim Adcock wrote:

> I have an Objective-C to C++ "translation aid" that I'm willing to make

> available on an "unsupported public domain" basis to strongly motivated

> users.

>

> The translator reduces by maybe a factor of 10X the amount of time

> required to translate software from Objective-C to C++.

>

> Unlike some language translators, this one chooses not to do a flawless

> job of translating, but rather emphasizes not messing up the original

> naming conventions, indentations, and commenting schemes. This leaves

> some final manual clean up work to the user. On the positive side, the

> resulting code looks like real code, not some kind of gibberish.

>

> The potential user should have a fair chunk of code that needs to be

> translated -- say more than 10 classes, should be quite knowledgable in

> C++ and Objective-C, and should have some idea of the difficulties and

> limitations involved in language translation. The translator is implemented

> in K+R C code which should be relatively portable, but may take a day

> or two's work to get running on a new machine. Finally, the potential user

> should realize that using the translator is not going to be a very useful

> way to compare the relative merits of Objective-C and C++.

>

> This tool is not flawless, but has proven to be useful to some people.





That's what I am looking for. Can you share it?



thanks,







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?102812-An-Objective-C-to-C-Translator&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Friday, October 4, 2013

Hope for your project - a little off topic.

Newsgroup: comp.lang.c++

Subject: Re: Hope for your project - a little off topic.

From: woodbrian77@...

Date: Fri, 4 Oct 2013 15:08:44 -0700 (PDT)





Also I'll pay someone $100 cash and give them a $200

investment in my company if they help me find someone

to work with. The company rewards investments to 3

times their original amount. So you would receive

anywhere from $0 to $600 on the investment part,

depending on how things go for the company. I'll pay

the $100 cash after working with whomever it is for 4

months.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?101065-Hope-for-your-project-a-little-off-topic&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums