C++ operator overloading for embedded DSL language in C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to use C++ operator overloading and create a DSL-like syntax for embedded DsL code in c++.
"a": auto = call("add2Numbers", "b", "c");
This is what I would like ideally. But anything close to this as valid C++ would be acceptable.

Good advice: don't. C++ is a big multi purpose language and already complicated enough. You will confuse people (and yourself too!) if you randomly change stuff. Especially operator overloading and the proposed pre processor should be treated carefully.
My advice for you is to write the functions you need. You gave an example about some kind of assignment (sorry, don't understand the given code) and I'm sure it is perfectly possible to write a function with a convenient interface to achieve what you're trying to do. The advantage is that your synthax stays all C++ and possible readers (and again your future self) don't get confused.
I hope I understood your question properly. If not, please correct me.
Felix

Related

What piece of hardware or software on a computer/microcontroller determines what code you can write (and not write)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
So I am writing code for a microcontroller (using Arduino IDE), which I do not have a ton of experience doing. This question popped into my head when I was wondering which 'parts' of C++ I could use in my code that would flash onto this NRF52840 microcontroller. Basically, I am wondering: What on that board determines the code I must write for it?
I understand saying "what code can I write/not write" is broad, but its because I dont know what to say instead. A few guesses I have for substitutes for this would be: certain libraries? certain coding languages?, certain types of languages (interpreted vs compiled)?
Sorry if this question is too horribly stated to get an answer, but this was legit the best I could do lol.
In the case of using C++, it depends entirely upon what language features the compiler you are using supports. I suppose there might be hardware out there that is so simplistic that certain features are simply beyond the ability to implement, but I cannot tell you either what hardware that is, or what language features would be so effected.

What optimizations can a C++ compiler perform that a C compiler cannot? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've somehow implanted in my head the idea that a C++ compiler can perform certain optimizations that a C compiler cannot. I (think I) remember hearing this in a conference talk, perhaps regarding the use of C++ in embedded programming.
If I recall correctly, I think these optimizations had to do with the idea that you can qualify the use of pointers (and other means of indirection, like references) with more information at compile-time.
For instance, "const" is an example of such a compile-time human-supplied tag available both C and C++. Is there similar information that only C++ has?
Some things that spring to mind are the different types of iterators and their requirements, but I'm not sure if that allows for C++ to make some optimizations.
EDIT: I think the talk I had in mind was Dan Saks's cppcon 2016 presentation, but I realize now that (from what I understand) he mainly mentions how C++'s type system allows for better compile-time type-checking.
I think some of the examples I would enjoy hearing more about are things closer to how C++'s std::sort can be optimized more readily than C's qsort (thanks to multiple commenters). Explanations to when this sort of scenario occurs would be greatly appreciated.

Good C++ alternative to MATLAB's "fminunc"? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to convert some code written in MATLAB to C++. I'm having some (or actually quite a lot of) trouble finding an alternative to the "fminunc" function which is used in the MATLAB code that I can replace and use in the C++ code. I've been looking at the "dlib"-library because I've heard it could be a function there I can use, but I'm not sure what function to use.
This is how the "fminunc" is used in the MATLAB code I want to convert:
[theta, cost] = ...
fminunc(#(t)(costFunction(t, X, y)), initial_theta, options);
Does anyone know any good optimizing functions like this in C++?
I believe what you are looking for is Google's Ceres Solver, an open source C++ library for modeling and solving large, complicated optimization problems. The code is designed to handle two classes of problems:
Non-linear Least Squares problems with bounds constraints.
General unconstrained optimization problems.
Automatic Differentiation is also supported.
Several cool example applications can be found here.
There are a bunch of optimizers in dlib, some that use gradients and others than just work on black-box functions. You can see some examples here http://dlib.net/optimization_ex.cpp.html and more generally here http://dlib.net/optimization.html.

How to approach a C++ parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wanting to have a go at a C++ parser for a formatter I am making.
You can obviously open a file and use getline(..) or get(), is this reasonable way of starting things off and then working out a system using vector arrays and hence creating loads of arrays and somehow structuring out and processing what you are doing from there. For example say I wanted to find ever function in a source file, all functions have the common syntax, "(){" once whitespace has been removed, so do you just look for common delimeters to parse out the sections into arrays. I suppose I will learn as I go.
Or I also assume there are tried and tested ways of doing this, and I would likley just be reinventing the wheel as they say.
C++ is a language that is quite hard to parse in the first place. So if you want anything other that really trivial C++ code to be "understood" by your parser, you are definitely better off starting with an existing product.
The Clang frontend library would perhaps be a good starting point.
There are also a number of "source to source" conversion examples based on clang. Here's one of them: http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang/

Which design pattern should be used for algorithm implemented with two libraries [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My algorithm is implemented in two versions which each uses a specific library (MKL and ITK). These implementations are provided since one of the libraries only are available to our users. Note that at the compilation time only one should be loaded and get compiled. I was confused which design pattern should be used here.
Thanks
In C++, the template way do a "strategy design pattern" in called a policy. It's described quite well in the first chapter of Andrei Alexandrescu's book "Modern C++ Design". Why, the template way, you might ask? Because it's compile time and only the code you use will be compiled.
I can't explain much because I don't actually have much experience using this, but in brief, a template don't have to be a type (like int, Person, float, etc.), it can be a function or a class. So you can code something like
Printer<ScreenStrategy> p; // or wathever!