|
Let's say I want to write this code:code:
Also, in trying to get code like that shown above to compile I was meet with this error from gcc 4.0.0: code:
|
# ¿ Feb 17, 2008 23:47 |
|
|
# ¿ Apr 26, 2024 07:14 |
|
Hrm, that may be the case but then is this not correct?code:
I have my own my_container_insert_iterator that I can pass to merge() which does the trick just fine, but I'd like to nix it in favor of std::insert_iterator<my_container> for neatness's sake. I already have const_iterator and iterator defined for my class and they work fine with STL algorithms, it's just the standard insert iterator that's giving me guff.
|
# ¿ Feb 18, 2008 02:01 |
|
Another question (still haven't solved the first as of yet):code:
code:
|
# ¿ Feb 18, 2008 04:28 |
|
That Turkey Story posted:No. reference and const_reference are references to the value_type, not references to the container type. 1. Ok, there's an error when I try to call merge(). 2. It's this merge() call that's the problem on line 183 of main.cpp 3. In the STL code there's some operator*() calling a operator=() that either isn't defined or is bad. 4. One operator=() takes typename my_container::const_reference. 4a. Check that I have typedef for const_reference, I do, ok. 5. The other operator=() is just the copy-assignment operator for std::insert_iterator<my_container> 6. Huh... I don't get it. I've got const_reference typedefed, so what's the problem? It never occurred to me that my_container::const_reference was wrong, though I can see now how that would logically be something further to check. And this error: code:
And when you say "get used to reading the error messages" do you mean simply from continued experience, or is there a better way you can suggest?
|
# ¿ Feb 18, 2008 18:58 |
|
That Turkey Story posted:You read it almost fully and were just about there but then you stopped! Well, back to work! FYI, I'm working on a Modern-C++-y trie class that supports any type as keys (so long as the user provides policies for their key type, defaults of course work with std::string). I'm basically modeling it more or less after std::set(). I think boost's spirit library has already implemented something like this as an implementation detail, I think they use a ternary search tree though. Fun stuff.
|
# ¿ Feb 19, 2008 02:45 |
|
Let's say that now I want to make my container exception safe. I'm not sure where I can find information on what parts of the STL are exception safe (and at what level). If anyone can recommend a book or website that would help in this area please do. I have Sutter's Exceptional C++, just not on hand at the moment, so if that's good enough just let me know. I can go re-read it now that most of it isn't over my head. In the mean time my immediate quandary involves conceptually reassuring myself that the following is no-throw guarantee exception safe: code:
|
# ¿ Feb 22, 2008 00:40 |
|
Beautiful.
|
# ¿ Feb 22, 2008 02:31 |
|
code:
1: c isn't initialized so you don't have any idea what's in c at the moment. 2: You're testing the value of c, but you don't know what c is. This is bad. 3: c becomes the return value of getchar(). getchar() will not return until the end of file or end of line is reached. So in your example let's say you type in a word like "stuff." 4: Whatever character c is, show it. 5: Go back to the top, and 2: since c isn't a "." (it's a "s") we continue to line 3 again. 3: c becomes the next character from stdin, you've already given the next character before, it's "t". getchar() won't wait for you to hit enter this time because it doesn't need to. ... Eventually c becomes "." and we end the loop, having printed out "stuff" to stdout. The reason stdin works like this is because it's line-buffered, that's just how it works. Here's an ok discussion with some more information. fret logic posted:I'm in WinXP right now so I can't try it out
|
# ¿ Feb 28, 2008 07:31 |
|
In C++ I'm wondering what's standard operating procedure when defining a class's typedef for size_type. In other words, if I have a template class which is a container for typename T and I want to define container<T>::size_type, what's the best thing to do? I considered typedef typename T::size_type size_type; but that doesn't work if T is int, for example. So then I considered using std::char_traits somehow because typedef typename std::char_traits<T>::int_type size_type; seems to work, but that could just be coincidental. It looks like gcc's implementation of the STL mostly just uses std::size_t, but say I use typedef size_t size_name; and I have a method that returns a size_type and then the container is instantiated with some_crazy_string which uses some_crazy_size_type as it's some_crazy_string::size_type typedef and some_crazy_size_type is incompatible with std::size_t... then I've got a bug, right? Edit: Or, is the best option to add another template parameter called SizeT and make it default to typename T::size_type, then use typedef SizeT size_type;? Lexical Unit fucked around with this message at 00:57 on Mar 5, 2008 |
# ¿ Mar 5, 2008 00:39 |
|
Avenging Dentist posted:Just use std::size_t. I'm not 100% sure what you're worried about, but size_t is perfectly acceptable, and it's not like the STL avoids it because they were too lazy to add another template argument. code:
|
# ¿ Mar 5, 2008 01:56 |
|
Ah I see. These standard container typedefs have a me a bit confused because they seem a little inconsistent from my perspective. It seems like value_type, pointer, const_pointer, reference, and const_reference, are all directly related to the type of the items in a container whereas size_type oddly has nothing at all to do with the type of the items in a container, but rather apparently only the number said items in the container. I always get hung up on these little details that people seem to think are obvious...
|
# ¿ Mar 5, 2008 07:05 |
|
graffin posted:I'm using it in C++, in the context of a template for a binary tree based dictionary. The object is an element in a node. In the dictionary, I'm trying to test the element to determine if it is an int or string.
|
# ¿ Mar 20, 2008 20:27 |
|
elevatordeadline, you're code works fine on my 64bit MacBook Pro running OS X 10.5 with gcc version 4.0.1 and also on a 32bit RHEL4AS machine with gcc version 3.4.6. You might open up your inttypes.h file and see what's in there. You may not find a perfectly portable solution, unfortunately.
|
# ¿ Mar 20, 2008 23:24 |
|
I've been asking questions in this thread about this code I'm writing to help me learn to be a better C++ programmer for a while, here's what I've come up with thus far: trie.h main.cpp QUnit.h Edit: moved code from codepad.org to permanent hosting So who wants to tell me how absolutely terrible my design/style/code is? Lexical Unit fucked around with this message at 01:17 on Mar 26, 2008 |
# ¿ Mar 25, 2008 21:12 |
|
Ah good point, that hadn't crossed my mind. Originally I had just the lexical_cast version but then I thought, "shouldn't I be nice and support compiling when boost isn't installed?" and that prompted me to do what I did there. I'm not sure how I'd make the stringstream version throw the same exceptions in the same cases without basically re-writing boost::lexical_cast though. Do you think it would be better to just leave out all the conditional compiling and just flatly require boost?
|
# ¿ Mar 26, 2008 01:00 |
|
Avenging Dentist posted:Speaking of, have you been following the Trie discussion on the Boost developers list?
|
# ¿ Mar 26, 2008 01:21 |
|
Well, I'm already subscribed to the gcc, gcc-help, xorg, TAO, libstdc++, festival-talk, and boost-users lists, so a little more won't hurt. It gives me something to do during work down-time. Interesting that they're modeling it after std::map, I went with std::set instead. However I can certainly see why one modeled on map would be quite useful.
|
# ¿ Mar 26, 2008 16:09 |
|
atof takes a const char*, you're giving it a char. Beaten of course, I meant to hit preview and not post.
|
# ¿ Apr 15, 2008 21:35 |
|
Hopefully that's just a dumbed down snippet of code and the real code allocates memory to number and populates operand... at least, that's what I assumed.
|
# ¿ Apr 15, 2008 21:40 |
|
Is this:code:
code:
Edit: I mean, does it always get the same answer and does it have the same set of side effects and gotchas? Lexical Unit fucked around with this message at 21:52 on Apr 22, 2008 |
# ¿ Apr 22, 2008 21:50 |
|
Nope! Not without an equal sign after file_size in both examples See a running example here.
Lexical Unit fucked around with this message at 00:05 on Apr 23, 2008 |
# ¿ Apr 22, 2008 22:13 |
|
That's what I thought but I was just checking since it seems to work for everything I've thrown at it. There's also this site which says that this codecode:
Leading me to wonder if the pubseekoff() method has the same limitation, and if that limitation is due to the same internal buffer that in_avail() uses, which in turn leads me to wonder if maybe in_avail() really does work just as well as either seek(end)/seek(beg) methods.
|
# ¿ Apr 23, 2008 00:11 |
|
JoeNotCharles posted:why doesn't LongPacket just inherit from Packet Smackbilly posted:I was wondering if there is anything wrong with this Smackbilly posted:or if there is a better way to do this code:
Lexical Unit fucked around with this message at 22:51 on May 5, 2008 |
# ¿ May 5, 2008 22:48 |
|
Why does this assert?code:
Answer: "After reading the last available character, the extraction not only reads past the end of the input sequence; it also fails to extract the requested character. Hence, failbit is set in addition to eofbit." Lexical Unit fucked around with this message at 17:26 on May 6, 2008 |
# ¿ May 6, 2008 16:48 |
|
Smackbilly posted:But you will still need a constant recording how many valid values there are for the purpose of declaring the links[] array. Unfortunately there is no way to simply query an enum type and ask it how many valid values there are. At my job I always see this sort of thing: code:
|
# ¿ Jun 5, 2008 03:41 |
|
I'm by no means a git expert but I use it for personal projects sometimes. In the past I've simply used git checkout to "unedit" (BitKeeper terminology) files. However it appears there are better options: http://bryan-murdock.blogspot.com/2007/07/git-revert-is-not-equivalent-to-svn.html
|
# ¿ Jun 16, 2008 17:03 |
|
Paradoxish posted:TinyXML is something like five source/header files. I don't see how linking against it statically will add any dependencies at all since end-users won't have to do anything. It'll be part of your executable. You can even just make the drat thing part of your project. For large projects where multiple teams possibly working for different companies need to integrate code bases to provide some solution that's targeted for some specific hardware/software configuration (aka: platform), using code that isn't "default" [sic] (read: isn't speced to be part of the target platform) is a problem. Sure, you can just compile statically and provide your binaries that will run on the target platform for deployment, but that doesn't solve the issue of integration testing. For my projects it's often the case that multiple companies are coding with respect to some spec, but our projects don't integrate with each other until late in the game. At that point someone from our company has travel to another company to do integration testing. Which just means we compile and run our code in the other company's environment and get both of our systems up and running and see that they actually work together as the spec says they should. Obviously there's usually problems at this point as it's the first time either company has interacted with each others' components. So the problem with using some library that's not in the spec is that maybe you won't be able to compile your software during integration testing because some other company doesn't have that library installed. And as most often is the case that company would not be willing to install it just for your code, they'd expect you to conform to the spec instead. One possible solution to that would simply be to package all of the necessary code and libraries into your codebase so you essentially don't care if the other company has them installed or not, because you're taking them along with you. But then there's the issue of wether or not you really want to incorporate some third party's code into your codebase or not. In many situations this would entail a full audit of the third party code and require additional further audits each time the third party released a new version that you might want to incorporate. TL,DR: Inside I'm really crying about not being able to use boost where I work while I spout out half-assed justifications for it that I've heard from my boss.
|
# ¿ Jul 17, 2008 19:21 |
|
Ugg boots posted:How is this a problem from using "non-default" libraries? Instead of including a Header and CPP files in your project you include a Header and LIB files... Sucks.
|
# ¿ Jul 17, 2008 19:29 |
|
You're completely right in my estimation. However the situation is that every few years we get to use a new version of RHEL (that's recently been stamped with a seal of approval) and that implicitly OKs a number of packages/libraries for our consumption. The assumption being that all those libraries/packages that come with some defined configuration/installation of that version of RHEL have undergone some kind of audit (though I highly doubt this is the case or if it is the case I highly doubt the usefulness or completeness of those audits).
|
# ¿ Jul 17, 2008 19:44 |
|
Ok, so I'm using private inheritance and promoting the methods I want to expose in the base class with using directives (did I get those technical terms correct?) but I'm a bit confused about one thing. Say I want to expose only the const version of a method, how do I do that? This doesn't work: using base::operator[] const;, and using base::operator[]; exposes both the const and non-const methods. Is there a common workaround for this or is my syntax wrong? \/\/ Oh well, that's what I thought. I was kinda hoping there was some c++ dark magic I could use to accomplish this. Lexical Unit fucked around with this message at 20:27 on Jul 21, 2008 |
# ¿ Jul 21, 2008 16:57 |
|
When is it safe to use this idiom:code:
code:
|
# ¿ Jul 24, 2008 16:13 |
|
That Turkey Story posted:Edit: So in other words, yes, your second example is safe (though the first one isn't because the vector is empty). Also, I'm just curious if there's a better way to get a char** out of a std::vector<std::string> than just allocating a char** big enough to store the memory and filling it manually in a loop: code:
Lexical Unit fucked around with this message at 18:19 on Jul 25, 2008 |
# ¿ Jul 25, 2008 18:16 |
|
That Turkey Story posted:First, watch out because that code isn't exception safe! If any of those new operations in the for loop throw bad_alloc, you are leaking memory. That Turkey Story posted:Make sure you handle and rethrow or use RAII. A simple start is to make fruitloops a std::vector instead of a char** (or perhaps better, a smart pointer like boost::scoped_array if you have boost). That Turkey Story posted:Second, do you mean the API takes a char** or char const* const*? That Turkey Story posted:If it's just an API not using const correctly, as in it's taking const-unqualified data even though it's guaranteed to not modify the data, you can just make a std::vector or scoped_array of char const* and then const_cast when passing the data through the API.
|
# ¿ Jul 25, 2008 20:13 |
|
ShoulderDaemon posted:Have you considered using XCB instead of xlib? It's much nicer.
|
# ¿ Jul 25, 2008 20:25 |
|
Heh, I just wrote something like that (I didn't use the fancy call to std::transform()). I realized this was essentially the same thing with my question about the &vector[0] idiom. The pointer returned by c_str() is valid to use so long as the string remains unchanged, correct? I think I'm getting the hang of this... I hardly ever need to think about this because I'm usually not interfacing with C libraries. It seems to work just fine, thanks for all the pointers
|
# ¿ Jul 25, 2008 20:47 |
|
So I want to know how y'all feel about this approach. I need to Intern some Atoms in my program. You do this just once, it's like an initialization step. So we need to know beforehand which Atoms should be interned, and then we need to be able to reference these Atoms by name, but only this predefined list of Atoms should be able to be referenced. After we have the enumeration of available Atoms, we need to then intern them, and in doing so we obtain their Atom values. An Atom value is just an integer value that is determined at runtime. Then we can generate a mapping from Atom name to Atom value, and whenever a user wants a particular Atom value, he just looks it up by name in the map. Except we don't want the map to go from string Atom name to integer Atom value. We don't want that because we want to restrict the valid key values to only those Atom names which have been interned, not an arbitrary string. So here goes: code:
The only thing I don't like is the code that generates the map. Basically I'm iterating over the values in the enum, but it feels kind of hackish. Comments? Suggestions? Disapprobations? Lexical Unit fucked around with this message at 18:56 on Jul 28, 2008 |
# ¿ Jul 28, 2008 17:39 |
|
http://www.gidforums.com/t-7134.html Assignment is when you call operator=(), initialization is a constructor. Also, the code would produce the wrong output if you did as you say, and put p2 = c++; p1 = c; in the body of the constructor.
|
# ¿ Jul 28, 2008 20:52 |
|
In your code example you are not initializing cool::p1 with c. You are creating a new megaObj called p1, and initializing it with c. After the first line in cool's constructor, you have two megaObj's called p1. There's this->p1 (or cool::p1) and p1, they are not the same, and this->p1 is initialized by its default zero-parameter constructor.Triple Tech posted:Why, is it because of the order in which c is incremented? Or some other reason? Lexical Unit fucked around with this message at 21:33 on Jul 28, 2008 |
# ¿ Jul 28, 2008 21:26 |
|
beuges posted:Why this: What I wanted is for the key to be the name of the Atom. But if the key was a string, then someone could typo and the program wouldn't work. I'd have to do runtime checking to make sure the key passed to get() was a valid key. And then what do I do if it isn't? Throw and exception probably. So I made the enums be exactly what the key names should be, for clarity, and this will catch typos at compile time. Then I can use the same define, expand it and stringify it, and then parse it to get the string names that XInternAtoms() needs. In my real code I also use a std::map<available_atom, std::pair<Atom, std::string> > so that the user can get not only a reference to an Atom, but also the string name of an Atom if they so desire.
|
# ¿ Jul 29, 2008 15:15 |
|
|
# ¿ Apr 26, 2024 07:14 |
|
prolecat posted:In that case, you're stuck setting the members of the struct in the body of the class constructor, barring some language feature I'm unaware of. code:
|
# ¿ Jul 31, 2008 13:21 |