the Design of classes referencing each other in the large c++ project

{ Multi-posted to comp.lang.c++. -mod }

Hello Group,


Say I have classes A and B and the both need to maintain a pointer/
reference to each for various reasons, say optimization. And there are
other classes in the project use these two classes, among others.
What is the best design for such architecture?


I saw errors by explicitly including headers of both classes. Also,
assume that include guards are included in each header file. I prefix
each class name with C to preserve convention.

Also, instead of including headers directly, I try to maintain
pointers to each class for now. But below is what I am trying to
create in best way, least coupling, and NO COMPILER ERRORS

In CA.h file
----------------
include"B.h"
//and other headers

class A {
private:
B * pB;
};
------------------

In CA.cpp
-----------------
#include "CA.h"
//other headers
// implementation of A class memebers


In CB.h file
----------------
include"A.h"
//and other headers

class B {
private:
A * pA;
};
------------------

In CB.cpp
-----------------
#include "CB.h"
//other headers
// implementation of A class members


COther.h

#include "CA.h"
#include "CB.h"
//some other stuff

COther.cpp

#include

//implementation
====================


Please let me know if my question isn't clear enough, I am working
with a huge system and want to organize one of its component classes
in the best fashion possible, avoiding coupling and a long build
time.

Other than Lakos's classic (Large scale C++ software design), what
reading material (books, papers) would you suggest dealing explicitly
with large projects and mentioned sort of issues?

Thanks

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Int value in a double variable

leocm wrote:
> What is the best manner to check if the value of a double variable is
> an int?

You wisely left "best" to our interpretation; checking the value
returned by modf has many benefits, anyway :-)

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview):
Do you need expertise in C++? I'm available.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Test for NaNs?

"Alf P. Steinbach" writes:

| * Gabriel Dos Reis:
| > "Alf P. Steinbach" writes:
| >
| > | Not very long ago there was anoher clc++m thread about NaN detection where I
| > | proposed x==x, and Someone Else pointed out that even g++ (IIRC) gets that
wrong
| > | when the right, or wrong :-), kind of optimization is turned on.
| >
| > GCC indeed has switches that tell the compiler to assume that some
| > kinds of floating point singularities are absent from the program.
| > There is nothing surprising in that.
|
| It surprised me that the compiler reports being IEEE 754 standard-conforming,
| via is_iec559, and isn't.

unhelpful oversimplification/distorsion.

With the appropriate switch (e.g. -ffinite-math-only), the user can
instruct the compiler has to trust him/her that there is no floating
point infelicities in your program. Consequently, the compiler is free to
optimize the input program accordingly. That the user thinks the
compiler is lying to him/her might actually be a consequence of a
reflection law where user is lying to the compiler. But, I suspect
the user already figured that part out by reading the fine manual.


-ffinite-math-only
Allow optimizations for floating-point arithmetic that assume that
arguments and results are not NaNs or +-Infs.

This option is not turned on by any -O option since it can result
in incorrect output for programs which depend on an exact
implementation of IEEE or ISO rules/specifications for math
functions. It may, however, yield faster code for programs that do
not require the guarantees of these specifications.

The default is -fno-finite-math-only.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Test for NaNs?

On Sep 13, 1:31 pm, "Alf P. Steinbach" wrote:
> * Gabriel Dos Reis:
>
> > "Alf P. Steinbach" writes:
>
> > | Not very long ago there was anoher clc++m thread about NaN detection where I
> > | proposed x==x, and Someone Else pointed out that even g++ (IIRC) gets that wrong
> > | when the right, or wrong :-), kind of optimization is turned on.
>
> > GCC indeed has switches that tell the compiler to assume that some
> > kinds of floating point singularities are absent from the program.
> > There is nothing surprising in that.
>
> It surprised me that the compiler reports being IEEE 754 standard-conforming,
> via is_iec559, and isn't.
[snip]

I think Alf's trying to say that if that particular optimization is
turned on, then the is_iec559 flag should be turned off.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

general purpose RAII with closures

On Sep 13, 1:30 pm, Peter Dimov wrote:
> On Sep 12, 1:10 pm, JoshuaMaur...@gmail.com wrote:
> ...
>
> > class Handle;
> > Handle * allocateHandle();
> > void deallocateHandle(Handle* );
>
> > int main()
> > { Handle * h = allocateHandle();
> > call_on_stack_unwind_t
> > freeHandleHelper(
> > boost::bind(deallocateHandle, h));
>
> > }
>
> Others have already mentioned Petru Marginean's ScopeGuard idiom:
>
> http://www.ddj.com/cpp/184403758
>
> For the code above, though, I'd use:
>
> int main()
> {
> Handle * h = allocateHandle();
> shared_ptr guard( h, deallocateHandle );
>
> }
>
> There's also
>
> shared_ptr guard( (void*)0, bind( deallocateHandle, h ) );
>
> for cases where allocateHandle doesn't return a pointer. These are
> rare in practice though.

Can most compilers actually optimize away the overhead of shared_ptr
for this use though? I fully expect the ScopeGuard idiom to be fully
expanded inline by any halfway decent compiler, resulting in optimal
code. However, shared_ptr does some internal allocations, and if this
is the threadsafe version, it also has synchronization, both of which
I'd expect to be difficult for the compiler to optimize away.

I was looking for an idiom for exception safety. The paper on the
topic describes exactly what I want, an easy way to get exception
safety (and other basic RAII) with no overhead. If I'm going to be
littering my code with it, I would hope it can be easily expanded
inline for no overhead.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Please suggest use-cases for introspective code generation

Andre Poenitz wrote:
>
> Incidentally, it is fun to watch the cattle cycle of denial and
> embracement going on with the Defenders of the Faith of Pure C++.
>
> Examples?
>
> Hash maps have to be bad, after all they were dropped for the Truth of
> the Year in 1998. Some people would have liked them in, still, something
> had to be sacrified for political reasons.
>
> Now it turns out the political reasons are forgotten, and of course
> hash maps are fine and are elevated to the state of Pure C++.
>
> So Truth is changing over time. What you are condemning here is a
> successful concept in other languages, and I wouldn't be surprised if
> they became Pure C++ in 2015. But then, of course, under the protection
> of the Defenders ;-)

The omission of hash maps from C++98 was, as far as I can tell,
not due to political reasons but rather logistical: the committee
simply didn't have time for it. See what Bjarne Stroustrup says
at .

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

C++0x: returning rvalue references, recycling temporaries

Howard Hinnant wrote:
>
> Now consider:
>
> string operator+(const string&, const string&);
> string operator+(string&& x, const string& y) {x += y; return
> move(x);}
>
> const string& d = a + b + c;
>
> This is slightly more expensive because it involves an extra move
> construction. However "a + b + c" now creates a temporary whose
> lifetime is going to extend beyond the semicolon until the reference d
> is destructed due to the language in 12.2 [class.temporary].
>
> For the cost of an extra move construction (after an append) you can
> sleep better at night. :-)

1. How is it more expensive by involving an extra move construction?
As far as I understand, move(x) above is just a shorthand for
static_cast(x). Does the static_cast incur any (significant)
operation in the machine code level?

2. Why do we need the move? It converts an lvalue reference to an rvalue
reference, but we are returning by (r)value anyway, and we can always
safely return a reference when the return type is not a reference and
that creates a copy of the referent; is this correct?

T foo(T& t) { return t; } // returns a *copy* of t
T foo(T&& t) { return t; } // what about this?

What about not writing the explicit move: is it correct/desirable?

string operator+(string&& x, const string& y) { x += y; return x; }

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

find uninitialized class members?

Tom Horsley wrote:
> Is there some compiler (or a g++ option I couldn't find) that can
> warn about constructors which fail to initialize some class
> members? Or even some code analysis tool which will do that?
>
> I clearly have an uninitialized member somewhere in a large
> complicated app, and I was hoping I could use a tool to reduce
> the number of constructors I need to review.
>
> (I know it must be something uninitialized because overriding
> the global new operators and calling calloc() in them makes
> the bug disappear :-).

I'm not sure how a constructor can fail to initialize a class member:
the member's constructor should be called, whether it is explicitly
mentioned in the initializer list. (And if it's not mentioned, the
default constructor should be available; otherwise, the program is
ill-formed.)

Are you talking about compiler bugs?

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

The difference between G++ and VC 2008 in allocate memory


> > address of ii out foo 0012FF6
> > address of ji out foo 0012FF5
sorry it is should be
address of ii out foo 0012FF60
address of ji out foo 0012FF54


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

arrays and cv qualification conversions

In article ,
Christof Meerwald wrote:

> consider the following program:
> [shortened]
> void f1(char *a[][2]);
> void f2(char * const a[][2]);
> void f3(const char * const a[][2]);
>
> int main()
> {
> char *a[1][2];
>
> f1(a);
> f2(a);
> f3(a);
> }
>
> which of these function calls (conversions) are legal?
>
> Most compilers appear to be happy with f2 (g++ 4.2, Visual C++ 2008, Sun
> Studio 12, Digital Mars C++, HP aCC), but only Digital Mars and Sun Studio
> will accept f3.
>
> Can anyone point me to the section in the C++ standard where it allows these
> conversions?

First, let's eliminate the implicit array-to-pointer nonsense, since
that's not at the heart of your question:

void f1(char * (*a)[2]); // pointer to char * [2]
void f2(char * const (*a)[2]); // pointer to char * const [2]
void f3(const char * const (*a)[2]); // pointer to const char* const [2]

int main()
{
char* aa [2];
char* (*a) [2] = aa; // pointer to char* [2]
f1(a);
f2(a);
f3(a);
}

All three calls are valid as far as I can tell. The first doesn't
involve any conversion. The second simply converts from pointer-to-T to
pointer-to-const-T, clearly valid. The third is the tricky one, and
valid as far as I can tell. See section 4.4 Qualification Conversions,
paragraph 4. In summary, you can add const (and volatile) at any
"depth", as long as you also add const to all shallower levels. As
stated in the standard, this is to ensure that const safety isn't
implicitly violated. The reasoning is that when you add it at some
level, it's not there in the caller's type therefore if the pointer were
changed to point to a const object, the caller could modify it.
Therefore, const must be added to all shallower levels to prevent the
callee from changing anything. Examining f3(), one can see that there's
no way const safety can be violated without a cast. If we added f4()

void f4(const char * (*a)[2]); // pointer to const char* [2]

calling it would be in error, since it could do (*a) [0] = "foo", and
then the main(0 could attempt to modify the string's contents by doing
(*a) [0] [0] = 'X', all without any const_cast.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Can declarations cause an exception? Or function calls?

On 13 sep, 04:28, winkerbean wrote:
> Can a simple declaration cause an exception to be throw?

A simple declaration, no.
But a variable définition, yes, since it may call a constructor.


> For example,
> could the following program conceivably result in an exception?
>
> int main()
> {
> int i;
> }
>
> Possibly on account of stack overflow?

Stack overflow doesn't raise exceptions.
This code is nothrow.


> What about a function call?

A function call may throw, of course, if an exception is raised in its
body.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

general purpose RAII with closures

On Sep 12, 1:10 pm, JoshuaMaur...@gmail.com wrote:
...

> class Handle;
> Handle * allocateHandle();
> void deallocateHandle(Handle* );
>
> int main()
> { Handle * h = allocateHandle();
> call_on_stack_unwind_t
> freeHandleHelper(
> boost::bind(deallocateHandle, h));
>
> }

Others have already mentioned Petru Marginean's ScopeGuard idiom:

http://www.ddj.com/cpp/184403758

For the code above, though, I'd use:

int main()
{
Handle * h = allocateHandle();
shared_ptr guard( h, deallocateHandle );
}

There's also

shared_ptr guard( (void*)0, bind( deallocateHandle, h ) );

for cases where allocateHandle doesn't return a pointer. These are
rare in practice though.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Test for NaNs?

* Gabriel Dos Reis:
> "Alf P. Steinbach" writes:
>
> | Not very long ago there was anoher clc++m thread about NaN detection where I
> | proposed x==x, and Someone Else pointed out that even g++ (IIRC) gets that wrong
> | when the right, or wrong :-), kind of optimization is turned on.
>
> GCC indeed has switches that tell the compiler to assume that some
> kinds of floating point singularities are absent from the program.
> There is nothing surprising in that.

It surprised me that the compiler reports being IEEE 754 standard-conforming,
via is_iec559, and isn't.

In other words, it surprised me that that compiler isn't reliable but lies to
you that it is.

And for at least one other compiler (icl) this is the default, as reported in
the previous clc++m discussion about this subject.

Perhaps you're right that the compiler lying to you "is" not surprising.

It is, perhaps, as you indicate, the new reality of programming, that our tools
can't be depended on, that that is what one should expect, that it isn't at all
surprising that they do very different things from the documented behavior, or
that one set of documentation contradicts another set, and that one should
therefore simply try this, and that, and so on, more or less at random, until
things work well enough to be usable or one decides that enough time's been wasted.

But I disagree with that fatalism.

I think it *should* surprise us when a compiler lies. I think compilers should
be dependable tools. I think we should be able to work under the assumption that
at least the compiler is dependable, even if much else isn't.


Cheers & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Please suggest use-cases for introspective code generation

Le Chaud Lapin wrote:
> One day, during a discussion, I tried to illustrate this principle to
> a group of colleagues with not much success until I gave the following
> proposition:
>
> "Suppose you are about to buy the latest-and-greatest of big-screen
> LCD TV's. One costs $2000, and the other costs $2005, and they are
> identical, in every aspect, even the logo and packaging, except the
> one that costs $2000 requires you to keep a small, heavy, non-toxic
> brick somewhere next to the TV for the TV to work. Which TV would you
> buy?"

Good example. Now another one:

"Suppose you are about to buy the latest-and-greatest of big-screen
LCD TV's. One costs $1000, and the other costs $2005, and they are
identical, in every aspect, even the logo and packaging, except the
one that costs $1000 requires you to keep a small, little paper
label somewhere next to the TV that says "Be aware of the Dragon".
Which TV would you buy?"

Andre'

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Duplication between type_traits and concepts

> Except for iterator traits that are present in C++03, type traits are
> mentioned only in TR1 and in the *draft* of the next C++ standard.
> Neither of those two papers is an ISO standard. This doesn't mean that
> they can be easily removed from the draft, but for sure it's not as
> "nearly impossible" as it would be if were speaking of a standard.

There are also char traits that could be successfully replaced (or
duplicated) with something like Character concept. Also, it seems that
some parts of numeric limits fit better into ArithmeticLike and
FloatingPointLike foundational concepts.

Regards,
&rzej

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Copy-assignment and C++0x move-assignment operator ambiguous?

When implementing the assignment operator of a class by copy-and-swap,
it's often preferable to have its argument passed "by value", instead of
by const-reference. As was discussed here, "Re: Is self assignment test
valid?",
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/97ad5390e34d63a9

Now when such a class is "upgraded" to C++0x, would it be okay to just
add a move-assignment, without having to change the original
copy-assignment operator? For example:

////////////////////////////////////////////////////////////
class foo {
public:
foo & operator=(foo); // Argument passed "by value".
foo & operator=(foo &&); // C++0x rvalue reference.
};

int main() {
foo obj;
obj = foo(); // Okay?
return 0;
}
////////////////////////////////////////////////////////////

So far, I've only tried GCC 4.3.0 (ConceptGCC - BoostCon Edition), which
says (unfortunately):
error: ambiguous overload for 'operator=' in 'obj = foo(0u)'
note: candidates are: foo& foo::operator=(foo)
note: foo& foo::operator=(foo&&)

While I would have appreciated the compiler to select the
move-assignment operator in this case.

I'm especially interested because I've just submitted a few tickets to
http://svn.boost.org, requesting a by-value argument for the
copy-assignment operator of various Boost types, as was suggested by
David Abrahams.

Kind regards,

--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Can declarations cause an exception? Or function calls?

winkerbean wrote:
> Can a simple declaration cause an exception to be throw? For
> example, could the following program conceivably result in an
> exception?
>
> int main()
> {
> int i;
> }

Not for an int, but possibly for a user defined type, where the
constructor might throw an exception.

>
> Possibly on account of stack overflow?

There is no such thing in the language. The use of a stack isn't
required for an implementation (but extremely common, of course).

> What about a function call?
>
> void f(){}
> int main()
> {
> f();
> }

A function could do just about anything, including throwing an
exception.


Bo Persson


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

find uninitialized class members?

On Sep 12, 10:29 pm, Tom Horsley wrote:
> Is there some compiler (or a g++ option I couldn't find) that can
> warn about constructors which fail to initialize some class
> members? Or even some code analysis tool which will do that?
>

PC lint will warn that not all members are initialized during
construction. And even if they are initialized in the wrong order.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Please suggest use-cases for introspective code generation

Le Chaud Lapin wrote:
> [...]
> Programming languages are not pots of soup. It is not a good idea to
> take as many "beneficial" features of tools and primitives at same
> level of abstraction and throw them all together.

That's an interesting stance to take in defense of "pure" C++.

After all, "pure" C++ _is_ a hodgepodge of beneficial features,
historical liabilities and accidents, political compromises and bad
influence of cosmic radiation.

But then, it _is_ quite usable for programming, so that cannot be bad.
For practical reasons.

*sigh*

Incidentally, it is fun to watch the cattle cycle of denial and
embracement going on with the Defenders of the Faith of Pure C++.

Examples?

Hash maps have to be bad, after all they were dropped for the Truth of
the Year in 1998. Some people would have liked them in, still, something
had to be sacrified for political reasons.

Now it turns out the political reasons are forgotten, and of course
hash maps are fine and are elevated to the state of Pure C++.

So Truth is changing over time. What you are condemning here is a
successful concept in other languages, and I wouldn't be surprised if
they became Pure C++ in 2015. But then, of course, under the protection
of the Defenders ;-)

Andre'

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Test for NaNs?

On 13 Sep, 04:28, Jiang wrote:

> 3. Some posters, including me, believe using isnan(), if it is
> available, is better than above "x==x" test.

So what if isnan() is not available, as on my system (VS2005)?
One alternative for my system is boost::math::isnan(), but it
is, as far as I know, not universally available.

And in anticipation of C++0x, will it include standardized
declarations of useful math constants like pi, e and Euler's
constant?

It turned out an old friend like 's M_PI is a
non-standard extension, and not available in VS2005.
(And before somebody suggests to flick extension
switches in the compiler: I try and make a point of
*not* using extensions.)

Rune


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

general purpose RAII with closures

On Sep 12, 12:10 pm, JoshuaMaur...@gmail.com wrote:
> I want to be able to create an object whose constructor takes an in-
> place function definition as an argument (a closure, facilitated by
> boost::lambda or the newC++0x standard), and that function will be
> called when the object is destroyed.

Why not use std::function?

class scope_guard
{
public:
scope_guard(function f)
: f(f)
{
}

~scope_guard()
{
f();
}

function f;

private:
scope_guard(const scope_guard& rhs)
scope_guard& operator=(const scope_guard& rhs)
};

// usage:

{
handle* h = allocateHandle();
scope_guard guard(bind(deallocateHandle, h));
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Matrix Multiplication

On 2008-09-13 05:16, Gaudium wrote:
> Hello All, I have difficulties in multiplying big matrices. I have to
> multiply two 4096 x 4096 matrices. I used matrix template laibrary,
> lapack, boost but they didn't help. Is there any other efficient
> library for matrix operations. The code I used with boost library is
> below. Is it possible to improve this code? Thanks for your help.
>
> #include
> #include
> #include
> #include
>
> using namespace boost::numeric::ublas;
> using namespace std;
>
> void main()
> {
> int i,j;
> int N=4096;
> ifstream infile("C:\\Nese\\cplusplus\\ornekler\\Trmat0yeni.dat");
>
> matrix A(N,N);
> for(i=0;i> {
> for(j=0;j> {
> infile>>A(i,j);
> }
> }
> infile.close();
>
> matrix B=prod(A,A);
>
> //cout<>
> }

First of all, are you sure that the actual multiplication is the bottle-
neck, and not reading the matrix from file? If non of the libraries you
tried helped this is probably as good as it gets without spending money
(Intel Math Kernel, etc.) or using more exotic libraries (NVidia CUDA,
etc.). You can also try to write your own multiplication function (it is
not very difficult) but I doubt that will help much.

--
Erik Wikström


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Int value in a double variable

What is the best manner to check if the value of a double variable is
an int?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Test for NaNs?

Jiang writes:

[...]

| 3. Some posters, including me, believe using isnan(), if it is
| available, is better than above "x==x" test.

yes, isnan() should be preferred.

(Also note that isnan() tests only for quiet NaN. For signaling NaN,
the x == x test is worse).

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

std::max(unsigned, size_t), amd64 and C++0x

On Sep 11, 5:46 pm, Pete Becker wrote:
> On 2008-09-10 22:59:16 -0400, Howard Hinnant
> said:
>
> > I consider this a bug in the rvalue-ref papers. :-) I don't consider
> > it a serious bug. But it is a bug. I've yet to submit a defect
> > report on it.
>
> Note, however, that this is not a defect report, since it doesn't apply
> to an existing standard. I hate to keep harping on this, but having a
> "defect report" has procedural consequences under ISO rules that are
> different from the C++ committees internal tracking requirements.

Is there a term (other than "defect report") which can be used to
describe a formal query about draft standards (and proposals)? The
reason you keep harping on about it is that people keep needing such a
term, and reusing "defect report" for it.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Matrix Multiplication

On 13 ÓÅÎÔ, 07:16, Gaudium wrote:
> Hello All, I have difficulties in multiplying big matrices. I have to
> multiply two 4096 x 4096 matrices. I used matrix template laibrary,
> lapack, boost but they didn't help. Is there any other efficient
> library for matrix operations. The code I used with boost library is
> below. Is it possible to improve this code? Thanks for your help.
>
> #include
> #include
> #include
> #include
>
> using namespace boost::numeric::ublas;
> using namespace std;
>
> void main()
> {
> int i,j;
> int N=4096;
> ifstream infile("C:\\Nese\\cplusplus\\ornekler\\Trmat0yeni.dat");
>
> matrix A(N,N);
> for(i=0;i> {
> for(j=0;j> {
> infile>>A(i,j);
> }
> }
> infile.close();
>
> matrix B=prod(A,A);
>
> //cout<>
> }
>
> Nese Aral

{ clc++m banner removed -- please, please don't quote it. -mod }

You can try Arageli library (http://www.unn.ru/cs/arageli/index.html)
if there is some fast algorithm for matrix multiplication then most
probably this algorithm is already implemented in this library.
Probably there is some specific information about these matrixes that
will help to improve performance? (a lot of nulls or something else)


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

find uninitialized class members?

On 13 ÓÅÎÔ, 06:29, Tom Horsley wrote:
> Is there some compiler (or a g++ option I couldn't find) that can
> warn about constructors which fail to initialize some class
> members? Or even some code analysis tool which will do that?
>
> I clearly have an uninitialized member somewhere in a large
> complicated app, and I was hoping I could use a tool to reduce
> the number of constructors I need to review.
>
> (I know it must be something uninitialized because overriding
> the global new operators and calling calloc() in them makes
> the bug disappear :-).

{ clc++m banner removed -- please, please don't quote it. -mod }

Which platform do you have?
On Linux I recommend to use Valrind (http://valgrind.org/) it provides
a lot of information about program execution including using of
uninitialized values, so you should just launch your program through
valgring (e.g.: valgrind --leak-check=full --log-file=my.log ./
my_program program_parameters) and when program end execution you will
receive all required information including file names and line numbers
where uninitialized values have been used (some code investigation
will be required anyway).
Of course, binaries should be compiled in debug mode and source files
should be available.

I don't know analogs for Valgrind of Windows, probably you should
check Purify, I have no much experience of working with it.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

general purpose RAII with closures

On Sep 12, 7:28 pm, gpderetta wrote:
[snip]
> The solution for this problem is well known, google for ScopeGuard.
[snip]
> Basically it relies on lifetime extension of temporaries.

Thank you Mathias Gaunard and gpderetta! I was unaware of C++03
12.2.5. (I should really sit down and read the whole standard one of
these days.) That nicely solves my problem of how to (effectively)
declare (and define) an instance of a class template on the stack
without explicitly specifying the template argument.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Matrix Multiplication

Hello All, I have difficulties in multiplying big matrices. I have to
multiply two 4096 x 4096 matrices. I used matrix template laibrary,
lapack, boost but they didn't help. Is there any other efficient
library for matrix operations. The code I used with boost library is
below. Is it possible to improve this code? Thanks for your help.

#include
#include
#include
#include

using namespace boost::numeric::ublas;
using namespace std;

void main()
{
int i,j;
int N=4096;
ifstream infile("C:\\Nese\\cplusplus\\ornekler\\Trmat0yeni.dat");

matrix A(N,N);
for(i=0;i {
for(j=0;j {
infile>>A(i,j);
}
}
infile.close();

matrix B=prod(A,A);

//cout<
}

Nese Aral

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

What type of smart pointer should a factory method return?

francis_r wrote:
> In my application I have a factory method that creates an object on
> the heap, and another object that ends up owning it. Right now my
> factory returns a shared_ptr, and the owner stores it as a shared_ptr
> as well. This solution has actually been working very well.
>
> However, my pointers are not really shared among different objects, so
> a shared_ptr is not needed. I have been trying come up with a better
> solution. For the owner I can use boost::scoped_ptr. However for the
> factory it is not obvious to me what type of pointer it should return.
> Options would be to return a raw pointer or an auto_ptr. The auto_ptr
> seems the correct choice, but I remember reading that it will become
> deprecated in the future. And a raw pointer could cause a memory leak
> if an exception would get thrown in between creating and assigning to
> owner.
>
> So what would be the correct solution then?

Until the next standard is released, auto_ptr looks like the correct
choice. It will be deprecated only in the next standard, which will
provide us with a better solution called unique_ptr.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Can declarations cause an exception? Or function calls?

Can a simple declaration cause an exception to be throw? For example,
could the following program conceivably result in an exception?

int main()
{
int i;
}

Possibly on account of stack overflow?
What about a function call?

void f(){}
int main()
{
f();
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

find uninitialized class members?

Is there some compiler (or a g++ option I couldn't find) that can
warn about constructors which fail to initialize some class
members? Or even some code analysis tool which will do that?

I clearly have an uninitialized member somewhere in a large
complicated app, and I was hoping I could use a tool to reduce
the number of constructors I need to review.

(I know it must be something uninitialized because overriding
the global new operators and calling calloc() in them makes
the bug disappear :-).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

plot an array

On Sep 12, 12:35 pm, dpm314 wrote:
> Hello -
>
> I am wondering if anyone can point me to a simple class or library in
> c++ to display a 2 or 3D array. I don't need anything fancy - I am
> looking for something easy to use/implement for visualizing some data
> analysis I am doing.
>

Please check Gnuplot and its C++/C interface please.

> Thanks, David M. - new lister
>

Regards,

Jiang


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

What type of smart pointer should a factory method return?

On 12 sep, 23:02, francis_r wrote:
> The auto_ptr
> seems the correct choice, but I remember reading that it will become
> deprecated in the future.
> [...]
> So what would be the correct solution then?

The correct solution would be the one that replaces auto_ptr,
unique_ptr.
It is C++0x only, though.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

C++0x: returning rvalue references, recycling temporaries

On Sep 12, 4:58 pm, marc.gli...@gmail.com wrote:
> On 12 sep, 05:38, Howard Hinnant wrote:
>
> > That's a fair summary of my thoughts on this as well. It isn't
> > that:
>
> > string a, b, c;
> > string d = a + b + c;
>
> > is unsafe when string+string returns a string&&. It is just that you
> > allow the possibility for your client to do something like:
>
> > const string& d = a + b + c;
> > // use d here
>
> > That code works if a + b + c returns by value and doesn't work if it
> > returns by string&&.
>
> Really, it doesn't? I just looked at N2723, and in paragraph 8.5.3.5
> it seems like the case where the initializer is a rvalue reference
> falls into the last "Otherwise", where a temporary is created and
> bound to the reference (not as good as binding to the same object as
> the rvalue ref and increasing its lifetime, but still ok). Or does the
> term "rvalue" mean "either rvalue or rvalue reference" there? I am not
> that familiar with the language of the standard, so I may easily
> misinterpret.
>
> I am really interested in this, because I have already started writing
> code that looks exactly like your example:
> const string& d = a + b + c;
> with my own type instead of std::string. And since I wrote that within
> the first day of trying to use rvalue references, I assume it is not a
> marginal case.
>
> What is your advice for someone writing a class like string? Always
> return by value?

Yes, always return by value.

Consider:

string operator+(const string&, const string&);
string&& operator+(string&& x, const string& y) {x += y; return x;}

const string& d = a + b + c;

"a+b" forms a temporary string which then binds to "x" in the second op
+ overload. That temporary is appended to and then returned by rvalue
ref. d now refers to the temporary created by "a+b" and modified by
"+= c". The life time of that temporary ends at the semicolon after
"c". So d is a dangling reference.

Now consider:

string operator+(const string&, const string&);
string operator+(string&& x, const string& y) {x += y; return
move(x);}

const string& d = a + b + c;

This is slightly more expensive because it involves an extra move
construction. However "a + b + c" now creates a temporary whose
lifetime is going to extend beyond the semicolon until the reference d
is destructed due to the language in 12.2 [class.temporary].

For the cost of an extra move construction (after an append) you can
sleep better at night. :-)

-Howard



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

general purpose RAII with closures

On Sep 12, 12:10 pm, JoshuaMaur...@gmail.com wrote:
> I want to be able to create an object whose constructor takes an in-
> place function definition as an argument (a closure, facilitated by
> boost::lambda or the newC++0x standard), and that function will be
> called when the object is destroyed.
>
> I want to create an object of that type on the stack and use
> boost::lambda (or the new inplace function definitions of C++0x) to
> give it a function which it will call when the object is destroyed
> from stack unwinding, a general purpose, ad hoc RAII if you will.
>



>
> Am I correct in concluding that the only way to get what I want is
> decl_type and/or auto in C++0x? I suddenly like auto that much more. I
> was just wondering if there was some hackery, possibly with macros, to
> get what I want in C++98. I believe not.

The solution for this problem is well known, google for ScopeGuard.

Here is a sketch (untested):

struct scope_base {};

template
struct scope_derived : scope_base {
F f;
~scope_derived() { f(); }
};

template
scope_derived at_unwind(F f) {
scope_derived r = {f};
return r;
}

// usage:

{
handle * h = allocateHandle();
scope_base const& guard = at_unwind(bind(deallocateHandle,h));
}


Basically it relies on lifetime extension of temporaries.

HTH,

--
gpd

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google

Test for NaNs?

On Sep 12, 3:28 am, Triple-DES wrote:
> On 11 Sep, 18:47, Alan McKenney wrote:
[snip]
> Probably not, but the standard states that is_iec559 should be true
> IFF the type adheres to the IEC 559 standard. My interpretation is
> that this also covers the semantics.
>
> > On a tangent, at one time, the C standard allowed
> > compilers to apply algebraic transformations, such
> > as converting "( a + b ) - a" to "b"
> > or "(a+b)*c" to "a*c + b*c". This broke a lot of
> > numerical code.
>
> > Does the C++ (and the C) standard allow this sort of
> > rewriting?
>
> In general, yes. When discussing compiler optimizations, the "as-if"
> rule is often mentioned. Simply put, an implementation can more or
> less do what it wants, as long as the result is as-if all the
> requirements of the standard were obeyed.
>
> In practice, the compiler will of course often get this wrong and
> apply optimizations that change the semantics of the program.

Do compilers actually do this? In which case, what use is a quiet NAN
if it's semantics will be optimized away?

I will also call shenanigans on your claim that compiler optimizations
frequently change the semantics of programs. I think people would be
up in arms if compiler optimizations broke their programs. Instead,
people just tend to turn on the standard optimizations and assume
it'll work as it's supposed to. I suppose NAN broken optimizations are
an exception, and the standard specifically allows other exceptions
such as copy constructor elision.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google