Friday, August 23, 2013

Strongly-typed non-scoped enums in C++11?

Newsgroup: comp.lang.c++

Subject: Strongly-typed non-scoped enums in C++11?

From: Paavo Helde <myfirstname@...>

Date: Fri, 23 Aug 2013 02:28:33 -0500





In C++11, you can have 'enum class abc' which is strongly-typed and

scoped, and 'enum abc: type' which are not scoped, but also not

strongly-typed. However, I would like to have a non-scoped strongly-typed

enum, is this possible somehow?



Background: I would like to have strongly-typed enums in order to find

out and fix suspicious comparisons and conversions to integers. However,

only one of our build machines has a compiler version which supports C++

11 enums, a legacy enum is needed for other builds. For the diagnostics

point of view it is fine if it works only in one build, so I thought

doing something like:



#ifdef HAS_C11_STRONGLY_TYPED_ENUMS

enum class abc {

#else

enum abc {

#endif

val1,

val2,

....

};



Alas, now all the codebase must be changed to use the names abc::val1,

abc::val2 in the C11 branch, and must not use the prefix in the legacy

branch. This seems to kill the whole benefit of this approach. Any ideas?



TIA

Paavo







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?72045-Strongly-typed-non-scoped-enums-in-C-11&goto=newpost

View all the progranning help forums at:

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

passing by reference

Newsgroup: comp.lang.c++

Subject: passing by reference

From: bob smith <bob@...>

Date: Thu, 22 Aug 2013 22:05:28 -0700 (PDT)



How does passing by reference work behind the scenes?



Does the compiler secretly pass in a pointer to the object?



Thanks.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?72005-passing-by-reference&goto=newpost

View all the progranning help forums at:

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

Thursday, August 22, 2013

A Better Container Choice?

Newsgroup: comp.lang.c++

Subject: A Better Container Choice?

From: mrc2323@...

Date: Thu, 22 Aug 2013 12:52:37 -0700



My current application has 2 large data sets that are combined into a

single data set that I must access by (part of) a string value.

Currently I have the structure declared as a map object, but after

populating the basic information I am adding information from another

database that's much larger - in a many-to-one situation.

Here's the fundamental information I use:

struct Res_Struct // Individual Event Finisher data

{

int resEvtNum; // link to Events table

int resYear; // Event Year

int resOAll; // OverAll Finish position

int resD_P; // Division Place

long resTime; // Finish Time

} resWork;

struct Hist_Fins // individual Finisher's results

{

int evtNum; // Result's Event # link

string PRF; // P/R indicator

Res_Struct histInfo; // Finisher's result(s) info

} histWork;

vector<Hist_Fins>::iterator hIter;

struct Fin_Struct // Individual Finisher data

{

long finLink; // unique Finisher (link)

char finGender; // gender

int finCount; // # Finishes by this participant

string finName; // Finisher Name (Last, First M.)

string finDoB; // (derived) DoB from event Age/Year

vector<Hist_Fins> histVect;

} finWork;

map<int, Fin_Struct> finMap;

map<int, Fin_Struct>::iterator fIter;



Yes, this seems a bit convoluted, but the application has been

growing in size and complexity, and I've not had time to redesign...

The important issue here is that I have ~160,000 records that

construct the basic information in the Fin_Struct. My other data (~

400,000 records) comprise the information that populates the "histVect"

object - 1-200 vector items in each map object. The input data files

are flat text data files (referencing some earlier posts about file I/o

efficiency).

Note that the map has an integer key value, and values range from 101

through ~160,000. I don't use the "name" as a key because I normally

scan the entire map object to look for objects that match some part of

the name value (e.g. I want to find all objects with names that start

with "WAL", etc.).

The use of an STL map doesn't seem best, because I don't use the map

in a traditional way, and the loading of the map takes a lot of time

<sigh>. Since the data objects are consecutive in an integer range, I

wonder if another container would be a better choice. I could use a

vector (and reserve a good amount of space "going in", rather than let

slow runtime grow occur), but I think I'd lose significant "load time"

by not referencing a map as I'd have to scan the vector 400,000 or more

times during the 2nd file population...

Both files contain the integer value that links them, as well as the

"name" string.

Any thoughts? TIA







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?71655-A-Better-Container-Choice&goto=newpost

View all the progranning help forums at:

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

Tuesday, August 20, 2013

Singleton class in C++

Newsgroup: comp.lang.c++

Subject: Singleton class in C++

From: bob smith <bob@...>

Date: Mon, 19 Aug 2013 17:54:40 -0700 (PDT)



If someone asks you to write a Singleton class in C++, can someone help me see what the answer would be?



In particular, I don't know if the getInstance() function ought to return Singleton* or Singleton&.



I'm also concerned about when and how the memory for this class will be deallocated.



Thanks.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?69346-Singleton-class-in-C&goto=newpost

View all the progranning help forums at:

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

Monday, August 19, 2013

When might it make sense to use inheritance when templates (compile-time polymorphism) is enough?

Newsgroup: comp.lang.c++

Subject: When might it make sense to use inheritance when templates (compile-time polymorphism) is enough?

From: "K. Frank" <kfrank29.c@...>

Date: Sat, 17 Aug 2013 14:37:17 -0700 (PDT)



Hello Group!



Prior to templates, if you wanted to reuse code across

different types, you would use inheritance / polymorphism

where your different types would derive from a common

base type that offered the capabilities needed by your

reusable code.



Now (with templates) if you know at compile time which

objects are of which concrete types you can use templates.

(If the concrete type of an object is only determined at

run time, you still need inheritance.)



This is kind of a soft question, but I'm wondering whether

there are situations where inheritance would still be

preferable, even when concrete types are known at compile

time, and a template solution could have been used.



Assume for this question that the code is special purpose,

so we're not trying to write some general, open-ended

library. That is, the code will be reused across a

smallish number of different types that are all being

designed together, and will not be extended to new types

in the future.



Also assume that efficiency isn't a concern, so that we

don't care about the cost of vtables or the cost (size)

of duplicated template code.



To illustrate my question, below is a simple, do-nothing

example that uses both inheritance and templates for

compile-time polymorphism. printPrintable is an

inheritance-based polymorphic function, while

printHasPrintMe is a generic template function.





Thanks for any thoughts and wisdom.





K. Frank





==========



#include <iostream>



class Printable {

public:

virtual void printMePoly() const = 0;

};



class A : public Printable {

public:

void printMePoly() const { std::cout << "aValue_ = " << aValue_ << std::endl; }

int aValue_ = 13;

};



class B {

public:

void printMeGen() const { std::cout << "bValue_ = " << bValue_ << std::endl; }

int bValue_ = 17;

};



void printPrintable (const Printable& p) {

p.printMePoly();

}



template<typename T> void printHasPrintMe (const T& p) {

p.printMeGen();

}



int main (int argc, char *argv[]) {

A a;

B b;

printPrintable (a);

printHasPrintMe (b);

}



==========







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?67951-When-might-it-make-sense-to-use-inheritance-when-templates-(compile-time-polymorphism)-is-enough&goto=newpost

View all the progranning help forums at:

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

Sunday, August 18, 2013

Initializing member references to dummy member variables

Newsgroup: comp.lang.c++

Subject: Initializing member references to dummy member variables

From: "K. Frank" <kfrank29.c@...>

Date: Sun, 11 Aug 2013 20:29:56 -0700 (PDT)



Hello Group!



Suppose I have a class that has a member reference

variable, and sometimes I want to initialize it,

and sometimes I don't. How much sense does it make

to initialize it to a dummy member variable (to shut

the compiler up)?



As a concrete example, let's say I have a class that

is constructed with a value of or reference to some kind

of handle, but the handle might be a number or it might

be a string:



struct Handle {

Handle (int iHandle) :

iHandle_(iHandle),

sHandle_(dummy),

useIHandle_(true)

{}

Handle (const std::string& sHandle) :

iHandle_(0),

sHandle_(sHandle),

useIHandle_(false)

{}

const int iHandle_;

const std::string& sHandle_;

bool useIHandle_;

std::string dummy_;

}



The point is that the member reference variable sHandle_

is supposed to be initialized (in the constructors'

initialization lists), whether or not it's actually

going to be used. The constructor that takes an int

argument doesn't have any std::strings floating

around with which to initialize sHandle_, hence the

introduction of the member variable dummy_.



Is this a reasonable approach? Is there an established

idiom for doing this kind of thing?





Thanks for any wisdom.





K. Frank







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?63071-Initializing-member-references-to-dummy-member-variables&goto=newpost

View all the progranning help forums at:

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

Saturday, August 17, 2013

Selecting Container for "Top 20" Application

Newsgroup: comp.lang.c++

Subject: Selecting Container for "Top 20" Application

From: mrc2323@...

Date: Sat, 17 Aug 2013 12:07:18 -0700



I am looking for an STL container that will efficiently handle a "Top

20" list. Specifically, I have >300,000 data records that I must scan

to find and store the highest 20 values.

None of the available STL containers seems well suited for this

application. For example:

- array, even though fixed in size, seems cumbersome to use: no delete

or erase function is available; insertion at some point is difficult;

etc. Replacement of the "end value" and sorting might work, but it's

tedious and slow...

- vector, while having insert & erase functions, incurs repeated size

expansion (expensive!) and truncation to maintain a fixed size.

Appending a new value if it's greater than the 20th element, followed by

sorting and truncation might be less work, but it might be very slow to

execute.

- queue/deque, set, and stack seem inappropriate for this application,

and the others (map, list, etc.) are completely unusable here.

Am I missing something? Is/are there reasonably efficient ways to

use array or vector that are appropriate? TIA







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?67887-Selecting-Container-for-Top-20-Application&goto=newpost

View all the progranning help forums at:

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