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
ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Is there a general stigma against using namespace std; or similar at global scope within a source file? None of the places I've worked have had an issue with it, but I've seen it mentioned as a bad thing and now I'm wondering.

(Obviously header files are a different story, so I'm specifically asking about source files.)

Adbot
ADBOT LOVES YOU

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


If you're developing on Windows you're going to have a bad time.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Do you actually care about the difference in performance?

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


The more I see the more I'm convinced that multiple inheritance is almost always a mistake.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Multiple implementation inheritance can maybe be OK if the class hierarchies involved are completely orthogonal. Early in my career I worked on a multiplatform graphics engine that allowed us to design forms for a desktop application. We had one set of classes that represented the abstract logic for various controls, and another that handled everything OS-specific. The only issues with that were related to the inheritance hierarchy being deep and C++ not having a mechanism to require every class deriving from a given parent to supply its own implementation of some pure virtual function (hello object slicing!).

We probably still should've used composition, though.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Ralith posted:

They can be convenient sometimes, but you never need them.

It's just a method of organizing your code inside a class. The upside is that if a class is declared private inside another class, there's absolutely no way for anything outside that class to access it without modifying the declaration of the outer class.

I can't think of a situation off the top of my head where that's a really good design, but it's there if you want it.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


I guess that falls under the category of things you can do but why.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


C++20 approved

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


The problem is that your approach only works for very small projects, and a build system that only supports small projects isn't all that useful. Everything's built to scale.

CMake is probably as close as you're going to get. For a small project where the instructions are just "compile everything in this folder and make an executable out of it", it's pretty simple to come up with something that works.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Just declare everything const and throw around a lot of mutable's until things compile.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Look at disjoint sets.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


The fact that no one else uses CMake shouldn't prevent you from using it, unless it's not installed on your build machines. If it is, you can just provide a script that calls CMake and let the larger build system run that.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


OneEightHundred posted:

What kind of situations does that happen in? I thought the assumption was that all unspecified digits were zero.

1 could be anything between 0.5 and 1.5, but 1.0 can only be between 0.95 and 1.05.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


fourwood posted:

Nah. Don’t report 3+ sig figs on your uncertainties. Please give me reputable sources to change my mind/undo my years of “simplistic” learning.

See propagation of uncertainty and references therein.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


IME make is easier for a small project but CMake is much better for large projects. I'm not sure exactly where the point of tradeoff is, but I've definitely run into it on projects that aren't all that large.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


#ifdef __cplusplus gives you a section that won't be seen by a C compiler. You can do a lot with that.

It's really tough to write that without a closing #endif.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


matti posted:

#undef __cplusplus
# include <unicode/whatever.h>
#define __cplusplus

ah yea! thanks, works

I really want to hear the story behind this one.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Zopotantor posted:

That is a stupid article. Not only is there a cheap, clever and simple algorithm to detect cycles in a linked list (Floyd's cycle detection algorithm, which every programmer ought to know just because of the "tortoise and hare" technique it uses), but there are actually real world use cases for it (I had to implement it long ago as part of a consistency check in a CAD system).

So you agree that it's a trivia question?

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Part of my job involves designing new algorithms. The only time I've ever heard of the cycle detection algorithm is in the context of interview questions, and I never knew the name until it came up in this thread. I can see it mattering in the late 60s back when every program could directly overwrite arbitrary parts of memory, but if you're writing anything at all modern, it's just not worth worrying about.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


The facilities that Rcpp provides to C++ code are just regular C++ libraries. You don't need to be using R at all to use them. The package build process through RStudio or similar does handle things like include/library paths, though, so you do have to set that up if you're not working with R.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.



https://twitter.com/trap0xf/status/1364121979796643841

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


ultrafilter posted:

The facilities that Rcpp provides to C++ code are just regular C++ libraries. You don't need to be using R at all to use them. The package build process through RStudio or similar does handle things like include/library paths, though, so you do have to set that up if you're not working with R.

Thinking about this some more, and I'm pretty sure it's true for the data structures that Rcpp provides. You'd probably run into some problems trying to use the random number generator library, though.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


What are you trying to do and why does that seem like a good solution?

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


You get one copy constructor per class. If you want more, you need more classes. This is probably better solved by composition than inheritance.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


I wish I had a better alternative to C++, but that's not happening any time soon. Everything I do is either legacy code where we're just stuck with what we've got, or writing an R package where no other reasonably performant language is really supported.

ultrafilter fucked around with this message at 18:06 on Mar 5, 2021

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Insurrectum posted:

Everything's fun and peachy in a compiled language until you have to touch a string

Also, yeah, not having to worry about the performance of a loop is great. But having to write loops every time I want to loop over something is extremely tiring. My biggest complaint about C++ is that it forces me to spend a lot of time thinking about things that I don't care about. That's gotten better with the modern standard library but it's still not all the way there yet.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


code:
#define { }
#define } {

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Just enforce a rule that heaps are always stack-allocated and stacks are always heap-allocated. You'll never be confused again.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Just out of curiosity, why do you need to know than an A2 is not an A3?

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


I'm going to need to implement an algorithm to compute N(i, j, k) for some function N that I'm defining. The recurrence relation for N is currently written as N(i, j, k) = \sum_c N(i, j - c, k - 1), but I'm not attached to the order of the variables. A straightforward recursive implementation does a lot of duplicate calls, so I want to use dynamic programming. The ranges of i, j and k are constant. What's likely to be the most performant way to implement this?

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


rjmccall posted:

So i is constant across the recurrence? It looks like you can think of this as a two-dimensional table with j+1 rows and k+1 columns. Conveniently, computation of any particular cell depends directly on values from the immediately preceding column (k-1). That admits an efficient algorithm where you can logically sweep left to right across the table, remembering only the previous column while building up the next. And since each column needs every cell from the previous column, there’s no point in being lazy about any of the cells.

So basically, allocate two columns as arrays, initialize the first for k == 0, and then iteratively build to higher values of k, computing each row in each from the values in the previous. After you’ve built the column for k-1 (the last full column you need), you can just skip to computing the last cell.

i is constant across the recurrence but at the end I need to sum over it. You're right though that I don't need to remember more than the previous value of k, which makes this significantly easier.

Xerophyte posted:

I'm not entirely sure given how you've written the relation but am I correct in understanding that 0 ≤ c < j when summing? In that case you have something like a summed area table by default and can avoid actually iterating over the previous column, because N(i, j, k) = Σc N(i, j - c, k - 1) = N(i, j - 1, k) + N(i, j, k - 1).

If so you'd still compute the full k-1:th column before the k:th column, but computing any individual value in the next column is just two lookups: the value immediately to the "left" in the previous column plus the value immediately preceding in the current column.

That's the correct range but there are some other functions of j and c that go into the sum. I left them out because that doesn't really affect the choice of data structure.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


rjmccall posted:

Yeah, as long as the computation trees are totally independent for different values of i, you aren't going to see any benefit from dynamic programming there.

Now if this whole computation is triggered a bunch for different argument values, that's different.

No, everything's the same. There's an input of size n and the ranges for i, j and k are computed from that in complicated ways. The key, though, is that the ranges for any one variable only depend on the overall input.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


malloc(n) allocates n bytes of memory. There's no default value for that parameter, so you have to specify something. The sizeof operator is your friend here.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


I really like sequence diagrams for describing how the components of a system interact. We should use them more often.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


What would you pick?

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


They might've just hosed up and set the timeout too low.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Look into X macros.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


CMake is a high-level scripting language that's used to generate a makefile. On any platform where it's supported, it is absolutely going to be easier to use than an actual platform-specific makefile if you have a project of any real complexity. The learning curve is not entirely gentle, but I think it's worth it.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


CMake is fine and good as long as your script works, but god help you if you need to debug anything.

Adbot
ADBOT LOVES YOU

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


glmnet is probably as close as you're going to get. C++ isn't a terribly popular language for stats outside of the guts of R.

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