Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
xPanda
Feb 6, 2003

Was that me or the door?

rjmccall posted:

Hilariously, standard C++ doesn't let you specialize member templates of non-fully-specialized templates. The only compliant solution is to hoist the member templates out of the class and specialize them there; for example, you would need to have:

code:
template <class T, unsigned i, unsigned n> struct multi_array_return_value {
  typedef T* return_type;
  typedef const T* const_return_type;
};

template <class T, unsigned n> struct multi_array_return_value<T, n, n> {
  typedef T& return_type;
  typedef const T& const_return_type;
};
etc.

Hmm, I think I see, I'll give it a go.

Is there a reason the standard is this way and not the way MSVC permits? It seems far more sensible and readable.

edit: Ok, I don't see it at all. What on earth am I meant to do? I'm having trouble seeing the logic here.

xPanda fucked around with this message at 10:10 on Jun 16, 2010

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
No good reason. There are some wierdnesses, but as long as it's understood that you can't specialize members along different dimensions than the enclosing template (†), everything stays reasonable. I don't know whether MSVC allows member specializations to be defined out-of-line or specifies what the interaction between specializations in the template and "standard" explicit member specializations is supposed to be, though.

† I mean like this:
code:
template <class T> class A {
  template <class U> void foo(U);
};

// This should not be well-formed.
template <class T> template <> void A<T*>::foo<>(T*) { ... }

// But after this it would be.
template <class T> class A<T*> {
  template <class U> void foo(U);
};

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"
In what little free time I have, I started playing around with llvm, specifically the Kaleidoscope tutorial they have on their website. Holy crap, it's a beautiful piece of software.

I want to familiarize myself with it and design my own toy language, but I've been out of the programming languages/compilers scene for a while (the last time I did anything I was playing with lex and yacc). Is there a highly recommended parser generator out there that generates C++ code? I know there's Boost.Spirit, but looking at some of the sample code makes my head hurt and I'm wondering how hideous it would look with a grammar of any significant size.

Skavoovee
Oct 2, 2006

by SA Support Robot
Hi! I've been trying to learn socket programming, but have run into a problem. I copied one of the server examples from http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html and have been using a data generator a friend of mine made a while ago. It connects once, all the data goes through, but when it gets to the accept() call in the while loop again I get a segmentation fault.

Here is my code http://codeviewer.org/view/code:fed and here is an example of what the data generator is sending (not sure if it's relevant, but whatevs):
code:
00000000001-0101010101010101010|2010-06-16 10:56:07.030374|1234567891234567891|1|1234567891234567891\n
It is 101 characters long, sent in plaintext, including the newline at the end.

I ran it through gdb, just to try to figure stuff out and here is the output from that: http://codeviewer.org/view/code:fee

Thanks, and if you need any more info from me please let me know

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Flobbster posted:

In what little free time I have, I started playing around with llvm, specifically the Kaleidoscope tutorial they have on their website. Holy crap, it's a beautiful piece of software.

I want to familiarize myself with it and design my own toy language, but I've been out of the programming languages/compilers scene for a while (the last time I did anything I was playing with lex and yacc). Is there a highly recommended parser generator out there that generates C++ code? I know there's Boost.Spirit, but looking at some of the sample code makes my head hurt and I'm wondering how hideous it would look with a grammar of any significant size.

I don't have any experience with spirit although people here seem to like it. You can use Bison+flex to make C++ parsers/scanners, though.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

xPanda posted:

edit: Ok, I don't see it at all. What on earth am I meant to do? I'm having trouble seeing the logic here.

Didn't see your edit before.

Under the standard, you cannot specialize a template in dependent context. So if you have a member template nested within a class template, you can only specialize it for one specific specialization of the outer template; you can't specialize it for, say, an entire pattern of specializations of the outer template ("pattern" meaning something like "primary template or partial specialization thereof"). The solution is to hoist the member template out of the class template, like I did with R, and then specialize it there. With member function templates this is actually really annoying/difficult/impossible, but unfortunately that's what's required.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Painless posted:

I don't have any experience with spirit although people here seem to like it. You can use Bison+flex to make C++ parsers/scanners, though.

Note that bison+flex may not be able to do everything you need (like if you wanted to write a parser for C++!), and Boost.Spirit has a lot of other built-in niceties that bison+flex may not have. It's been a while since I used bison+flex, but as I recall, they don't have stuff that automatically handles things like delimited lists or escape sequences in strings, both of which Spirit can do really easily.

Of course, if you mistype anything in Spirit, you are in for a world of pain, so it all depends on your tolerance for template errors. You could probably compile it under clang though, and you'd get some decent error messages, but I'm not sure how much that'd help.

RonniePudding
Oct 18, 2005

C# question here.

Is it possible to pass a list as an argument into a function? Or do I have to convert it to an array first?

code:
public void listFunc(List<myClass> tList){ //???
...
}

RonniePudding fucked around with this message at 00:49 on Jun 18, 2010

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Yes, you can pass an instance of any type to a function if the function accepts that type.

shrughes
Oct 11, 2008

(call/cc call/cc)

RonniePudding posted:

Is it possible to pass a list as an argument into a function? Or do I have to convert it to an array first?

Why didn't you just write the code, run it, and see if it works?

floWenoL
Oct 23, 2002

shrughes posted:

Why didn't you just write the code, run it, and see if it works?

Come on, don't expect too much; he can't even tell the difference between "C/C++" and "C#".

RonniePudding
Oct 18, 2005

Thanks for the help guys! Why post if you're just going to insult me? This thread is about helping isn't it?

There is no Small questions for C# thread, and most competent programmers know two languages anyway so that's why I posted here. Also, I did try to run this in my program, but it's in Unity and depending on what class I inherit from depends on what features are added (string, list). It did not run, and does not make sense to me why it would not run seeing as how I can use an array just fine. Is that a better question, thread gods?

RonniePudding fucked around with this message at 12:20 on Jun 18, 2010

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

RonniePudding posted:

There is no Small questions for C# thread

http://forums.somethingawful.com/showthread.php?threadid=2262300

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

RonniePudding posted:

Thanks for the help guys! Why post if you're just going to insult me? This thread is about helping isn't it?
Someone (me) already answered your question. No holds barred after that. If your question is specific to Unity, you should ask in the game development thread.

Jose Cuervo
Aug 25, 2004
I have the following code that does not work in the following way: If I enter a valid file name the first time, the file gets read in just fine. If I enter an invalid file name at first, and then a valid file name the second time, the code seems to enter the while (!readIn.eof()) { loop and never stop.

I cannot figure out why this is happening. Is there something stupid I am missing or doing?

code:
void readInData() {

	string fileName;
	char *dataFileName= new char[30];
	
	cout<< "Name of data source file (omit '.txt'): ";
	cin>> fileName;
	fileName += ".txt";
	strcpy_s(dataFileName, 30, fileName.c_str());
	cout<< dataFileName << endl;

	// Make sure the file exists. If it does, open it for reading.
	fstream readIn;
	readIn.open(dataFileName, ios::in);
	while (!readIn.is_open()) {
		cout<< "ERROR! File does not exist." << endl;
		cout<< "Name of data source file (omit '.txt'): ";
		cin>> fileName;
		fileName += ".txt";
		strcpy_s(dataFileName, 30, fileName.c_str());
		cout<< dataFileName << endl;
		readIn.open(dataFileName, ios::in);
	}
	
	char newLine[2000];
	vector<string> dataLines;
	int lineCount= 0;

	// Read lines into a vector of strings
	while (!readIn.eof()) {
		readIn.getline(newLine, 2000);
		dataLines.push_back(newLine);
		lineCount++;
	}
	readIn.close();

ctz
Feb 6, 2003

Jose Cuervo posted:

Is there something stupid I am missing or doing?

Yes, you're using iostreams -- possibly the most poorly designed programming interfaces in the history of computing.

(Real answer: predicate your loop on readIn.good() rather than !readIn.eof(). There are more problems possible with a stream than just EOF, and their incidence isn't consistent between implementations or defined properly in the C++ standard.)

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!
Call clear on your stream before the read loop.

Jose Cuervo
Aug 25, 2004

ctz posted:

Yes, you're using iostreams -- possibly the most poorly designed programming interfaces in the history of computing.

(Real answer: predicate your loop on readIn.good() rather than !readIn.eof(). There are more problems possible with a stream than just EOF, and their incidence isn't consistent between implementations or defined properly in the C++ standard.)

Thanks, that worked. Is there something else you suggest using instead of iostreams?

Standish
May 21, 2001

Jose Cuervo posted:

Thanks, that worked. Is there something else you suggest using instead of iostreams?

use the C stdio stuff -- fopen(), fread(), fgets(), printf(), fprintf() (but not gets()).

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Those suck too.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"
Does any language or framework have an I/O library that isn't at least 68% terrible? C-style I/O sucks, C++ iostreams suck, Java's I/O has sucked so many times that over time they've added about 10 different types of streams/readers/channels/descriptors that all suck (will NIO.2 finally get it right??)...

Maybe I'm just forgetting an obvious good one because the lovely ones stick out so much.

ToxicFrog
Apr 26, 2008


Plenty. Lua, python, and bash come to mind.

Can't think of any for C/++ though.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Perl!


while (my $line = <STDIN>) {
...
}

Vanadium
Jan 8, 2005

Line-based IO with iostreams is not that bad,

code:
std::string line;
std::vector<std::string> lines;

while (std::getline(std::cin, line))
  lines.push_back(line);
or whatever.

darkhand
Jan 18, 2010

This beard just won't do!
I am just now getting into the Boost library, is their File IO not very good either?

MagneticWombats
Aug 19, 2004
JUMP!
Is there a way to emulate Haskell-like argument deconstruction in C++ without doing something bad like using dynamic_cast? I'm trying to translate (the process of evaluating expressions at least) http://okmij.org/ftp/Haskell/Lambda_calc.lhs into C++ in an OOPy manner and am having a hard time translating things like

code:
> check_eta (L v (A t (Var v')))
>       | v == v' && (let (flag,_) = occurs t v in not flag) = t
My code to do this is sort of dumb- I have a visitor that has a flag that literally says, "did you call this from an apply?" so if the visitor visits an apply, it will then visit the (Var v') equivalent and the visitor, while in the Var will act differently than if it weren't called from the Apply.

I'm probably square peg round holing this, and if so, what would somebody else do?

Scrub code:
http://codepad.org/MG59J4Yh
Probably helpful too:
http://codepad.org/iHltsgUI

MagneticWombats fucked around with this message at 05:49 on Jun 19, 2010

shrughes
Oct 11, 2008

(call/cc call/cc)

MagneticWombats posted:

Is there a way to emulate Haskell-like argument deconstruction in C++ without doing something bad like using dynamic_cast?

Yes. Or kind of. See http://forums.somethingawful.com/showthread.php?threadid=2773485&userid=0&perpage=40&pagenumber=170#post377240191 and rjmccall's reply on the same page.

You might be able to do nested deconstruction with autoconversion hackery, see if that tr1::tie library mentioned in the link above can do it. It's probably just not worth bothering with nested deconstruction even if it's possible.

Note that this just gives you deconstruction, it doesn't give you pattern matching. Ultimately you'll just have to implement visitors all over the place or just forget about C++ entirely.

And don't follow some stupid rule that says you have to implement every visitor with a separate header file and cpp file.

shrughes fucked around with this message at 05:55 on Jun 19, 2010

MagneticWombats
Aug 19, 2004
JUMP!

shrughes posted:

Yes. Or kind of. See http://forums.somethingawful.com/showthread.php?threadid=2773485&userid=0&perpage=40&pagenumber=170#post377240191 and rjmccall's reply on the same page.

You might be able to do nested deconstruction with autoconversion hackery, see if that tr1::tie library mentioned in the link above can do it. It's probably just not worth bothering with nested deconstruction even if it's possible.

Note that this just gives you deconstruction, it doesn't give you pattern matching. Ultimately you'll just have to implement visitors all over the place or just forget about C++ entirely.

And don't follow some stupid rule that says you have to implement every visitor with a separate header file and cpp file.

I'm going to interpret this as, "don't". But ultimately, I want to have the same class hierarchy and the same tree structure representing the lambda calculus expressions. The real problem, I think, is that I need to dispatch based not only on type but also on what the objects themselves point to. Of course I could just hack in an enum that says that this term is a Lambda and this term is an Apply :v:

EDIT: I think a more direct approach would be to create a data structure to keep track of scope and then do a second pass looking at the applies and then associate the bound variables parameters and then some magic.

I'm thinking:
code:
(A (L (Var x 1) 
      (Var x 1))
   (Var x 0))
The associator visitor or function or method, while acting on Apply, will create a helper visitor that will work on the overall environment data structure and replace every instance of (Var x 1) into (Var x 0) (except the one in the argument position of Lambda). Or something. gently caress.

The problem for eta-reduction remains :smithicide:
code:
(L (Var x 0)
   (Apply ???
          (Var x 0)))
but at least I don't have to worry about beta and alpha reduction as much.

EDIT2:
Would it be bad form to just send a visitor that returns NULL if something isn't what you want it to be? For example, I have a CheckApplyVisitor that returns NULL if the thing visitor doesn't see an Apply.

MagneticWombats fucked around with this message at 16:12 on Jun 19, 2010

Modern Pragmatist
Aug 20, 2008
I'm working on a project where I need to basically hijack the sound card: read from the output buffer, apply a series of filters, then rewrite the altered output to the output buffer replacing the original input. Does anyone have any recommendations on what library to use to do this or know of any good tutorials.

Right now I am looking at PortAudio because it has good cross-platform compatibility but the examples don't deal with pulling data off of the sound card for processing, they all rely upon portaudio generated input data. Also, it doesn't help that their wiki appears to be broken.

shrughes
Oct 11, 2008

(call/cc call/cc)

MagneticWombats posted:

Would it be bad form to just send a visitor that returns NULL if something isn't what you want it to be? For example, I have a CheckApplyVisitor that returns NULL if the thing visitor doesn't see an Apply.

No (it would not be bad form). My personal preference would be to have it take an extra bool* or bool& parameter for the purpose, but hey.

MagneticWombats
Aug 19, 2004
JUMP!

shrughes posted:

No (it would not be bad form). My personal preference would be to have it take an extra bool* or bool& parameter for the purpose, but hey.

Or that.

EDIT:
Even then, would the general method I'm trying to use be what you would use to tackle this problem in C++?

MagneticWombats fucked around with this message at 06:57 on Jun 21, 2010

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.

Modern Pragmatist posted:

I'm working on a project where I need to basically hijack the sound card: read from the output buffer, apply a series of filters, then rewrite the altered output to the output buffer replacing the original input. Does anyone have any recommendations on what library to use to do this or know of any good tutorials.

Right now I am looking at PortAudio because it has good cross-platform compatibility but the examples don't deal with pulling data off of the sound card for processing, they all rely upon portaudio generated input data. Also, it doesn't help that their wiki appears to be broken.

I don't think there's any portable API that does what you're asking for, or that there's even an OS-level API to do that (on major OSes). If you really need to do exactly what you're describing in your post, you will probably have to add your own hooks to your OS's audio subsystem or your sound card's driver (you do have an open-source driver for your soundcard, right? ;)).

On the other hand, there might be another way to do what you're trying to do that doesn't involve hacking custom APIs into your OS. If you would post more details about your setup (OS and soundcard) and the ultimate goal you're trying to achieve we could probably point you in the right direction.

OddObserver
Apr 3, 2009

Mr.Radar posted:

On the other hand, there might be another way to do what you're trying to do that doesn't involve hacking custom APIs into your OS. If you would post more details about your setup (OS and soundcard) and the ultimate goal you're trying to achieve we could probably point you in the right direction.

On Linux, it might be doable user-level as some sort of an ALSA module (assuming apps use ALSA, but most can be configured to); but that might just be harder than messing with the sound driver.

polyfractal
Dec 20, 2004

Unwind my riddle.
I'm tossing around designs for some scientific computing software. The application will need to manage millions of (small) nodes and I'm afraid memory constraints may be a real problem. Should I avoid creating class instances because of associated object overhead and just stick to arrays of structs and functions to manipulate those arrays?

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
There's only going to be a difference in "object overhead" if your classes have virtual members. (Class instances can be stored in arrays, too)

Mustach fucked around with this message at 21:18 on Jun 21, 2010

polyfractal
Dec 20, 2004

Unwind my riddle.

Mustach posted:

There's only going to be a difference in "object overhead" if your classes have virtual members.

Oooooh, gotcha. Thanks. :)

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

polyfractal posted:

I'm tossing around designs for some scientific computing software. The application will need to manage millions of (small) nodes and I'm afraid memory constraints may be a real problem. Should I avoid creating class instances because of associated object overhead and just stick to arrays of structs and functions to manipulate those arrays?

What kind of "nodes" are we talking about here?

Mustach is right in general, but when scientific packages refer to "non-object-based" implementation, it's not because they use structs instead of classes. It's because the data for each node isn't stored all together. Example: if "nodes" are vertices, each node might just be an arbitrary handle that ends up being transformed into an index into an array that stores coordinate data. This can let you do cool stuff like return consecutive ranges of nodes using much less memory (i.e. return "start = 1, end = 100" instead of "[1, 2, 3, ..., 100]").

EDIT: in practice, these implementations are almost always slower than object-based ones, but also use considerably less memory, assuming you get clever.

Avenging Dentist fucked around with this message at 21:52 on Jun 21, 2010

polyfractal
Dec 20, 2004

Unwind my riddle.

Avenging Dentist posted:

What kind of "nodes" are we talking about here?

Mustach is right in general, but when scientific packages refer to "non-object-based" implementation, it's not because they use structs instead of classes. It's because the data for each node isn't stored all together. Example: if "nodes" are vertices, each node might just be an arbitrary handle that ends up being transformed into an index into an array that stores coordinate data. This can let you do cool stuff like return consecutive ranges of nodes using much less memory (i.e. return "start = 1, end = 100" instead of "[1, 2, 3, ..., 100]").

Erm, neurons :3:

I'm working on a biological neural network model. Each node is a neuron and contains information such as coordinate position and connection details. Some of the data is stereotyped (ie. all neurons of type X have a resting potential of Z), while other data is unique to that neuron. The program will work in two phases: the first phase generates the spatial positioning and connectivity of the neuron nodes. The second phase uses this spatial information to generate a wiring diagram and use that for neural computation junk.

My current design feels very naive and inefficient. It consists of a giant, gently caress-off array of neuron objects, spatially sorted by an Octree class. After the neurons are positioned, each neuron object gets an updated list of connection pointers.


I'm really struggling to work out an efficient design for this. I'm a competent programmer, but have never dealt with programs that demand huge amounts of memory or really efficient performance. Any help or reading material would be appreciated (for reference I'm an ex-computer science major turned biologist, so I know my biology better than my CS unfortunately).


Edit: I looked at structures like directed graphs, but don't feel they provide any benefit since I will never need to do stuff like sorting, shortest path or network flow. Really the only information I need to keep track of are 1) where is the node spatially 2) who does it connect to and 3) what are its internal properties. Is having a bigass list really the best way to accomplish this?

polyfractal fucked around with this message at 22:09 on Jun 21, 2010

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

polyfractal posted:

It consists of a giant, gently caress-off array of neuron objects,

This is fine if you don't need to add/remove neurons after the initial setup.

polyfractal posted:

spatially sorted by an Octree class.

There might be a better spatial partitioning, but if you're just dealing with point objects, octrees are probably pretty ok.

Adbot
ADBOT LOVES YOU

xPanda
Feb 6, 2003

Was that me or the door?
I have what I think is a pretty stupid question.

In my continuing effort to port a program from MSVC to GCC, I've come up against this type of error a number of times with this sort of code:
code:
localityScheme.add(LocalityStep(1, 40));
This will give the error "no matching function for call to ‘ChangeScheme<MonteCarlo::LocalityStep>::add(MonteCarlo::LocalityStep)’", and is seemingly fixed by changing the previous code to:
code:
LocalityStep ls1(1,40);
localityScheme.add(ls1);
This will not cause an error. This change is not necessary using MSVC, but is with GCC. The ChangeScheme.add method expects a reference to a class (void add(T& state);). Is there something I can change in the one-liner which will make it work with GCC and MSVC?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply