|
Avenging Dentist posted:Well, I mean that, and the inverse: using a variadic template as a template parameters to take non-variadic template types as arguments. i.e.: I doubt it. That would imply that from the context of foo, T should be expected to be able to take any number of template arguments, which std::map cannot.
|
# ? Aug 25, 2008 22:19 |
|
|
# ? May 18, 2024 03:11 |
|
I was messing around today and I realized that you can make template unions. code:
|
# ? Aug 26, 2008 02:06 |
|
Avenging Dentist posted:I was messing around today and I realized that you can make template unions. C++: "I can't think of a use case right now, but maybe it will be helpful later. Put it in the standard."
|
# ? Aug 26, 2008 02:33 |
|
Scaevolus posted:C++: "I can't think of a use case right now, but maybe it will be helpful later. Put it in the standard." Use case: variants
|
# ? Aug 26, 2008 02:37 |
|
Doing about anything with that union is an aliasing violation. Edit: Well, okay, only in one direction.
|
# ? Aug 26, 2008 03:28 |
|
Vanadium posted:Doing about anything with that union is an aliasing violation. No it's not; the whole point of unions is to avoid strict-aliasing (not aliasing) violations. You probably mean it's undefined to write to a union member and to read from another one (mostly).
|
# ? Aug 26, 2008 11:06 |
|
This function is supposed to take a null-terminated array of characters (c-string). It takes every character added, compares it to a boolean array size 255, and checks if the memory location where the character would be is true or not (ie, to see if the character is not already in the array - if it's false, that character has not been added yet. if it's true, it has). However, even a simple sentence such as "The quick brown fox jumps over the lazy brown dog." will produce output like this (I added a cout statement in the if loop to see what exactly it was taking in). T h e q u i c k b r o w n f x j m p s v t l a z y d g . E 2 " # $ ( ` ! ð How can I get rid of all that extra crap after the period? Shouldn't it stop taking characters after the period? The syntax to get that is just test.insert("The quick brown fox jumps over the lazy dog."); code:
|
# ? Aug 31, 2008 01:29 |
|
Short answer: you are looping 255 times, instead of once per character in the string. Consider what would happen if you had a string with 255 'a's followed by every other letter, or if you had a string (as is the case here) with less than 255 characters. Furthermore, your indentation is mildly headache-inducing, the function calls you're making look like you're inappropriately abstracting in the wrong places, that's obviously not the code you ran to test with because that doesn't have any IO statements in it, and you're doing C string handling with C++. None of these are major problems, but you may want to ask your TA for a "perfect" solution after you hand in this assignment so you can see how they would structure the problem. Edit: My solution: code:
ShoulderDaemon fucked around with this message at 02:33 on Aug 31, 2008 |
# ? Aug 31, 2008 02:22 |
|
ShoulderDaemon posted:Short answer: you are looping 255 times, instead of once per character in the string. Consider what would happen if you had a string with 255 'a's followed by every other letter, or if you had a string (as is the case here) with less than 255 characters. You're right, I just didn't know how to get the length of a dynamically allocated character array - everywhere I searched online said there was no way to do this if you don't know what it'll be at compile time. quote:Furthermore, your indentation is mildly headache-inducing, Whoa, it looked okay on my screen before I posted it quote:that's obviously not the code you ran to test with because that doesn't have any IO statements in it, This is part of a class, I mentioned the function call was test.insert("The quick brown fox jumps over the lazy dog."); and that I stuck a cout statement in the if loop to see what exactly it was taking from the input sentence. I can't change the function parameters, it has to remain a character array. But otherwise, you really have helped me out a bunch. I'm very rusty on C++, so thank you for being patient with my retarded-looking code and vague questions.
|
# ? Aug 31, 2008 04:24 |
|
Whilst farting I posted:You're right, I just didn't know how to get the length of a dynamically allocated character array - everywhere I searched online said there was no way to do this if you don't know what it'll be at compile time. Yep, can't be done. You just loop until you hit a nul character. I use the pointer type so that I can use while ( *str ) { ... ++str }, but if you want an explicit index you'd use something like for ( int i = 0; str[i]; ++i ) { ... }. If you want to find the length of a C-string at runtime, you use strlen(3). Note that all these methods only work under the assumption that this is a well-formed C-string; if you're passed something that doesn't have a terminating nul, the program will crash or have some more subtle bug. Whilst farting I posted:Whoa, it looked okay on my screen before I posted it The only really terrible bit was the extra layer of indentation after charToInsert = arr[i]. Whilst farting I posted:This is part of a class, I mentioned the function call was test.insert("The quick brown fox jumps over the lazy dog."); and that I stuck a cout statement in the if loop to see what exactly it was taking from the input sentence. It's usually considered bad form to post code that isn't exactly the code you're testing, because if you're doing something stupid while inserting the missing debugging code, then no-one will be able to recreate the error you see, and everyone just gets irritated at eachother. In your case, it wasn't a big deal because your problem was elsewhere, but you should get in the habit of not making people guess what you're doing. Whilst farting I posted:I can't change the function parameters, it has to remain a character array. But otherwise, you really have helped me out a bunch. I'm very rusty on C++, so thank you for being patient with my retarded-looking code and vague questions. My function would work with the same parameters yours works with, for what that's worth. Arrays are passed in C and C++ as the address of the first element, so you can treat them as pointers to a single element and explicitly manage the pointer arithmetic. As you're still learning, you shouldn't do this unless you're already comfortable with pointer arithmetic, but at some point during your class there should be a discussion about passing semantics and the array/pointer pseudo-equivalence.
|
# ? Aug 31, 2008 04:47 |
|
Whilst farting I posted:Whoa, it looked okay on my screen before I posted it You have your tab size set to something small, so every time you hit tab it looks like it indents 2 or 4 or whatever characters. But it actually just put one tab character in, so when it got posted to the board with a tab size of 8 the indentation was huge.
|
# ? Aug 31, 2008 15:42 |
|
Also, instead ofcode:
code:
|
# ? Sep 1, 2008 20:29 |
|
ShoulderDaemon posted:Yep, can't be done. You just loop until you hit a nul character. I use the pointer type so that I can use while ( *str ) { ... ++str }, but if you want an explicit index you'd use something like for ( int i = 0; str[i]; ++i ) { ... }. If you want to find the length of a C-string at runtime, you use strlen(3). Note that all these methods only work under the assumption that this is a well-formed C-string; if you're passed something that doesn't have a terminating nul, the program will crash or have some more subtle bug. Thanks for this - it's hard to find really specific information of this nature. Also, I feel like a dope for completely forgetting you could just do something like using arr[i] as a conditional quote:It's usually considered bad form to post code that isn't exactly the code you're testing, because if you're doing something stupid while inserting the missing debugging code, then no-one will be able to recreate the error you see, and everyone just gets irritated at eachother. In your case, it wasn't a big deal because your problem was elsewhere, but you should get in the habit of not making people guess what you're doing. I had isolated my problem to that function but in the future if I'm having issues I'll post the full code. quote:My function would work with the same parameters yours works with, for what that's worth. Arrays are passed in C and C++ as the address of the first element, so you can treat them as pointers to a single element and explicitly manage the pointer arithmetic. As you're still learning, you shouldn't do this unless you're already comfortable with pointer arithmetic, but at some point during your class there should be a discussion about passing semantics and the array/pointer pseudo-equivalence. I'm used to Java, believe it or not I've taken several courses that involve heavy C++ but pointers are always the things that screw me up, no matter how much reading I do or how many examples I see. This post has really been informative though, so thanks for all your help and patience! Adhemar posted:Also, instead of I wound up tightening my code to this later - I was more concerned with functionality over style when I posted it (obviously). Thanks for the tip!
|
# ? Sep 2, 2008 02:09 |
|
|
# ? Sep 2, 2008 02:20 |
|
Adhemar posted:Also, instead of Does it actually compile differently or?
|
# ? Sep 3, 2008 17:25 |
|
tyrelhill posted:Does it actually compile differently or? I've worked with people who prefer the '== false' on negative conditions, but omit the '== true' for positive conditions, on the grounds that it's harder to misread compared to the possibility of not spotting a small '!' tacked on to the front.
|
# ? Sep 3, 2008 17:40 |
|
TSDK posted:Ignoring possible overloads, then it should compile to identical code. I'm in this boat. Unless it's obvious that you're going to be checking that something is false, I use == false.
|
# ? Sep 3, 2008 18:11 |
|
Ugg boots posted:I'm in this boat. Unless it's obvious that you're going to be checking that something is false, I use == false. Yeah me too, I was worried that maybe it was less efficient.
|
# ? Sep 3, 2008 20:16 |
|
So why is it that Boost doesn't have a proper, standalone singleton library yet? I don't want to have to rip off another Alexandrescu design and roll my own again, darnit <>
|
# ? Sep 8, 2008 03:46 |
|
There are talks of a singleton library every once in a while and I think one or two went up for review without success. If you search through the mailing list archives you could probably see what the problems were and you might even find an implementation you can use even though it did not make it through review. Edit: There were two that went up for review Jason Hise's in May 2005 Tobias Schwinger's in January 2008 I'd imagine that Tobias will resubmit his library after he makes it more generic, but I haven't heard anything about it recently. Edit2: Haha look at this guy http://john.torjo.com/ (the review manager for the second singleton lib) Edit3: This guy rules http://torjo.blogspot.com/ That Turkey Story fucked around with this message at 04:21 on Sep 8, 2008 |
# ? Sep 8, 2008 04:05 |
|
The Red Baron posted:So why is it that Boost doesn't have a proper, standalone singleton library yet? I don't want to have to rip off another Alexandrescu design and roll my own again, darnit <> Probably because singletons are next to trivial to implement?
|
# ? Sep 8, 2008 04:34 |
|
That Turkey Story posted:There are talks of a singleton library every once in a while and I think one or two went up for review without success. If you search through the mailing list archives you could probably see what the problems were and you might even find an implementation you can use even though it did not make it through review. Thanks! I'll have a look at those quote:Probably because singletons are next to trivial to implement? Indeed, but as there are several gotchas with trivial implementations such as static vars (synchronization, lifetime and dependecy issues etc), I'd say there is definitely room for a generic, peer-reviewed library for the purpose
|
# ? Sep 8, 2008 04:52 |
|
Not a C question really but I wanna know how to set up vim to be a pretty good IDE for C programming. I saw a website out there describing out to set up vim for python, and had all these neat things like a debugger, code completion, COLOR, and so forth. I'm starting to like vim (even though I don't have a handle on all the commands and stuff yet), and since I'm going to be using vim for the entire semester, I'd like to configure it a bit for C. You guys got anything?
|
# ? Sep 9, 2008 02:44 |
|
I've been using system("start blah.exe") to open external programs, but doing so seems to stall my program until the program called is terminated.. How can I start another program without giving it control? I'd rather avoid .net if possible. EDIT: I figured it out, winExec is what I was looking for. FearIt fucked around with this message at 07:56 on Sep 9, 2008 |
# ? Sep 9, 2008 07:53 |
|
ShellExecute
|
# ? Sep 9, 2008 07:55 |
|
ShinAli posted:Not a C question really but I wanna know how to set up vim to be a pretty good IDE for C programming. cscope integration is neat ":help cs"
|
# ? Sep 9, 2008 11:32 |
|
ShinAli posted:Not a C question really but I wanna know how to set up vim to be a pretty good IDE for C programming. These are my C specific settings: autocmd BufRead,BufNewFile *.c,*.h,*.cc,*.cxx,*.hxx set cindent formatoptions=corq cinoptions=:0(0 textwidth=72 sm comments=sr:/*,rmb:*,exr:*/ That does auto-indentation with lining up the way I like it, paren/brace/bracket-matching, automatic comment formatting and wrapping (ie, you write /*<enter>, it fills in " * " for you, etc). Colors is just :syntax on. I don't use make integration and that stuff much, but see :help make.
|
# ? Sep 9, 2008 13:38 |
|
FearIt posted:I've been using system("start blah.exe") to open external programs, but doing so seems to stall my program until the program called is terminated.. Maybe CreateProcess would be worth a look as well
|
# ? Sep 9, 2008 17:34 |
|
I'd like to have an asynchronous deadline timer. There's very little I need: 1. I'd like to be able to say: "run this function in x seconds, and don't block while you wait." 2. Assuming I've done #1 above, but x seconds haven't passed, I'd like to be able to say: "ok, nevermind. Stop waiting and don't run that function." I'd love to use Boost.Asio, but I can't. I may have mentioned not being able to use boost in the past, that's changed recently. Now I'm allowed to use boost 1.32.0 But of course Asio isn't available in that version Does anyone know of any other C/C++ libraries that could do this. I tried using libevent but it didn't really suit my needs. libevent assumes some kind of "main loop" that their event_dispatch() will be called from, but I can't be tied down to that paradigm. My boss implemented a solution to provide this sort of functionality some years ago, but it's nasty the time has come where we really need to redesign it from the ground up. So I'd like to know if there's already a well made wheel out there (besides Asio) before I try and roll my own.
|
# ? Sep 9, 2008 21:01 |
|
Lexical Unit posted:I'd like to have an asynchronous deadline timer. There's very little I need: On Unix you can use alarm() or setitimer() and catch SIGALRM, but that not be flexible enough, since you can't set more than one timer.
|
# ? Sep 10, 2008 04:21 |
|
Unix gets all the cool toys: kqueue, setitimer, ZFS... I guess I should have mentioned I'm on RedHat Linux. Thanks for the suggestion though. I'm not sure if either of those are on RHEL4AS3, but if alarm is, it might be good enough for my immediate needs. If so I could use it for now while I consider more flexible/robust options.
|
# ? Sep 10, 2008 04:37 |
|
alarm() is POSIX, so any recent RHEL should have it. Keep in mind that, like threads, signal handlers have their own set of limitations and caveats that you might want to brush up on, depending on what your function is doing.
|
# ? Sep 10, 2008 04:52 |
|
Mustach posted:alarm() is POSIX, so any recent RHEL should have it. Keep in mind that, like threads, signal handlers have their own set of limitations and caveats that you might want to brush up on, depending on what your function is doing. alarm(3) man page posted:HISTORY
|
# ? Sep 10, 2008 06:39 |
|
timer_create()/timer_settime()/etc. are a whole lot more useful than alarm() or setitimer().
|
# ? Sep 10, 2008 10:10 |
|
more falafel please posted:
|
# ? Sep 10, 2008 12:40 |
|
Hi, I'm using the windows.h winapi and I'm trying to figure out how to find all of the hwnd's of windows with class "XYZ". Anyone have a clue how I could do this?
|
# ? Sep 11, 2008 00:01 |
|
FearIt posted:Hi, I'm using the windows.h winapi and I'm trying to figure out how to find all of the hwnd's of windows with class "XYZ". FindWindowEx or EnumWindows Pooball fucked around with this message at 02:55 on Sep 11, 2008 |
# ? Sep 11, 2008 02:53 |
|
Okay so I'm going to uni soon to do Computer Science & Cybernetics and in the 3 (or 4, hopefully) years I'm hoping to a whole multitude of things that I'm sure will be a mix of a lot of languages. I have a lot of ideas about AI, evolution simulation, robotic control, VR/stereoscopic games - interacting at all "levels" from hardware to high level software, that I just can't wait to start putting to the test, and for the core "logic" of most of these it seems like the most obvious option is C++ as a lot of it will be really pushing PCs so needs to be a low level language, and for this reason I *understand* that python would not be as effective, but supporting of objects and various stuff C apparently doesn't have (although I haven't ruled out other languages) also the VR stuff I intend to be doing as mods to games (eg Source/UT4 engine eye tracking based stereoscopic focus will be something that will hopefully be allowed by the bridge between hardware and software). I know this is not beyond me, but it *will* mean I most likely have to do a lot of independent learning outside of classes / lectures so I want to start learning C++ as soon as possible. However, with most tutorial sites I learn how to make a simple "hello world" app and then look at another site which does it differently or uses a different standard library (which is confusing as hell). I'm not *so* concerned about visual things at the moment as I expect that I'd be building any interfaces or graphical representations in a simpler language (Visual Basic Gui arf). What is the difference between Visual C++ and straight up C++? I can imagine I'll be working with various environments...Linux, Windows, embedded chips :O Syntax is not an issue, as I've a lot of experience in PHP and dabbled VB, Basic, Pascal, C, ASM, Java etc, it is about learning how the language does things but I find it hard to sit and read through an ebook as my attention span plummets if I am not doing things and learning and understanding things as I learn interactively to a degree. I will have a (small) budget for books after I get my Student Loan (I'd count this as a worthwhile expense for my learning). Is there a reverse version of this? http://bisqwit.iki.fi/story/howto/php/ Where should I start? Are there any good goon recommended free tutorial sites I can busy myself with before I sink money into a good book? Thanks! Rat Supremacy fucked around with this message at 14:09 on Sep 11, 2008 |
# ? Sep 11, 2008 14:06 |
|
haywire posted:What is the difference between Visual C++ and straight up C++? I can imagine I'll be working with various environments...Linux, Windows, embedded chips :O Visual C++ is Microsoft's C++ development environment for Windows. Its compiler uses the same language as other C++ compilers (though it provides a bunch of extensions), and people seem to like it. You will probably have to use separate compilers to compile for your embedded platforms but you should be able to actually write and edit code with Visual C++. quote:Where should I start? Are there any good goon recommended free tutorial sites I can busy myself with before I sink money into a good book? I cannot make an official goon recommendation, but "Thinking in C++" is a book by Bruce Eckel that is readable for free on the web. It kind of assumes you are vaguely aware of how programming works and goes on to explain the C++ stuff. It is a bit OOP centered but probably a decent starting place. Also, most people agree that C++ tutorials you find on the web tend to suck.
|
# ? Sep 11, 2008 14:26 |
|
|
# ? May 18, 2024 03:11 |
|
haywire posted:What is the difference between Visual C++ and straight up C++? I can imagine I'll be working with various environments...Linux, Windows, embedded chips :O I know you said you don't want to buy a book just yet, but once you do (and since you say you've got programming experience) you can't go wrong with The C++ Programming Language. The author invented the language, and aside from covering just about everything in standard C++, it's got plenty of exercises at the end of each chapter. A "beginner-level" book that gets recommended a lot in this forum is "Accelerated C++", but I've never read it. I'll second "Thinking in C++" as a decent web book. A good quick reference for the Standard Library is Apache's documentation. Websites that come up frequently in searches but should be avoided are cplusplus.com and codeproject. Especially cplusplus.com; that site is a bane on the Internet. At least codeproject has some diamonds in its rough. cplusplus.com has nothing worthwhile. Anything decent that it offers is small fries compared to the abundance of misinformation, bad practices, and platform-specific code attributed as standard C++. Mustach fucked around with this message at 14:52 on Sep 11, 2008 |
# ? Sep 11, 2008 14:43 |