|
Could you use enable_if and is_base_of to have the templated copy constructor be skipped when T is any class that derives from Base?
|
# ? Oct 8, 2011 05:32 |
|
|
# ? Jun 8, 2024 05:41 |
|
Dicky B posted:I assumed that in cases where an explicit declaration is available it would take priority over a template method during overload resolution. This is actually true, but only if the template instantiates to the same thing as the non-template.
|
# ? Oct 9, 2011 05:09 |
|
rjmccall posted:This is actually true, but only if the template instantiates to the same thing as the non-template. if it doesn't, does the template always have priority, or is it undefined, or what?
|
# ? Oct 9, 2011 05:48 |
|
There's this big huge section about overload resolution in the standard, but basically, and glossing over a lot of details: 1. For every function template, use template argument deduction to try to come up with a minimal specialization that makes the argument types match the parameter types. Ignore any function template for which this fails. 2. Now that you have a bunch of candidates with concrete signatures, try to initialize all the parameters with the corresponding arguments. Ignore any candidate for which this fails; otherwise, remember what you had to do to initialize all those parameters. 3. If there are multiple viable candidates, try to find a candidate that's better than all the others. 3a. A cannot be better than B if the conversion sequence needed to initialize the nth parameter of A is worse than the sequence needed to initialize the nth parameter of B. Otherwise, A is better than B if 3b. any of the sequences for A is better than the corresponding sequence for B, or 3c. A is not a template specialization and B is, or 3b. A and B are both template specializations and A's template is more specialized than B's, which is based on this whole other complicated set of rules. ETA: So applying this to the current problem, template argument deduction (1) makes the constructor have parameter type 'const Derived &'. An lvalue of type 'const Derived' can be passed to both 'const Derived &' and 'const Base &', so both candidates survive (2). The conversion sequence of 'const Derived' -> 'const Derived' is better than the conversion sequence of 'const Derived' -> 'const Base', so (3b) says the template specialization is better. If you repeated this with an argument that's an lvalue of type 'const Base', TAD gives the template spec a parameter of type 'const Base &', so (3c) is required to break the tie. ETA more: This all forms the core of a pretty convincing argument against "unrestricted function templates": template argument deduction works too well, and the function template tends to grab all the calls, so it gets really hard to ever overload it. rjmccall fucked around with this message at 07:08 on Oct 9, 2011 |
# ? Oct 9, 2011 06:57 |
|
rjmccall posted:This all forms the core of a pretty convincing argument against "unrestricted function templates": template argument deduction works too well, and the function template tends to grab all the calls, so it gets really hard to ever overload it. by "unrestricted function templates" do you mean templates without concepts?
|
# ? Oct 9, 2011 14:43 |
|
I mean templates which accept essentially arbitrary arguments like this and that don't have some sort of enable_if guard on them.
|
# ? Oct 9, 2011 22:17 |
|
Okay, can someone help me understand what's going on in the following block?code:
Edit: VV Thanks, that helped. VV KiloVictorDongs fucked around with this message at 21:49 on Oct 11, 2011 |
# ? Oct 11, 2011 20:39 |
|
It's just pointlessly spelling out all the intermediate values in m_mapActualExp[uType][endDate][lType] += (int) numOfExpiringLeases;. If that's not meaningful to you, I don't think anyone here is going to help. ETA: unless you're confused about what the += operator does, in which case you need to read a book.
|
# ? Oct 11, 2011 21:03 |
|
I'm looking for any suggestions on using the preprocessor to do some conditionally-compiled debug information. Are there some mature systems for this, or anything that you do to conditionally log in your code? In other languages, I'm not so performance-bound so I usually just create a log class, or use something like log4j or log4n to log messages at different log levels. I'm wondering if I'm really freaking out in trying to conditionally compile it in, since I think a branch predictor should take care of things, and I don't think this code will ever get to some profane enterprise size where the instruction cache would get clogged. Then again, I could be wrong.
|
# ? Oct 11, 2011 21:34 |
|
Any non-brain-damaged compiler is smart enough to just completely drop a conditional block if the condition const-folds to false, and will completely drop a function call if the function is inlined, does nothing, and the parameter passing is operation-free (i.e. no copies).
|
# ? Oct 11, 2011 22:15 |
|
Yeah but conditionally compiling debug messages is completely easy. For example, just define a debugf function for debug messages and then in release mode, omit the definition and instead #define debugf(...) ((void)(0)). That's a better idea than some conditional thing because.. you're going to have a macro expand to a conditional expression? That's more crazy than having a function that's replaced by a macro that expands to nothing.
|
# ? Oct 11, 2011 23:08 |
|
shrughes posted:Yeah but conditionally compiling debug messages is completely easy. For example, just define a debugf function for debug messages and then in release mode, omit the definition and instead #define debugf(...) ((void)(0)). So having an inline code:
The alternative to that, which I have seen people use, is to have DEBUGF1, DEBUGF2, DEBUGF3 etc. defined so as to support more parameters, but that's horrible to use.
|
# ? Oct 11, 2011 23:19 |
|
roomforthetuna posted:The reason for doing it as a conditional thing is that, I believe, you can't do '...' as a parameter list for a #define macro. Sure you can.
|
# ? Oct 11, 2011 23:21 |
|
shrughes posted:Sure you can. vvvv Ah, so no getting at them individually within the #define. That makes more sense.
|
# ? Oct 11, 2011 23:28 |
|
#define debug_printf(format, ...) printf(format, __VA_ARGS__) Google for "variadic macros"
|
# ? Oct 11, 2011 23:29 |
|
Also, variadic functions will usually not compile away to nothing.
|
# ? Oct 12, 2011 00:03 |
|
Variadic macros are a C99 feature that is also now a C++11 feature. Lots of compilers that support them in C also supported them in C++03 as an extension. Microsoft's compiler has supported variadic macros for a quite a few releases in C++ mode IIRC even though they don't officially try to fully support C99. This is just a warning I guess if you are concerned about having truly standard C++03 code.
|
# ? Oct 12, 2011 01:06 |
|
Even if you're dealing with a compiler that can't do that, parameter lists enclosed in () only count as one parameter in macros. It looks kind of awkward but it works. i.e.: code:
|
# ? Oct 12, 2011 01:30 |
|
I need a (paid, good) tutor for a Data Structures course. My teacher is mediocre and the material is becoming a little too difficult for me to piece together myself without driving myself up a wall. I need someone who knows their poo poo and would be able to answer the abidance of questions I have every week after looking at our sample code and lectures. It's entirely C++, which is why I asked in this thread. Anyone have any suggestions where I could find someone? Some other web community, maybe? I'm going to ask at my school but I figured I'd see if anyone here had any suggestions.
|
# ? Oct 12, 2011 04:22 |
|
you can just ask questions here and we'll answer them for free. :P
|
# ? Oct 12, 2011 04:45 |
|
TasteMyHouse posted:you can just ask questions here and we'll answer them for free. :P This. Don't worry about asking lots of questions. As long as you are not asking us to do your homework for you, we will gladly help you out. Responses are usually fairly quick as well.
|
# ? Oct 12, 2011 14:32 |
|
I will do your homework for you for Packers season tickets.
|
# ? Oct 12, 2011 15:06 |
|
I'm way too baller for your free help.
|
# ? Oct 12, 2011 16:11 |
|
tractor fanatic posted:I will do your homework for you for Packers season tickets. I hope I'm not still in school in 30 or so years As far as supplementary data structure reading goes, do you guys have suggestions for essential books? My teacher suggests "Data Structures and Other Objects in C++". I also have a copy of Jeffrey Child's "Classes and Data Structures in C++", which is decent but sorta jumps all over much like my lecture notes.
|
# ? Oct 12, 2011 16:41 |
|
Re: debug logging, I was going to post back asking if there was anything standard or good out there that does a lot of logging already. Doing one preprocessor method would be good for some things, but I wanted something more sophisticated . . . . . . but then I realized I was being a hypocrite. There is indeed a log4cxx that's in the veins of log4j or log4net. I look at it and think it's too heavyweight. So I guess I need to figure out what the heck I even want to do. I have a different question about boost threads. I spawn off a bunch of threads and then join them back at main at the end of the program. However, my program doesn't terminate without a break signal. I am assuming I must have missed some thread somewhere. Is there a way to figure out which one easily? Like with gdb? Or is there something more I have to do than join?
|
# ? Oct 12, 2011 16:44 |
GDB can tell you whether you still have threads running, at least. A rather standard command is thread apply all bt, run a backtrace on every thread. Joining a thread should be sufficient to free all its resources. You have checked that all of your joins actually complete, right? (Add a print or breakpoint after the last one.)
|
|
# ? Oct 12, 2011 17:10 |
|
nielsm posted:GDB can tell you whether you still have threads running, at least. Thanks for the gdb operation. I'll try that tonight. I thought all my threads joined okay, but I'm assuming a join is a blocking operation here. Is there any reason it wouldn't? All the of threads I run are stored in objects with a common interface, and I basically expose a join method for them to call the join on their internal threads if need be; some of them don't have a thread so they just return back. I put all these in a container and just iterate the whole mess, calling join one-at-a-time. Before doing this, I've already gone through and basically polled that each one has signalled they exited their main run body; I suppose a wait-notify system would be better blablabla. But at the point of joining I don't see it get caught up. So there's probably some dingleberry thread elsewhere.
|
# ? Oct 12, 2011 17:26 |
|
I am a new C++ person. I use the newest version of Xcode. I am moving into file input/output. I can't open files however, because the program can't find my file. First thought was that the file I created was not in the correct directory. I tried moving it to every possible directory, the program still can't see it. What gives? - Inside my project folders, there are no "debug" folders. - I place the data file to be read inside the project directory.
|
# ? Oct 12, 2011 20:28 |
|
Try supplying an absolute filename rather than a relative one. You can also see what the working directory of your program is with the function "getcwd()" as demonstrated here e: changed link to use PATH_MAX TasteMyHouse fucked around with this message at 22:11 on Oct 12, 2011 |
# ? Oct 12, 2011 20:54 |
|
Cool, so there is a debug folder, just in a totally different place. I got it working thanks to you. Problem: solved!
|
# ? Oct 12, 2011 21:33 |
|
Progression Please posted:Cool, so there is a debug folder, just in a totally different place. I got it working thanks to you. Problem: solved! Despite everything working, I thought I'd add to double check the delimeters in your strings. If you're doing "C:\this\that\nother thing" those slashes are going to be interpreted as "C:\[tab]his[tab]hat[newline]other thing" unless you put another slash in front of them.
|
# ? Oct 12, 2011 22:40 |
|
Rocko Bonaparte posted:Despite everything working, I thought I'd add to double check the delimeters in your strings. If you're doing "C:\this\that\nother thing" those slashes are going to be interpreted as "C:\[tab]his[tab]hat[newline]other thing" unless you put another slash in front of them. He mentioned that he was using X Code, so he's on OS X, not Windows.
|
# ? Oct 12, 2011 22:44 |
|
TasteMyHouse posted:He mentioned that he was using X Code, so he's on OS X, not Windows. Well that's what I get. Regarding the gdb thread checking, I ran it and found all my threads were still alive. So I checked and found that I wrote all the code to join up the threads, but didn't actually have it hooked up anywhere it would get run. So that was easy. I'm starting to reign in most of the memory leaks in my program. I'm trying to make some sense of what valgrind considers as "possibly lost." The FAQ kind of stinks: quote:"possibly lost" means your program is leaking memory, unless you're doing funny things with pointers. This is sometimes reasonable. Use --show-possibly-lost=no if you don't want to see these reports.
|
# ? Oct 13, 2011 19:25 |
|
I would guess that valgrind can find a pointer to the interior of the allocation, but not to the start of the allocation. It's possible to do that and still know how to re-derive the start but valgrind is right to be suspicious. (operator new[] actually does this implicitly, but I assume valgrind knows about that and special-cases it somehow) More devious tricks, like storing the only pointer to an allocation as a relative offset instead of an absolute value, would probably just get reported as "definitely lost".
|
# ? Oct 13, 2011 21:26 |
|
I have a program that takes a command line argument which should be an integer (e.g., ./program 42), and I need a way to ensure that an integer was entered, else it should print an error. How do I do that?
|
# ? Oct 14, 2011 00:00 |
Ziir posted:I have a program that takes a command line argument which should be an integer (e.g., ./program 42), and I need a way to ensure that an integer was entered, else it should print an error. How do I do that? code:
Since argc counts the number entries in the argv array, when argc is 1 you have just the name of your executable and zero arguments, when it is 2 you have your executable name and one argument, and so forth. You should now be able to answer your own question.
|
|
# ? Oct 14, 2011 00:12 |
|
erm, not quite what he's asking... Ziir -- if you take the string in the argv array that corresponds to the argument you're talking about and use it to initialize a stringstream object, you can attempt to extract formatted data from the stringstream, and if the extraction fails, you know that the argument is not an integer. like this: code:
|
# ? Oct 14, 2011 01:31 |
|
TasteMyHouse posted:erm, not quite what he's asking... pre:$ ./wrong 10asdf hey! good job. int was: 10
|
# ? Oct 14, 2011 01:52 |
|
oh drat. What's the best way to do this without boost, then? I'm actually curious myself now. I guess you could just check if eof is set as well after extracting the int?
|
# ? Oct 14, 2011 02:07 |
|
|
# ? Jun 8, 2024 05:41 |
|
pseudorandom name posted:
This is why designing a program is difficult, and programming a thing to someone else's inadequately specific design is worse. Edit: quote:oh drat. What's the best way to do this without boost, then? I'm actually curious myself now Edit2: You might also want to make sure it's not too many digits for the number to fit into an integer, if you're really wanting to be anal about input value checking. roomforthetuna fucked around with this message at 02:19 on Oct 14, 2011 |
# ? Oct 14, 2011 02:08 |